Lecture 2 - Graph Traversal
Lecture 2 - Graph Traversal
b e
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
a 0 d {a, b, c} a
1
c
1
b e
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
a 0 d {a, b, c} a
{a, b, c, f} b
1
c
1
b e
2
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 d {a, b, c} a
a
1 {a, b, c, f} b
{a, b, c, f, e} c
c
1 2
b e
2
i
f
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2
b e
2
3 i
f 3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
3 d {a, b, c} a
a 0
1 {a, b, c, f} b
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2 {a, b, c, f, e, g, h, d, i} e
b e
2 All distances
i are filled in
3 f 3 after
3 processing e
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
3 d {a, b, c} a
a 0
1 {a, b, c, f} b
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
2 {a, b, c, f, e, g, h, d, i} g
1
b e
2
3 i
3 f
3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 3 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 {a, b, c, f, e, g, h, d, i} h
2
b e
2
3 i
3 f
3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
3 {a} --
a 0 d {a, b, c} a
{a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
{a, b, c, f, e, g, h, d, i} d
1 2
b e
2
f 3 i
3 3
g h
Breadth-first search : Example
FIFO Just after
Queue Q Processing vertex
s
{a} --
0 3 d {a, b, c} a
a {a, b, c, f} b
1
{a, b, c, f, e} c
c {a, b, c, f, e, g, h} f
1 2 {a, b, c, f, e, g, h, d, i} i
b e
2 Algorithm terminates :
All vertices are processed.
3 i
3 f
3
g h
Breadth-first Search: Analysis
• Running time: O(V+E) = considered linear
time in graphs
– Initialization: O(V)
– Queue operations: O(V)
• Each vertex enqueued and dequeued at most once
• Both enqueue and dequeue operations take O(1) time
– Processing gray vertices: O(E)
• Each vertex is processed at most once and
σ𝒖∈𝑽 𝑨𝒅𝒋 𝒖 = 𝑶(𝑬)
Breadth-first search: The paths to the
root
• BFT(G,s), where VH={v∈V:H(v) ≠NIL} ∪{s}
and EH={(H[v], v) ∈ E: v ∈VH – {s}}
is a breadth first tree such that
– VH consists of all vertices in V that are reachable
from s
– For all v ∈ VH, unique path p(v,s) in GH constitutes
a sp(s,v) in G
Breadth-first Tree: The BFS tree
s
a 0 3d
s 1
3 d c
0 1
a 2
1 b e
c 2
i
1 2 f 3
3 3
b e g h
2
3 i
3 f
3
g h
Depth-first search: Idea
• Graph G=(V,E) directed or undirected
• Adjacency list representation
• Goal: Systematically explore every vertex and
every edge
• Idea: Search deeper whenever possible
– Using a LIFO queue (Stack; FIFO queue used in
BFS)
Depth-first search: Key components
• Maintains several field for each v ∈ V
• Like BFS, colors the vertices to indicate their
states. Each vertex is
– Initially white
– Grayed when discovered
– Blacked when finished
• Like BFS, records discovery of a white v
during scanning Adj[u] by H[v] u
Depth-first search: Key components
• Unlike BFS, predecessor graph GH produced
by DFS forms spanning forest
• GH= (V,EH) where
EH={(H[v],v): v ∈ V and H[v] != NIL}
• GH = depth-first forest(DFF) is composed of
disjoint depth-first trees (DFTs)
Depth-first search: Key components
• DFS also timestamps each vertex with two
timestamps
• d[v] : records when v is first discovered and
grayed
• f[v]: records when v is finished and blackened
• Since there is only one discovery event and
finishing event for each vertex we have
1<=d[v] <f[v]<=2|V|
Depth-first search: Algorithm
DFS-VISIT(G, u)
DFS(G) color[u] gray
for each u ∈ V do
d[u] time time+1
color[u] white for each v ∈ Adj[u] do
H[u] NIL if color[v]= white then
time 0 H[v] u
for each u ∈ V do DFS-VISIT(G,v)
if color[u]= white
then color[u] black
DFS-VISIT(G,u) f[u] time time +1
Depth-first search: Analysis
• Running time: O(V+E)
• Initialization loop in DFS: O(V)
• Main loop in DFS: O(V) exclusive of time to execute
calls to DFS-VISIT
• DFS-VISIT is called exactly once for each v ∈ V since
– DFS-VISIT is invoked only on white vertices and
– DFS-VISIT(G,u) immediately colors u as gray
• For loop of DFS-VISIT(G, u) is executed |Adj[u]|
time
• Since sum |Adj[u]| = E, total cost of executing loop of
DFS-VISIT is O(E).
Depth-first search: Example
x y z
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
3 4
w v u
Depth-first search: Example
x y z
1
s t
3 4 5
w v u
Depth-first search: Example
x y z
1
s t
3 4 5 6
w v u
Depth-first search: Example
x y z
1
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: Example
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: DFT and DFF
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u
y z
1 12 8 11 13 16
x t
s
2 7 9 10
3 4 5 6 14 15
w v u
Topological Sort
• In college, before taking a particular course, students usually
must take all applicable prerequisite courses.
• For example, before taking the Programming II course, students
must take the Programming I course.
• However, certain courses can be taken independently of each
other.
• The courses within a department can be represented as a directed
graph.
• A directed edge from, say, vertex u to vertex v means that the
course represented by the vertex u is a prerequisite of the course
represented by the vertex v.
• It would be helpful for students to know, before starting a major,
the sequence in which they can take courses so that before
taking a particular course they take all its prerequisite courses.
• This section describes an algorithm that can be used to output
the vertices of a directed graph in such a sequence.
Example
• Problem: Find
322
an order in 143
321
which all these
courses can be
taken 142
326
370
• Example: 142 341
401
Topological sort
• Topological sort (of directed acyclic graph): It
is linear ordering of all the vertices such that if
(u,v) is a directed edge, then u appears before
v in the ordering.
– We assume that the graph has no cycles.
Valid order
C
A
A B E C D F
Topological sort : Algorithm 1
• Step 1: Identify vertices that have no incoming
edges
– The “In-degree” of these vertices is zero.
B
C
A
D E
Topological sort : Algorithm 1
• Step 1: Identify vertices that have no incoming
edges
– If no such edges, graph has cycles (cyclic graph).
B
C
A
Example of a cyclic
graph: No vertex of
In-degree 0
D
Topological sort : Algorithm 1
• Step 2: Delete this vertex of in-degree 0 and all
its outgoing edges from the graph. Place it in
the output.
B
C
A
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A B
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A B F
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
A B F C D E
Summary of Topological Sort
Algorithm B
• Problem:
– Need a faster way to find vertices with in-degree 0
instead of searching through entire in-degree array
Topological sort: Improvement
• Key Idea: Initialize and maintain a queue (or
stack) of vertices with In-degree 0
Queue A F
C
A
F
In-Degree
D E array
Topological sort: Improvement
After each vertex is output, when updating In-degree
array, enqueue any vertex whose In-degree has become
zero.
Queue F B
Dequeue Enqueue
Output A
B
C
A
F
In-Degree
D E array
Topological sort Algorithm:
Improvement
• Store each vertex’s In-degree in an array
• Initialize a queue with all In-degree zero
vertices
• While there are vertices remaining in the
queue:
– Dequeue and output a vertex
– Reduce In-degree of all vertices adjacent to it by 1
– Enqueue any of these vertices whose In-degree
became zero.
Topological Sort Algorithm 1:
Analysis
• For input graph G(V,E), Run time =??
• Break down into total time required to:
– Initialize In-degree array: O(|E|)
– Initialize Queue with In-degree 0 vertices: O(|V|)
– Dequeue and output vertex:
• |V| vertices, each takes only O(1) to dequeue and output.
• So total time = O(|V|)
– Reduce In-degree of all vertices adjacent to a vertex
and Enqueue any In-Degree 0 vertices: O(|E|)
– Total time =O(|V| + |E|) Linear running time!
Topological sort: Algorithm 2
• The algorithm
– Run DFS(G)
– When a vertex is finished, output it
– Vertices are output in the reverse topologically
sorted order
• Runs in O(V+E) time – a linear time
algorithm