0%
前言
图之广度优先搜索的Java版API实现
源代码
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
| package com;
public class BreadthFirstSearch {
private boolean[] marked; private int nodeCount; private Queue<Integer> waitSearch;
BreadthFirstSearch(Graph graph, int node) { this.marked = new boolean[graph.getNodeLength()]; this.nodeCount = 0; this.waitSearch = new Queue<>();
bfs(graph, node); }
public int getCount() { return nodeCount; }
private void bfs(Graph graph, int node) { marked[node] = true; waitSearch.enQueue(node); while (!waitSearch.isEmpty()) { Integer wait = waitSearch.deQueue(); for (Integer n : graph.adj(wait)) { if (!marked[n]) { bfs(graph, n); } } } nodeCount++; }
public boolean marked(int node) { return marked[node]; }
}
|
完成
参考文献
哔哩哔哩——黑马程序员