0% found this document useful (0 votes)
24 views

22 - Elementary Graph Algorithms

Depth-first search (DFS) methodically explores every edge of a graph by recursively exploring unseen vertices connected to previously found vertices. As it explores, it assigns each vertex a color of white, gray, or black to track its discovery and finish times. DFS numbers the vertices from 1 to 2|V| based on these times to provide an ordering useful for other graph algorithms.

Uploaded by

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

22 - Elementary Graph Algorithms

Depth-first search (DFS) methodically explores every edge of a graph by recursively exploring unseen vertices connected to previously found vertices. As it explores, it assigns each vertex a color of white, gray, or black to track its discovery and finish times. DFS numbers the vertices from 1 to 2|V| based on these times to provide an ordering useful for other graph algorithms.

Uploaded by

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

Chapter 22

Elementary Graph Algorithms

Sun-Yuan Hsieh
謝孫源 教授
成功大學資訊工程學系
Outline

Graph representation
Breadth-first search
Depth-first search
Topological sort
Strongly connected components

2
Graph representation

Given graph G = (V, E).


May be either directed or undirected.
Two common ways to represent for algorithms:
1. Adjacency lists.
2. Adjacency matrix.

3
When expressing the running time of an algorithm, it’s
often in terms of both |V| and |E|.

In asymptotic notation  and only in asymptotic notation 


we’ll drop the cardinality.

Example: (V+E).

4
Adjacency lists

Array Adj of |V| lists, one per vertex.


Vertex u’s list has all vertices v such that (u, v)  E.
(Works for both directed and undirected graphs.)
Example: For an undirected graph:

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

If edges have weights, can put the weights in the lists.


Weight: w : E  R

Space: (V+E)

Time: to list all vertices adjacent to u: (degree(u))


Time: to determine if (u, v)  E: (degree(u))

6
Example: For a directed graph:

Adj
1 2 1 2 /
2 4 /
3 1 /
3 4 4 4 3 /

Same asymptotic space and time

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

Input: Graph G = (V,E), either directed or undirected, and


source vertex s  V

Output: d[v] = distance (smallest # of edges) from s to v,


for all v  V

Also [v] = u such that (u, v) is last edge on a shortest path


s→v
• u is v’s predecessor
• set of edges {([v], v) : v ≠ s} forms a tree

9
Breadth-first search

Idea: Send a wave out from s.


• First hits all vertices 1 edge from s.
• From there, hits all vertices 2 edges from s.
• Etc.

Use FIFO queue Q to maintain wavefront.


• v  Q if and only if wave has hit v but has not come out of v
yet.

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

Can show that Q consists of vertices with d


values.
i i i …. i i+1 i+1 … i+1
• Only 1 or 2 values.
• If 2, differ by 1 and all smallest are first. 15
Since each vertex gets a finite d value at most once, values
assigned to vertices are monotonically increasing over time.

BFS may not reach all vertices.


Time = O(V + E).
• O(V) because every vertex enqueued at most once.
• O(E) because every vertex dequeued at most once and we examine
(u, v) only when u is dequeued. Therefore, every edge examined at
most once if directed, at most twice if undirected.

16
Depth-first search

Input: G = (V, E), directed or undirected. No source vertex


given!
Output: 2 timestamps on each vertex:
d[v] = discovery time
f [v] = finishing time
These will be useful for other algorithms later on.
Can also compute [v].

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)

Discovery and finish times:


Unique integers from 1 to 2|V|.
For all v, d[v] < f[v].
In other words, 1 ≤ d[v] < f[v] ≤ 2|V|.

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

4/5 3/6 4/5 3/6


x y z x y z

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

Time = (V + E).


• Similar to BFS analysis.
• , not just O, since guaranteed to examine every
vertex and edge.
26
DFS forms a depth-first forest comprised of  1 depth-first trees.

Each tree is made of edges (u, v) such that


u is gray and v is white when (u, v) is explored.

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.

So d[u] < d[v] < f[u] < f[v] cannot happen.


Like parentheses:
OK: ()[] ([]) [()]
Not OK: ([)] [(])

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.

Theorem [Proof omitted.]


In DFS of an undirected graph, we get only tree and
back edges. No forward or cross edges.

31
Topological sort

Directed acyclic graph (dag)


A directed graph with no cycles.
Good for modeling processes and structures that have a
partial order:
a > b and b > c a > c.
But may have a and b such that neither a > b nor b > a.

Can always make a total order


(either a > b or b > a for all a ≠ b) from
a partial order. In fact, that’s what a topological sort will do.

32
Example: dag of dependencies for putting on goalie equipment:

25/26 socks 15/24 shorts 7/14 T-shirt 1/6 batting glove

16/23 hose 8/13 chest pad

9/12
17/22 pants sweater

18/21 skates 10/11 mask

19/20 leg pads 2/5 catch glove

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

socks undershirts pants shoes watch shirt belt tie jacket

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

Suppose there is a back edge (u, v).


B T
Then v is ancestor of u in depth-first forest.
Therefore, there is a path v → u, so v → u → v is a 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.

 (u, v) is a back edge.


 contradiction of previous lemma (dag has no back
edges).

40
 Is v white?
 Then becomes descendant of u.

By parenthesis theorem, d[u] < d[v] < f[v] < f[u].


 Is v black?
 Then v is already finished.

Since we’re exploring (u, v), we have not yet finished u.


Therefore, f[v] < f[u].

41
Strongly connected components

Given directed graph G = (V,E).


A strongly connected component (SCC) of G is a
maximal set of vertices C  V such that for all u, v  C,
both u → v and v → u.
Example: [Just show SCC’s at first. Do DFS a little later.]

14/19 15/16 3/4 1/12 6/9

17/18 13/20 2/5 10/11 7/8

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.

Observation: G and GT have the same SCC’s.


(u and v are reachable from each other in G if and only
if reachable from each other in GT.)

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

For our example: 7/8


17/18 13/20 2/5 10/11

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)

Time: (V + E).


47
C B
14/19
14/ 15/16
15/ 3/4
3/ 1/12
1/ 6/9
6/

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.

To prove that it works, first deal with 2 notational issues:


Will be discussing d[u] and f[u]. These always refer to first
DFS. 
Extend notation for d and f to sets of vertices U V :
d(U) = minuU {d[u]} (earliest discovery time)
f(U) = maxuU {f[u]} (latest finishing time)

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’

Then f(C) > f(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:

 When we do the second DFS, on GT, start with SCC C


such that f(C) is maximum.
• The second DFS starts from some x  C, and it
visits all vertices in C.
• Corollary says that since f(C) > f(C’) for all C’ ≠ C,
there are no edges from C to C’ in GT.
Therefore, DFS will visit only vertices in C.

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.

We are visiting vertices of (GT)SCC in reverse of


topologically sorted order.

55

You might also like