The graph below can be represented by the
ExampleGraph class
listed below it.
The menu shows
breadth-first
and
depth-first search traces. If you haven't yet started the
Graph Search and Creation
module, these traces can be skipped for now.
This section walks through a breadth-first search starting with
vertex A. The queue and breadth-first predecessor
tree are shown after each iteration of the while loop in the
algorithm. The queue is shown with its head to the left and tail to the
right. Also shown is the graph's adjacency list representation.
Queue |
Predecessor Tree |
|
|
A is removed from the queue, and its adjacent vertices are added
to the end of the queue in the order in which they appear on the
adjacency list.
The distance of each adjacent vertex from
A is recorded, and
the vertices are also linked to
A in the predecessor tree:
Queue |
Predecessor Tree |
|
|
B is removed from the queue, and its one open adjacent vertex,
D, is added
to the end of the queue.
D's distance from
A is recorded, and it is linked
to
B in the predecessor tree:
Queue |
Predecessor Tree |
|
|
C is removed from the queue, and its one open adjacent vertex,
E, is added
to the end of the queue.
E's distance from
A is recorded, and it is linked
to
C in the predecessor tree:
Queue |
Predecessor Tree |
|
|
D is removed from the queue, but neither of its adjacent vertices
is still open, so nothing happens:
Queue |
Predecessor Tree |
|
|
E is removed from the queue, but neither of its adjacent vertices
is still open, so nothing happens. Since the queue is empty, the
algorithm halts.
Note that the path from
A to
D discovered by breadth-first
search is
A — B — D, an optimal (shortest) path.
Queue |
Predecessor Tree |
|
|
This section walks through a depth-first search starting with
vertex A. The stack and depth-first predecessor
tree are shown after each iteration of the while loop in the
algorithm. The stack is shown with its top to the left.
Also shown is the graph's adjacency list representation.
Stack |
Predecessor Tree |
|
|
A is popped from the stack, and its adjacent vertices are pushed
in the order in which they appear on the
adjacency list.
Note that since
B precedes
C on the adjacency
list,
B is pushed first.
The distance of each adjacent vertex from
A is recorded, and
the vertices are also linked to
A in the predecessor tree:
Stack |
Predecessor Tree |
|
|
C is popped from the stack, and its one open adjacent
vertex,
E, is pushed.
E's distance from
A is recorded, and it is linked
to
C in the predecessor tree:
Stack |
Predecessor Tree |
|
|
E is popped, and its one open adjacent
vertex,
D, is pushed.
D's distance from
A is recorded, and it is linked
to
E in the predecessor tree:
Stack |
Predecessor Tree |
|
|
D is popped, but neither of its adjacent vertices
is still open, so nothing happens:
Stack |
Predecessor Tree |
|
|
B is popped, but neither of its adjacent vertices
is still open, so nothing happens. Since the stack is empty, the
algorithm halts.
Note that the path from
A to
D discovered by depth-first
search is
A — C — E — D, a non-optimal path.
Stack |
Predecessor Tree |
|
|