Breadth-first

Visits vertices level by level from a start vertex.

Time complexity O(V+E), gograph function traverse.NewBreadthFirstIterator.

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.

Use it in Go

it, err := traverse.NewBreadthFirstIterator(g, "Core")
if err != nil {
	return err
}
for it.HasNext() {
	fmt.Println(it.Next().Label())
}

Example graphs

More in Traversal

  • Depth-first: Follows one path as deep as it can, then continues from the most recently found vertex.
  • 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