GraphTraversing
GraphTraversing
4.3.1 Graph-traversal
Advanced Notes
www.pmt.education
Specification:
www.pmt.education
Graph-Traversal
Graph-traversal is the process of visiting each vertex in a
graph. There are two algorithms in this section -
depth-first and breadth-first graph-traversals. In a
depth-first search, a branch is fully explored before
backtracking, whereas in a breadth-first search a node is
fully explored before venturing on to the next node.
Depth-First Search
Depth-first traversal uses a stack. Depth-first
traversal is used for navigating a maze. The
following example uses a tree, but a depth-first
algorithm can be performed on any connected
graph.
www.pmt.education
Example 1:
Here is a graph. This is a binary-tree.
www.pmt.education
Next, the nodes adjacent to F are observed. These
are B and G. B is higher alphabetically so B is
discovered first.
www.pmt.education
There are no undiscovered nodes adjacent to A. Therefore, A can be
popped off the stack and labelled completely explored, visually indicated by
the purple colour.
www.pmt.education
B has an adjacent undiscovered node, so D is visited.
www.pmt.education
C has no adjacent undiscovered nodes (it is completely explored) so it is
popped off the stack, and the next item in the stack, D, is revisited.
www.pmt.education
E has no undiscovered adjacent node so it is completely explored and can
be removed from the stack. The next item on the stack, D, is revisited.
www.pmt.education
B is completely explored. B is popped off the stack and F is revisited.
www.pmt.education
H is the only undiscovered node adjacent to G.
www.pmt.education
G is completely explored so it is popped from the stack.
www.pmt.education
There are no more items on the stack so the algorithm is complete.
Example 2:
Here is another graph. In this example, the graph is not a binary tree.
www.pmt.education
Any node can be chosen to traverse from. In this example, the start node
will be A.
www.pmt.education
The smallest undiscovered node adjacent to C is F.
www.pmt.education
E has no undiscovered neighbours so it is completely explored. It is popped
off the stack and the next item on the stack is revisited.
www.pmt.education
G is H’s only undiscovered neighbour.
www.pmt.education
H is completely explored.
F is completely explored.
www.pmt.education
C is completely explored.
www.pmt.education
D has no undiscovered neighbours so it is popped from the stack, and the
next item (B) is revisited.
B is completely explored.
www.pmt.education
A is completely explored.
www.pmt.education
Breadth-First Search
Breadth-first traversal uses a queue. This algorithm
will work on any connected graph. Breadth-first
traversal is useful for determining the shortest path
on an unweighted graph.
Example 1:
Here is a graph.
www.pmt.education
The undiscovered nodes adjacent to F are added to the queue and the
result in alphabetical order.
www.pmt.education
Now that F is completely explored, we can move on to the next node. To do
this, we look at the first position of the queue. B is removed from the top of
the queue, so this is the next node to be inspected. The undiscovered
nodes adjacent to B are added to the queue and results - A and D have
been discovered.
www.pmt.education
The next item in the queue is removed and inspected.
G has one adjacent undiscovered node. H is added to the result and to the
queue.
www.pmt.education
G is now completely explored.
www.pmt.education
There are no undiscovered vertices adjacent to A, so it is completely
explored.
www.pmt.education
D has two adjacent undiscovered nodes which are put into the queue and
the result in alphabetical order.
D is completely explored.
www.pmt.education
The next item in the queue is H.
www.pmt.education
C is inspected next.
C is completely explored.
www.pmt.education
Finally, E is at the top of the queue.
E is completely explored.
www.pmt.education
There are no more items in the queue, so the algorithm terminates and the
result is printed.
Example 2:
Here is another graph. In this example, the graph is not a binary tree.
www.pmt.education
Any node can be chosen for graph-traversal. For this example, we will start
with A.
All nodes adjacent to A are placed in the queue as they are discovered in
alphabetical order and are added to the result.
www.pmt.education
The next node is taken from the queue.
The undiscovered node adjacent to B is added to the queue and the result.
www.pmt.education
B is now completely explored.
D is next to be explored.
www.pmt.education
D has no adjacent undiscovered nodes so it is completely explored.
www.pmt.education
E has one adjacent undiscovered vertex - F.
www.pmt.education
C is next to be explored.
www.pmt.education
C is completely explored.
www.pmt.education
F has no adjacent undiscovered nodes so F is completely explored.
www.pmt.education
H’s only undiscovered neighbour is added to the queue and the result.
www.pmt.education
Finally, G is removed from the queue and explored.
www.pmt.education
The queue is empty, so the algorithm terminates.
www.pmt.education