Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a 'search key') and explores the neighbor nodes first, before moving to the next level neighbors.
依此类推,直到图中所有顶点都被访问完为止。
下面是Java实现的无向图的广度优先搜索:
public class BreadthFirstPaths { private static final int INFINITY = Integer.MAX_VALUE; private boolean[] marked; // marked[v] = is there an s-v path private int[] edgeTo; // edgeTo[v] = previous edge on shortest s-v path private int[] distTo; // distTo[v] = number of edges shortest s-v path /** * Computes the shortest path between the source vertex <tt>s</tt> * and every other vertex in the graph <tt>G</tt>. * @param G the graph * @param s the source vertex */ public BreadthFirstPaths(Graph G, int s) { marked = new boolean[G.V()]; distTo = new int[G.V()]; edgeTo = new int[G.V()]; bfs(G, s); assert check(G, s); } /** * Computes the shortest path between any one of the source vertices in <tt>sources</tt> * and every other vertex in graph <tt>G</tt>. * @param G the graph * @param sources the source vertices */ public BreadthFirstPaths(Graph G, Iterable<Integer> sources) { marked = new boolean[G.V()]; distTo = new int[G.V()]; edgeTo = new int[G.V()]; for (int v = 0; v < G.V(); v++) distTo[v] = INFINITY; bfs(G, sources); } // breadth-first search from a single source private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); for (int v = 0; v < G.V(); v++) distTo[v] = INFINITY; distTo[s] = 0; marked[s] = true; q.enqueue(s); while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; distTo[w] = distTo[v] + 1; marked[w] = true; q.enqueue(w); } } } } // breadth-first search from multiple sources private void bfs(Graph G, Iterable<Integer> sources) { Queue<Integer> q = new Queue<Integer>(); for (int s : sources) { marked[s] = true; distTo[s] = 0; q.enqueue(s); } while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { edgeTo[w] = v; distTo[w] = distTo[v] + 1; marked[w] = true; q.enqueue(w); } } } } /** * Is there a path between the source vertex <tt>s</tt> (or sources) and vertex <tt>v</tt>? * @param v the vertex * @return <tt>true</tt> if there is a path, and <tt>false</tt> otherwise */ public boolean hasPathTo(int v) { return marked[v]; } /** * Returns the number of edges in a shortest path between the source vertex <tt>s</tt> * (or sources) and vertex <tt>v</tt>? * @param v the vertex * @return the number of edges in a shortest path */ public int distTo(int v) { return distTo[v]; } /** * Returns a shortest path between the source vertex <tt>s</tt> (or sources) * and <tt>v</tt>, or <tt>null</tt> if no such path. * @param v the vertex * @return the sequence of vertices on a shortest path, as an Iterable */ public Iterable<Integer> pathTo(int v) { if (!hasPathTo(v)) return null; Stack<Integer> path = new Stack<Integer>(); int x; for (x = v; distTo[x] != 0; x = edgeTo[x]) path.push(x); path.push(x); return path; } // check optimality conditions for single source private boolean check(Graph G, int s) { // check that the distance of s = 0 if (distTo[s] != 0) { StdOut.println("distance of source " + s + " to itself = " + distTo[s]); return false; } // check that for each edge v-w dist[w] <= dist[v] + 1 // provided v is reachable from s for (int v = 0; v < G.V(); v++) { for (int w : G.adj(v)) { if (hasPathTo(v) != hasPathTo(w)) { StdOut.println("edge " + v + "-" + w); StdOut.println("hasPathTo(" + v + ") = " + hasPathTo(v)); StdOut.println("hasPathTo(" + w + ") = " + hasPathTo(w)); return false; } if (hasPathTo(v) && (distTo[w] > distTo[v] + 1)) { StdOut.println("edge " + v + "-" + w); StdOut.println("distTo[" + v + "] = " + distTo[v]); StdOut.println("distTo[" + w + "] = " + distTo[w]); return false; } } } // check that v = edgeTo[w] satisfies distTo[w] + distTo[v] + 1 // provided v is reachable from s for (int w = 0; w < G.V(); w++) { if (!hasPathTo(w) || w == s) continue; int v = edgeTo[w]; if (distTo[w] != distTo[v] + 1) { StdOut.println("shortest path edge " + v + "-" + w); StdOut.println("distTo[" + v + "] = " + distTo[v]); StdOut.println("distTo[" + w + "] = " + distTo[w]); return false; } } return true; } /** * Unit tests the <tt>BreadthFirstPaths</tt> data type. */ public static void main(String[] args) { In in = new In(args[0]); Graph G = new Graph(in); // StdOut.println(G); int s = Integer.parseInt(args[1]); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); for (int v = 0; v < G.V(); v++) { if (bfs.hasPathTo(v)) { StdOut.printf("%d to %d (%d): ", s, v, bfs.distTo(v)); for (int x : bfs.pathTo(v)) { if (x == s) StdOut.print(x); else StdOut.print("-" + x); } StdOut.println(); } else { StdOut.printf("%d to %d (-): not connected\n", s, v); } } } }
其中BFS向量每经过一次乘运算后,新添加的非零值即为搜索结点。
另外,不难发现如果用与对角矩阵X进行若干次相乘后,即可得到以图中每个结点为起点,进行BFS搜索后构成的结果矩阵。
import numpy as np from numpy.random import rand def init(dimension, startVertex = 0): mat = np.ones((dimension, dimension), dtype = np.int32) mat[(rand(dimension ** 2 / 2) * dimension).astype(int), (rand(dimension ** 2 / 2) * dimension).astype(int)] = 0 for i in range(dimension): mat[i, i] = 1 return mat def bfsSingal(adjacencyMat, startVertex): def nonZeroNum(vector): return len(np.nonzero(vector)[0]) adjacencyMat = adjacencyMat.T n = np.shape(adjacencyMat)[0] current = np.zeros((n, 1), dtype = np.int32) current[startVertex, 0] = 1 i = 0 stop = False print("The %sth search: %s" % (i, current.T)) while(not stop): i += 1 next = np.dot(adjacencyMat, current) stop = nonZeroNum(current) == nonZeroNum(next) if(not stop): current = next print("The %sth search: %s" % (i, current.T)) if __name__ == "__main__": adjacencyMat = init(7, 3) adjacencyMat1 = np.array([[1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 0], [1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 1, 1, 0, 1]]) adjacencyMat2 = np.array([[1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 0], [1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 1, 0, 1]]) bfsSingal(adjacencyMat1, 3)