Breadth-first
Visits vertices level by level from a start vertex.
Time complexity O(V+E), gograph function traverse.BFSWithDepth.
traverse.BFS gives the same order without the depths, and traverse.NewBreadthFirstIterator gives it one Next at a time. path.BFSPaths returns the route with the fewest edges to each vertex, the edges the steps draw.
How it works
Breadth-first search keeps a queue of the vertices it has found but not visited yet. It takes the vertex at the front and puts each neighbor it hasn't seen at the back. So every vertex one edge from the start comes out before any vertex two edges away, and so on, one level at a time.
When every edge counts the same, that order gives the fewest edges from the start to each vertex, such as the fewest hops between two machines. gograph marks a vertex as seen when it joins the queue, so each one is visited once and the search costs O(V+E). Vertices the start can't reach are never visited.
traverse.BFS and traverse.BFSWithDepth return an iter.Seq, so a range loop can break as soon as it finds what it needs, and BFSWithDepth gives each vertex with its number of edges from the start. path.BFSPaths runs the same search and keeps the edge that first found each vertex, so PathTo on its result gives a route with the fewest edges to any vertex, start first, and false for one out of reach. When edges have different weights, the fewest edges isn't the shortest route, and Closest-first or Dijkstra gives the real distances.
Maze
A maze of 30 cells in five rows, A to E, and six columns, 1 to 6, so C4 is row C, column 4. Each edge is an open passage between two cells. The way from A1 to E6 winds through most of the maze, and two short loops give a few cells a second route.
An undirected graph with 30 vertices and 31 edges.
Starting at A1, find every cell of the maze, the cells fewest steps away first.
From A1, breadth-first visits 30 vertices: A1, A2, A3, B3, B2, B1, C1, C2, D2, E2, and so on to E5.
Step by step
- Start at A1. Put it in the queue.
- Take A1 from the front of the queue.
- Add A2, the only unseen neighbor of A1, to the back of the queue.
- Take A2 from the front of the queue.
- Add A3, the only unseen neighbor of A2, to the back of the queue.
- Take A3 from the front of the queue.
55 more steps lead to the last one:
- Done. Breadth-first reached 30 vertices from A1.
Use it in Go
for v, depth := range traverse.BFSWithDepth(g, "A1") {
fmt.Println(v.Label(), depth)
}
paths, err := path.BFSPaths(g, "A1")
if err != nil {
return err
}
route, _ := paths.PathTo("E5")
for _, v := range route {
fmt.Println(v.Label())
}
Example graphs
- Home network, 6 vertices and 6 edges
- Office network, 13 vertices and 16 edges
- Maze, 30 vertices and 31 edges
- Karate club, 34 vertices and 78 edges
- Campus network, 100 vertices and 114 edges
More in Traversal
- Depth-first: Follows one path as deep as it can, then backs up to the last vertex with a neighbor left to try, and sorts every edge it meets into four kinds.
- Closest-first: Visits vertices in order of their distance from a start vertex, nearest first.
- Random walk: Moves from a start vertex to a random out-neighbor again and again, with heavier edges more likely.