The movement of a program through a graph is called traversal. There are several types of traversal.
The first method of traversal is called breadth-first.
- Designate one vertex as the 'root', where the traversal begins
- Visit each vertex connected to the root
- Once all directly connected vertices are visited, visit vertexes one step farther away
Breadth-first using queues
Implemented using the Queue data structure. FIFO structure. Place the first vertex into the queue
Mark it as explored
Repeat
Visit every vertex it is connected to
Add each vertex to the queue
Until all its child vertices have been visited
Repeat
Pop front vertex from queue
Mark the new front vertex as explored
Repeat
Visit every vertex the new front vertex is connected to
Add each vertex to the queue
Until every vertex has been visited
Until queue is empty