0% found this document useful (0 votes)
65 views22 pages

COMP20007 Design of Algorithms: Graph Traversal

The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It explains that BFS explores all neighbors of a starting node before moving to their neighbors, while DFS fully explores one branch before backtracking. Pseudocode and examples are provided to illustrate how each algorithm traverses a graph and maintains a queue or stack. Applications of DFS like checking for cycles or connectivity are also mentioned.

Uploaded by

李宇皓
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views22 pages

COMP20007 Design of Algorithms: Graph Traversal

The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It explains that BFS explores all neighbors of a starting node before moving to their neighbors, while DFS fully explores one branch before backtracking. Pseudocode and examples are provided to illustrate how each algorithm traverses a graph and maintains a queue or stack. Applications of DFS like checking for cycles or connectivity are also mentioned.

Uploaded by

李宇皓
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

COMP20007 Design of Algorithms

Graph Traversal

Lars Kulik

Lecture 7

Semester 1, 2022

1
Breadth-First and Depth-First Traversal

There are two natural approaches to the traversal of a graph.

Suppose we have a graph and we want to explore all its nodes


systematically. Suppose we start from node v and v has
neighbouring nodes x, y and z.

In a breadth-first approach we, roughly, explore x, y and z before


exploring any of their neighboring nodes.

In a depth-first approach, we may explore, say, x first, but then,


before exploring y and z, we first explore one of x’s neighbours,
then one of its neighbours, and so on.

(This is really hard to express in English—we do need


pseudo-code!)
2
Depth-First Search

Both graph traversal methods rely on


marking nodes as they are visited—so
a c that we can avoid revisiting nodes.

f Depth-first search is based on


d
backtracking.
b e g
Neighbouring nodes are considered in,
say, alphabetical order.

For the example graph, nodes are


visited in the order a, b, d, e, c, f , g .

3
Depth-First Search: The Traversal Stack

a c
DFS corresponds to using a
f stack discipline for keeping
d track of where we are in the
b e g overall process.

Here is how the “where-we-came-from” stack develops for the


example:
e
d d d
b b b b b c g
a a a a a a a a a f f f
− − − − − − − − − − − − −

4
Depth-First Search: The Traversal Stack

Levitin uses a more compact notation


for the stack’s history. Here is how the
stack develops, in Levitin’s notation:
a c
e4,1
f d3,2
d
b2,3 c5,4 g7,6
b e g
a1,5 f6,7

The first subscripts give the order in


which nodes are pushed, the second the
order in which they are popped off the
stack.

5
Depth-First Search: The Depth-First Search Forest

Another useful tool for depicting a DF


traversal is the DFS tree (for a
a c connected graph).

More generally, we get a DFS forest:


f
d
a f
b e g tree edge →
b c g

back edge → d

6
Depth-First Search: The Algorithm
function DFS(hV , E i)
mark each node in V with 0
count ← 0
for each v in V do
if v is marked 0 then
DfsExplore(v )

function DfsExplore(v )
count ← count + 1
mark v with count
for each edge (v , w ) do ⊲ w is v ’s neighbour
if w is marked with 0 then
DfsExplore(w )

This works both for directed and undirected graphs. 7


Depth-First Search: The Algorithm

The “marking” of nodes is usually done by maintaining a separate


array, mark, indexed by V .

For example, when we wrote “mark v with count”, that would be


implemented as “mark[v] := count”.

How to find the nodes adjacent to v depends on the graph


representation used.

Using an adjacency matrix adj, we need to consider adj[v,w] for


each w in V . Here the complexity of graph traversal is Θ(|V |2 ).

Using adjacency lists, for each v , we traverse the list adj[v].


In this case, the complexity of traversal is Θ(|V | + |E |). Why? ✎
8
Applications of Depth-First Search

It is easy to adapt the DFS algorithm so that it can decide whether


a graph is connected.

How? ✎

9
Applications of Depth-First Search

It is easy to adapt the DFS algorithm so that it can decide whether


a graph is connected.

