22 - Elementary Graph Algorithms
22 - Elementary Graph Algorithms
Sun-Yuan Hsieh
謝孫源 教授
成功大學資訊工程學系
Outline
Graph representation
Breadth-first search
Depth-first search
Topological sort
Strongly connected components
2
Graph representation
3
When expressing the running time of an algorithm, it’s
often in terms of both |V| and |E|.
Example: (V+E).
4
Adjacency lists
Adj
2
1 2 5 /
1
2 1 5 4 3 /
3 3 2 4 /
5 4
4 2 5 3 /
5 4 1 2 /
5
Adjacency lists
Space: (V+E)
6
Example: For a directed graph:
Adj
1 2 1 2 /
2 4 /
3 1 /
3 4 4 4 3 /
7
Adjacency matrix
1 2 3 4 5
1 0 1 0 0 1
2 1 0 1 1 1
3 0 1 0 1 0
4 0 1 1 0 1
5 1 1 0 1 0
1 2 3 4
1 0 1 0 0
2 0 0 0 1
3 1 0 0 0
4 0 0 1 1
Space: (V2)
Time: to list all vertices adjacent to u: (V)
Time: to determine if (u, v) E: (1)
Can store weights instead of bits for weighted graph
8
Breadth-first search
9
Breadth-first search
10
r s t u (b) r s t u
(a)
0 1 0
1
v w x y v w x y
s Q w r
Q
0 1 1
(c) r s t u r s t u
(d)
1 0 2 1 0 2
1 2 2 1 2
v w x y v w x y
Q r t x Q t x v
1 2 2 2 2 2
11
(e) r s t u (f) r s t u
1 0 2 3 1 0 2 3
2 1 2 2 1 2 3
v w x y v w x y
Q x v u Q v u y
2 2 3 2 3 3
(g) r s t u (h) r s t u
1 0 2 3 1 0 2 3
2 1 2 3 2 1 2 3
v w x y v w x y
Q u y y
3 3 3
12
(i) r s t u
1 0 2 3
2 1 2 3
v w x y
Q
13
BFS(V, E, s)
for each u V – {s}
do d[u] ← ∞
d[s] ← 0
Q ←φ
ENQUEUE(Q, s)
While Q ≠ φ
do u ← DEQUEUE(Q)
for each v Adj[u]
do if d[v] = ∞
then d[v] ← d[u] + 1
ENQUEUE(Q, v)
14
Example: directed graph
3
0 f
s 1
c 2
1 g
a 2
e i
h 3
b 3
3
16
Depth-first search
17
Will methodically explore every edge.
Start over from different vertices as necessary.
As soon as we discover a vertex, explore from it.
Unlike BFS, which puts a vertex on a queue so that we explore
from it later.
18
As DFS progresses, every vertex has a color:
WHITE = undiscovered
GRAY = discovered, but not finished (not done exploring from
it)
BLACK = finished (have found everything reachable from it)
19
Discovery time
(a) u v w (b) u v w
1/ 1/ 2/
x y z x y z
(c) u v w (d) u v w
1/ 2/ 1/ 2/
3/ 4/ 3/
x y z x y z
20
(e) u v w (f) u v w
1/ 2/ 1/ 2/
B B
4/ 3/ 4/5 3/
x y z x y z
(g) u v w (h) u v w
1/ 2/ 1/ 2/7
B B
21
(i) u v w (j) u v w
1/ 2/7 1/8 2/7
F B B
F
4/5 3/6 4/5 3/6
x y z x y z
(k) u v w
(l) u v w
1/8 2/7 9/ 1/8 2/7 9/
F B
F B C
4/5 3/6 4/5 3/6
x y z
x y z
22
(m) u v w (n) u v w
1/8 2/7 9/ 1/8 2/7 9/
F B C B C
F
B
4/5 3/6 10/ 4/5 3/6 10/
x y z x y z
(o) u v w (o) u v w
1/8 2/7 9/ 1/8 2/7 9/12
F B C F B C
B B
4/5 3/6 10/11 4/5 3/6 10/11
x y z x y z
23
Pseudocode: Uses a global timestamp time.
DFS(V,E)
for each u V
do color[u] ← WHITE
time ← 0
for each u V
do if color[u] = WHITE
then DFS - Visit(u)
24
DFS - Visit(u)
color[u] ← GRAY discover u
time ← time +1
d[u] ← time
for each v Adj[u] explore (u, v)
do if color[v] = WHITE
then DFS - Visit(v)
color[u] ← BLACK
time ← time +1
f[u] ← time finish u
25
Example: d f
T C
1 12 8 11 13 16
T T C
T
B F
C
2 7 9 10
T
C
T
3 4 5 6 14 15
C C
27
Theorem (Parenthesis theorem)
[Proof omitted.]
For all u, v, exactly one of the following holds:
1. d[u] < f[u] < d[v] < f[v] or d[v] < f[v] < d[u] < f[u] and neither of u and v
is a descendant of the other.
2. d[u] < d[v] < f[v] < f[u] and v is a descendant of u.
3. d[v] < d[u] < f[u] < f[v] and u is a descendant of v.
Corollary
v is a proper descendant of u if and only if
d[u] < d[v] < f[v] < f[u].
28
(a) y z s t
3/6 2/9 1/10 11/16
B F C B
4/5 7/8 12/13 C 14/15
C C u
x w v
(b)
s t
z v u
y w
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
( s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
29
Theorem (White-path theorem)
[Proof omitted.]
v is a descendant of u if and only if at time d[u], there is a
path u → v consisting of only white vertices.
(Except for u, which was just colored gray.)
Classification of edges
Tree edge: in the depth-first forest.
Found by exploring (u, v).
Back edge: (u, v), where u is a descendant of v.
Forward edge: (u, v), where v is a descendant of u, but not a tree
edge.
Cross edge: any other edge. Can go between vertices in the same
depth-first tree or in different depth-first trees.
30
In an undirected graph, there may be some ambiguity
since (u, v) and (v, u) are the same edge. Classify by the
first type above that matches.
31
Topological sort
32
Example: dag of dependencies for putting on goalie equipment:
9/12
17/22 pants sweater
3/4 blocker
33
Topological sort
定義:
A topological sort of a dag G=(V, E) is a linear ordering of all its
vertices. (dag: Directed acyclic graph) 如 edge(u, v), u appears
before v in the ordering
undershirts 11/16 socks 17/18
watch 9/10
12/15
pants shoes 13/14
1/8
shirt
belt tie 2/5
6/7
jacket 3/4
34
Lemma
A directed graph G is acyclic if and only if a DFS
of G yields no back edges. v
Proof ( ): Show that back edge cycle. T
35
( ) :
Show that cycle back edge.
Suppose G contains cycle c.
At time d[v], vertices of c form a white path v → u
(since v is the first vertex discovered in c).
By white-path theorem, u is descendant of v in
depth-first forest.
Therefore, (u, v) is a back edge.
36
Topological sort of a dag: a linear ordering of vertices
such that if (u, v)∈E, then u appears somewhere before v.
(Not like sorting numbers.)
TOPOLOGICAL-SORT(V, E)
call DFS(V, E) to compute finishing times f[v] for all v V
output vertices in order of decreasing finish times
37
Don’t need to sort by finish times.
Can just output vertices as they’re finished and understand that
we want the reverse of this list.
Or put them onto the front of a linked list as they’re finished.
When done, the list contains vertices in topological sorted
order.
Time: (V+E)
38
Order:
26 socks
27 shorts
28 hose
29 pants
21 skates
20 leg pads
14 t-shirt
13 chest pad
12 sweater
11 mask
6 batting glove
5 catch glove
4 blocker
39
Correctness: Just need show if (u, v) E, then f[v] < f[u].
When we explore (u, v), what are the colors of u and v?
u is gray.
Is v gray, too?
No, because then v would be ancestor of u.
40
Is v white?
Then becomes descendant of u.
41
Strongly connected components
42
Algorithm uses GT = transpose of G.
GT = (V, ET), ET = {(u, v) : (v, u) E}.
GT is G with all edges reversed.
Can create GT in (V + E) time if using adjacency lists.
43
Component graph
GSCC = (VSCC, ESCC).
VSCC has one vertex for each SCC in G.
ESCC has an edge if there’s an edge between the corresponding
SCC’s in G.
14/19 15/16 3/4 1/12 6/9
44
Lemma
GSCC is a dag. More formally, let C and C’ be distinct
SCC’s in G, let u, v C, u’, v’ C’, and suppose there is
a path u → u’ in G. Then there cannot also be a path v’→
v in G.
Proof Suppose there is a path v’→ v in G. Then there are
paths u → u’ → v’ and v’ → v → u in G. Therefore,
u and v’ are reachable from each other, so they are
not in separate SCC’s.
45
SCC(G)
call DFS(G) to compute finishing times f[u] for all u
compute GT
call DFS(GT), but in the main loop, consider vertices in order of
decreasing f[u]
(as computed in first DFS)
output the vertices in each tree of the depth-first forest formed in
second DFS
as a separate SCC
46
Example:
1. Do DFS 14/19 15/16 3/4 1/12 6/9
2. GT 10/11 7/8
17/18 13/20 2/5
3. DFS (roots blackened)
B B F
17/
17/18 13/
13/20 2/5
2/ 10/11
10/ 7/8
7/
B C C
3/4
3/ 6/7
6/ 10/13
10/ 9/14
9/ 17/20
17/
2/5
2/ 1/8
1/ 11/12
11/ 15/16
15/ 18/19
18/
48
How can this possibly work?
Idea: By considering vertices in second DFS in
decreasing order of finishing times from first
DFS, we are visiting vertices of the component
graph in topological sort order.
49
Lemma
Let C and C’ be distinct SCC’s in G = (V, E). Suppose
there is an edge (u, v) E such that u C and v C’.
C u v C’
50
Proof
Two cases, depending on which SCC had the first
discovered vertex during first DFS.
If d(C) < d(C’), let x be the first vertex discovered in C. At
time d[x], all vertices in C and C’ are white. Thus, there exist
paths of white vertices from x to all vertices in C and C’.
By the white-path theorem, all vertices in C and C’ are
descendants of x in depth-first tree.
By the parenthesis theorem, f[x] = f(C) > f(C’).
51
If d(C) > d(C’), let y be the first vertex discovered in C’. At
time d[y], all vertices in C’ are white and there is a white path
from y to each vertex in C’ all vertices in C’ become
descendants of y.
Again, f[y] = f(C’).
At time d[y], all vertices in C are white.
By earlier lemma, since there is an edge (u, v), we cannot have
a path from C’ to C.
So no vertex in C is reachable from y.
Therefore, at time f[y], all vertices in C are still white.
Therefore, for all w C, f[w] > f[y], which implies that f(C) >
f(C’).
52
Corollary
Let C and C’ be distinct SCC’s in G = (V, E). Suppose
there is an edge (u, v) ET, where u C and v C’.
Then f(C) < f(C’).
Proof (u, v) ET (v, u) E. Since SCC’s of G
and GT are the same, f(C’) > f(C).
Corollary
Let C and C’ be distinct SCC’s in G = (V, E), and
suppose that f(C) > f(C’).
Then there cannot be an edge from C to C’ in GT.
Proof It’s the contrapositive of the previous corollary.
53
Why the SCC procedure works:
54
The next root chosen in the second DFS is in SCC C’ such that f(C’)
is maximum over all SCC’s other than C. DFS visits all vertices in
C’, but the only edges out of C’ go to C, which we’ve already
visited.
Each time we choose a root for the second DFS, it can reach only
vertices in its SCC—get tree edges to these,
vertices in SCC’s already visited in second DFS—get no tree edges
to these.
55