Basic Traversal Algorithm |
|||
Set a flag in all vertices to FALSE, meaning "not visited" | |||
Pick a starting vertex | |||
Mark vertex Vstart as visited | |||
Make a list of all vertices adjacent to vertex Vstart | |||
Loop | |||
Select a vertex, Vi from the list. Remove the vertex from the list. Visit the vertex. Mark vertex Vi as "visited". Add to the list any vertices adjacent to Vi which have not been visited. |
|||
Repeat until the list is empty |
Stack
Order of Traversal: 1 - 4 - 9 - 8 - 3 - 7 - 2 - 6 - 5 Note that each path down from node 1 is followed as far as possible before backing up to the last "branch" to continue down another path. This is known as... Depth First Traversal |
Queue
Order of Traversal: 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 Note that starting from node 1 visits all nodes one edge away are visited first, then all nodes two edges away are visited an so on. This is known as... Breadth First Traversal |