-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
85 lines (73 loc) · 2.85 KB
/
BreadthFirstSearch.java
File metadata and controls
85 lines (73 loc) · 2.85 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
/*
* Code by:@yinengy
* Time: 9/22/2018
*
* Correspond to the algorithm state in page 90 and 91,
* all /* comments are from the book.
*/
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
public class BreadthFirstSearch {
/**
* main algorithm
*
* @param graph the graph in Adjacency List form
* @param s the root node
* @return a tree generated BFS
*/
public static AdjacencyList BFS(AdjacencyList graph, int s) {
/* Set Discovered[s] = true and Discovered[v] = false for all other v */
Boolean[] Discovered = new Boolean[graph.getNumV() + 1]; //initial false
for (int n = 0; n < Discovered.length; n++){
Discovered[n] = false;
}
Discovered[s] = true;
/* Initialize L[0] to consist of the single element s */
Queue<Integer>[] L = new Queue[graph.getNumV()+1]; // store all pointers to those layers
L[0] = new LinkedBlockingQueue<>();
L[0].add(s);
/* Set the layer counter i = 0 */
int i = 0;
/* Set the current BFS tree T = ∅ */
AdjacencyList tree = new AdjacencyList(graph.getNumV()); // use AdjacencyList to repent a new tree
/* While L[i] is not empty */
while (!L[i].isEmpty()) { // O(n) to set up L
/* Initialize an empty list L[i + 1] */
L[i + 1] = new LinkedBlockingQueue<>();
/* For each node u ∈ L[i] */
while (!L[i].isEmpty()) { // O(m) for consider all edges in all loops
/* Consider each edge (u, v) incident to u */
int u = L[i].remove();
LinkedList<Integer> edges = graph.getEdges(u);
for (int v : edges) { // O(degree(n))
/* If Discovered[v] = false then */
if (!Discovered[v]) {
/* Set Discovered[v] = true */
Discovered[v] = true;
/* Add edge (u, v) to the tree T */
tree.addEdge(u, v);
//tree.addEdge(v, u); //uncomment this line will get undirected tree
/* Add v to the list L[i + 1] */
L[i + 1].add(v);
} /* Endif */
}
}/* Endfor */
/* Increment the layer counter i by one */
i++;
}/* Endwhile */
// all in all, O(n+m)
return tree;
}
/**
* simple test case use the csv file in /test/
*/
public static void main(String[] args) throws FileNotFoundException {
AdjacencyList graph = new AdjacencyList(8);
graph.addFromCSV("test\\graph.csv");
System.out.println(graph);
AdjacencyList tree = BFS(graph,1);
System.out.println(tree);
}
}