-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBFSUndirected.java
More file actions
87 lines (66 loc) · 1.95 KB
/
BFSUndirected.java
File metadata and controls
87 lines (66 loc) · 1.95 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
// http://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
import java.util.LinkedList;
import java.util.Iterator;
class BFSUndirected {
int numOfVertices;
LinkedList<Integer> adjacencyList[];
BFSUndirected(int numOfVertices) {
this.numOfVertices = numOfVertices;
adjacencyList = new LinkedList[numOfVertices];
while(numOfVertices-- > 0) {
adjacencyList[numOfVertices] = new LinkedList();
}
}
void addEdge(int v1, int v2) {
adjacencyList[v1].add(v2);
adjacencyList[v2].add(v1);
}
void printGraph() {
for(int i = 0; i < adjacencyList.length; i++) {
System.out.print(i + " - ");
for(int j = 0; j < adjacencyList[i].size(); j++) {
System.out.print(adjacencyList[i].get(j) + " ");
}
System.out.println();
}
}
void bfs(int s) {
boolean[] visited = new boolean[numOfVertices];
LinkedList<Integer> queue = new LinkedList<>();
visited[s] = true;
queue.add(s);
while(queue.size() != 0) {
s = queue.poll();
System.out.print(s + " ");
// now find the neighbors and add it to queue
//using Iterator
Iterator<Integer> i = adjacencyList[s].listIterator();
while(i.hasNext()) {
int n = i.next();
if(!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
// using adjacencyList directly
/* for(int i = 0; i < adjacencyList[s].size(); i++) {
if(!visited[adjacencyList[s].get(i)]) {
queue.add(adjacencyList[s].get(i));
visited[adjacencyList[s].get(i)] = true;
}
} */
}
}
public static void main(String[] args) {
System.out.println("BFS");
BFSUndirected bfsUndirected = new BFSUndirected(4);
bfsUndirected.addEdge(0, 1);
bfsUndirected.addEdge(0, 2);
bfsUndirected.addEdge(1, 2);
bfsUndirected.addEdge(2, 0);
bfsUndirected.addEdge(2, 3);
bfsUndirected.addEdge(3, 3);
bfsUndirected.printGraph();
bfsUndirected.bfs(2);
}
}