-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphBFS.java
55 lines (44 loc) · 1.34 KB
/
GraphBFS.java
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
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
// 广度优先遍历
public class GraphBFS {
private Graph G;
private boolean[] visited;
private ArrayList<Integer> order = new ArrayList<>(); //先序遍历结果
public GraphBFS(Graph G){
this.G = G;
visited = new boolean[G.V()];
// 多包一层for,防止有多个联通分量
for(int v = 0 ;v<G.V();v++){
if(!visited[v])
bfs(v);
}
}
// 广度优先遍历 , 路径同时是最短路径
private void bfs(int s){
Queue<Integer> queue = new LinkedList<>(); // LinkedList 实现了queue接口
// 入队并标记访问
queue.add(s);
visited[s] = true;
while(!queue.isEmpty()){
int v = queue.remove(); //从队列取出元素
order.add(v);
for(int w:G.adj(v)){
if(!visited[w]) {
// 入队并标记访问
queue.add(w);
visited[w] = true;
}
}
}
}
public Iterable<Integer> order(){
return order;
}
public static void main(String[] args){
Graph g = new Graph("g.txt");
GraphBFS graphBFS = new GraphBFS(g);
System.out.println(graphBFS.order());
}
}