-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathAdjacencyListGraph.java
More file actions
151 lines (118 loc) · 4.03 KB
/
Copy pathAdjacencyListGraph.java
File metadata and controls
151 lines (118 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package util;
import java.util.*;
public class AdjacencyListGraph {
private Map<Integer, List<Integer>> adjacencyList;
// Constructor
public AdjacencyListGraph() {
this.adjacencyList = new HashMap<>();
}
// Add a vertex to the graph
public void addVertex(int vertex) {
adjacencyList.put(vertex, new LinkedList<>());
}
// Remove a vertex from the graph
private void removeVertex(int vertex) {
adjacencyList.remove(vertex);
// Remove edges pointing to the removed vertex
for (List<Integer> neighbors : adjacencyList.values()) {
neighbors.remove((Integer) vertex);
}
}
// Add an edge between two vertices
public void addEdge(int source, int destination) {
adjacencyList.get(source).add(destination);
adjacencyList.get(destination).add(source);
}
// Remove an edge between two vertices
public void removeEdge(int source, int destination) {
adjacencyList.get(source).remove((Integer) destination);
adjacencyList.get(destination).remove((Integer) source);
}
// Display the adjacency list
public void printGraph() {
for (Map.Entry<Integer, List<Integer>> entry : adjacencyList.entrySet()) {
System.out.print(entry.getKey() + " -> ");
for (Integer neighbor : entry.getValue()) {
System.out.print(neighbor + " ");
}
System.out.println();
}
}
public void BFSIterative(int startVertex) {
Set<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
queue.add(startVertex);
visited.add(startVertex);
// Keep traversing till the queue is empty
while (!queue.isEmpty()) {
// Get an element from the queue and mark as visited
int currentVertex = queue.poll();
System.out.print(currentVertex + " ");
// Add all the neighbors to the queue
for (int neighbor :
adjacencyList.getOrDefault(currentVertex, Collections.emptyList()))
if (!visited.contains(neighbor)) {
queue.add(neighbor);
visited.add(neighbor);
}
}
}
public void DFSIterative(int startVertex) {
Set<Integer> visited = new HashSet<>();
Stack<Integer> stack = new Stack<>();
stack.push(startVertex);
// Keep traversing until stack is empty
while (!stack.isEmpty()) {
int currentVertex = stack.pop();
// Get an element from stack and mark as visited
// if we haven't visited it already
if (!visited.contains(currentVertex)) {
System.out.print(currentVertex + " ");
visited.add(currentVertex);
// Add all the unvisited neighbors to stack
for (int neighbor :
adjacencyList.getOrDefault(currentVertex, Collections.emptyList()))
if (!visited.contains(neighbor))
stack.push(neighbor);
}
}
}
public void DFS(int startVertex) {
Set<Integer> visited = new HashSet<>();
DFSRecursive(startVertex, visited);
}
private void DFSRecursive(int vertex, Set<Integer> visited) {
// Add the vertex to visited set
visited.add(vertex);
System.out.print(vertex + " ");
// Iterate through each neighbor of the vertex
for (int neighbor :
adjacencyList.getOrDefault(vertex, Collections.emptyList()))
// If not visited, perform dfs on the vertex
if (!visited.contains(neighbor))
DFSRecursive(neighbor, visited);
}
public static void main(String[] args) {
AdjacencyListGraph graph = new AdjacencyListGraph();
// Adding vertices
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
// Adding edges
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.addEdge(3, 1);
// Displaying the graph
System.out.println("Graph after adding vertices and edges:");
graph.printGraph();
// Removing vertex 2
graph.removeVertex(2);
graph.removeVertex(1);
System.out.println("\nGraph after removing vertex 2:");
graph.printGraph();
// Removing edge (3, 1)
graph.removeEdge(3, 1);
System.out.println("\nGraph after removing edge (3, 1):");
graph.printGraph();
}
}