-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
48 lines (39 loc) · 1.58 KB
/
BreadthFirstSearch.java
File metadata and controls
48 lines (39 loc) · 1.58 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
import SupportData.GraphSearchUtil;
import java.util.*;
/**
* 广度优先搜索;
* 图的搜索算法,对每个节点:搜索其子节点(相连节点),如果该节点被搜索过,那么就跳过,否则加入到搜索节点队列;当前节点完成后,从队列中选择
* 第一个节点继续搜索.直到队列中不再有节点.
*
*/
@SuppressWarnings("unused")
public class BreadthFirstSearch implements AlgorithmInGraph{
public void showAlgorithm() {
int [][] graph = GraphSearchUtil.generateGraph(5);
System.out.println("当前生成的图为:");
GraphSearchUtil.printGraph(graph);
List<Integer> orders = search(graph);
System.out.println("广度优先搜索的节点依次为(从0节点开始搜索):");
System.out.println(Arrays.toString(orders.toArray()));
}
public List<Integer> search(int [][] graph){
int point_num = graph[0].length;
List<Integer> result = new ArrayList<Integer>();
Queue<Integer> queue = new LinkedList<Integer>();
List<Integer> searchedList = new ArrayList<Integer>(); //已经查找过的点
queue.offer(0);
searchedList.add(0);
int index = 0;
while(!queue.isEmpty()){
Integer currentPoint = queue.poll();
result.add(currentPoint);
for(int i = 0;i<graph[currentPoint].length;i++){
if(!searchedList.contains(i) && graph[currentPoint][i] == 1){
queue.offer(i);
searchedList.add(i);
}
}
}
return result;
}
}