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.

Office network

Switches and rooms in a four-team office, wired through a core switch.

An undirected graph with 13 vertices and 16 edges.

Find every switch and room the core switch can reach, the ones fewest hops away first.

From Core, breadth-first visits 13 vertices: Core, Sales, Support, Ops, Dev, Uplink, Sales 1, Lab, Support 1, Shared, and so on to Dev 2.

Step by step

  1. Start at Core. Put it in the queue.
  2. Take Core from the front of the queue.
  3. Add Sales, Support, Ops and Dev, the unseen neighbors of Core, to the back of the queue.
  4. Take Sales from the front of the queue.
  5. Add Uplink, Sales 1 and Lab, the unseen neighbors of Sales, to the back of the queue.
  6. Take Support from the front of the queue.

21 more steps lead to the last one:

  1. Done. Breadth-first reached 13 vertices from Core.

Use it in Go

for v, depth := range traverse.BFSWithDepth(g, "Core") {
	fmt.Println(v.Label(), depth)
}

paths, err := path.BFSPaths(g, "Core")
if err != nil {
	return err
}
route, _ := paths.PathTo("Uplink")
for _, v := range route {
	fmt.Println(v.Label())
}

Example graphs

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.

All algorithms