How? ✎
It is also easy to adapt it so that it can decide whether a graph has
a cycle.

How? ✎

9
Applications of Depth-First Search

It is easy to adapt the DFS algorithm so that it can decide whether


a graph is connected.

How? ✎
It is also easy to adapt it so that it can decide whether a graph has
a cycle.

How? ✎
In terms of DFS forests, how can we tell if we have traversed a
dag?

9
Breadth-First Search

Breadth-first search proceeds in a


concentric manner, visiting all nodes
a c that are one step away from the start
node, then all those that are two steps
f away (except those that were already
d
visited), and so on.
b e g
Again, neighbouring nodes are
considered in, say, alphabetical order.

For the example graph, nodes are


visited in the order a, b, c, d, e, f , g .

10
Depth-First Search vs Breadth-First

Typical depth-first search: Typical breadth-first search:

a a
• • • • • • • • • •

• • • • • •

• • • • • • • •

• • • •
• • • •
• • • • • •

• • • • • • • • • •

11
Breadth-First Search: The Traversal Queue

BFS uses a queue discipline for keeping


track of pending tasks.

How the queue develops for the


a c example:

a1
f
d b2 c3 d4 e5
e g c3 d4 e5
b
d4 e5 f6
e5 f6
f6
g7

The subscript again is Levitin’s; it gives


the order in which nodes are processed. 12
The Breadth-First Search Forest

Here is the BFS tree for the example:

a c a
tree edge →
f b c d e
d

b e g ր f
cross edge
g

In general, we may get a BFS forest.

13
Breadth-First Search: The Algorithm
function BFS(hV , E i)
mark each node in V with 0
count ← 0, init(queue) ⊲ create an empty queue
for each v in V do
if v is marked 0 then
count ← count + 1
mark v with count
inject(queue, v ) ⊲ queue containing just v
while queue is non-empty do
u ← eject(queue) ⊲ dequeues u
for each edge (u, w ) adjacent to u do
if w is marked with 0 then
count ← count + 1
mark w with count
inject(queue, w ) ⊲ enqueues w

14
Breadth-First Search: The Algorithm

BFS has the same complexity as DFS.

Again, the same algorithm works for directed graphs as well.

Certain problems are most easily solved by adapting BFS.

For example, given a graph and two nodes, a and b in the graph,
how would you find the fewest number of edges between two given
vertices a and b?

15
Topological Sorting

We mentioned scheduling problems and their representation by


directed graphs.

Assume a directed edge from a to b means that task a must be


completed before b can be started.

Then the graph has to be a dag.

Assume the tasks are carried out by a single person, unable to


multi-task.

Then we should try to linearize the graph, that is, order the nodes
in a sequence v1 , v2 , . . . , vn such that for each edge (vi , vj ) ∈ E , we
have i < j.

16
Topological Sorting: Example

There are four different ways to linearize the following graph.

a c e

b d f

Here is one:

b a d c e f

17
Topological Sorting Algorithm 1

We can solve the top-sort problem with depth-first search:

1. Perform DFS and note the order in which nodes are popped
off the stack.
2. List the nodes in the reverse of that order.

This works because of the stack discipline.

If (u, v ) is an edge then it is possible (for some way of deciding


ties) to arrive at a DFS stack with u sitting below v .

Taking the “reverse popping order” ensures that u is listed before


v.

18
Topological Sorting Example Again

Using the DFS method and resolving ties by using alphabetical


order, the graph gives rise to the traversal stack shown on the right
(the popping order shown in red):

a c e
e3,1 f4,2
c2,3 d6,5
a1,4 b5,6
b d f

Taking the nodes in reverse popping order yields b, d, a, c, f , e.

19
Topological Sorting Algorithm 2

An alternative method would be to repeatedly select a random


source in the graph (that is, a node with no incoming edges), list
it, and remove it from the graph.

This is a very natural approach, but it has the drawback that we


repeatedly need to scan the graph for a source.

However, it exemplifies the general principle of


decrease-and-conquer.

20

You might also like