Document 7405486

Download Report

Transcript Document 7405486

COSC 3101A - Design and
Analysis of Algorithms
10
BFS, DFS
Topological Sort
Strongly Connected Components
Many of these slides are taken from Monica Nicolescu, Univ. of Nevada, Reno, [email protected]
Searching in a Graph
• Graph searching = systematically follow the
edges of the graph so as to visit the vertices of
the graph
• Two basic graph searching algorithms:
– Breadth-first search
– Depth-first search
– The difference between them is in the order in which
they explore the unvisited edges of the graph
• Graph algorithms are typically elaborations of
the basic graph-searching algorithms
7/6/2004 Lecture 10
COSC3101A
2
Breadth-First Search (BFS)
• Input:
– A graph G = (V, E) (directed or undirected)
– A source vertex s  V
• Goal:
– Explore the edges of G to “discover” every vertex
reachable from s, taking the ones closest to s first
• Output:
– d[v] = distance (smallest # of edges) from s to v, for
all v  V
– A “breadth-first tree” rooted at s that contains all
reachable vertices
7/6/2004 Lecture 10
COSC3101A
3
Breadth-First Search (cont.)
• Discover vertices in increasing order of distance
from the source s – search in breadth not depth
– Find all vertices at 1 edge from s, then all vertices at 2
edges from s, and so on
2
1
3
5
4
11
6
7
12
9
7
7/6/2004 Lecture 10
COSC3101A
4
Breadth-First Search (cont.)
• Keeping track of progress:
– Color each vertex in either white,
gray or black
source
1
2
3
5
4
– When being discovered a vertex
becomes gray
1
2
– After discovering all its adjacent
vertices the node becomes black
5
4
1
2
– Initially, all vertices are white
– Use FIFO queue Q to maintain the
set of gray vertices
7/6/2004 Lecture 10
COSC3101A
3
3
5
4
5
Breadth-First Tree
• BFS constructs a breadth-first tree
– Initially contains the root (source vertex s)
– When vertex v is discovered while scanning
source
the adjacency list of a vertex u  vertex v
and edge (u, v) are added to the tree
– u is the predecessor (parent) of v in the
1
2
3
5
4
breadth-first tree
– A vertex is discovered only once  it has at
most one parent
7/6/2004 Lecture 10
COSC3101A
6
BFS Additional Data Structures
• G = (V, E) represented using adjacency lists
• color[u] – the color of the vertex for all u  V
• [u] – predecessor of u
– If u = s (root) or node u has not yet been
1
source
d=1
=1
2
3
discovered  [u] = NIL
• d[u] – the distance from the source s to
vertex u
5
4
d=1
=1
d=2
=5
d=2
=2
• Use a FIFO queue Q to maintain the set of
gray vertices
7/6/2004 Lecture 10
COSC3101A
7
BFS(G, s)
1. for each u  V[G] - {s}
2.
r
s
t
u
v
r
w
s
x
t
y
u


do color[u]  WHITE
3.
d[u] ← 
4.
[u] =
NIL

5. color[s]  GRAY
6. d[s] ← 0
7. [s] = NIL
8. Q  
9. Q ← ENQUEUE(Q, s)
7/6/2004 Lecture 10
COSC3101A

v
r

w
s

x
t

y
u

0




v
w
Q: s

x

y
8
BFS(G, s)
10. while Q  
11.
do u ← DEQUEUE(Q)
12.
for each v  Adj[u]
do if color[v] = WHITE
13.
14.
then color[v] ← GRAY
15.
d[v] ← d[u] + 1
16.
17.
18.
s
t
u

0


Q: s

v
r

w
s

x
t

y
u

0


Q: w

v
1
w

x

y
[v] = u
r
s
t
u
ENQUEUE(Q, v)
1
0


Q: w, r

v
color[u]  BLACK
7/6/2004 Lecture 10
r
COSC3101A
1
w

x

y
9
Example
r
s
t
u
r
s
t
u
r
s
t
u

0


1
0


1
0
2



v
w
Q: s
r
s

x

y

x

y

y
u
t
u

1
v
w
Q: r, t, x
r
s
2
x
t

1
v
w
Q: w, r
r
s
t
u
1
2

1
2
3
0
2
3
2
x

y
2
x
3
y
t
u
2
1
v
w
Q: v, u, y
r
s
t
u
2
3
1
0
2
3
3
y
2
1
v
w
Q: 
2
x
3
y
0
0
2
1
v
w
Q: t, x, v
r
s
2
x

y
t
u
2
1
v
w
Q: x, v, u
r
s
1
2
3
1
3
y
2
1
2
v
w
x
Q: y COSC3101A
0
2
1
2
v
w
x
Q:
u, y Lecture 10
7/6/2004
0
1
10
Analysis of BFS
1. for each u  V - {s}
2.
do color[u]  WHITE
3.
d[u] ← 
4.
[u] = NIL
O(V)
5. color[s]  GRAY
6. d[s] ← 0
7. [s] = NIL
(1)
8. Q  
9. Q ← ENQUEUE(Q, s)
7/6/2004 Lecture 10
COSC3101A
11
Analysis of BFS
10. while Q  
11.
do u ← DEQUEUE(Q)
12.
for each v  Adj[u]
do if color[v] = WHITE
13.
14.
then color[v] = GRAY
15.
d[v] ← d[u] + 1
16.
[v] = u
17.
ENQUEUE(Q, v)
18.
(1)
Scan Adj[u] for all vertices
in the graph
• Each vertex is scanned only
once, when the vertex is
dequeued
• Sum of lengths of all
adjacency lists = (E)
• Scanning operations:
O(E)
(1)
color[u]  BLACK
• Total running time for BFS = O(V + E)
7/6/2004 Lecture 10
COSC3101A
12
Shortest Paths Property
• BFS finds the shortest-path distance from the
source vertex s  V to each node in the graph
• Shortest-path distance = (s, u)
– Minimum number of edges in any path from s to u
source
s
t
u
1
0
2
3
2
1
2
3
x
y
r
v
7/6/2004 Lecture 10
w
COSC3101A
13
Depth-First Search
• Input:
– G = (V, E) (No source vertex given!)
• Goal:
1
2
3
5
4
– Explore the edges of G to “discover” every vertex in V
starting at the most current visited node
– Search may be repeated from multiple sources
• Output:
– 2 timestamps on each vertex:
• d[v] = discovery time
• f[v] = finishing time (done with examining v’s adjacency list)
– Depth-first forest
7/6/2004 Lecture 10
COSC3101A
14
Depth-First Search
• Search “deeper” in the graph whenever
possible
• Edges are explored out of the most recently
discovered vertex v that still has unexplored
edges
1
2
3
5
4
• After all edges of v have been explored, the search
“backtracks” from the parent of v
• The process continues until all vertices reachable from the
original source have been discovered
• If undiscovered vertices remain, choose one of them as a
new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
7/6/2004 Lecture 10
COSC3101A
15
DFS Additional Data Structures
• Global variable: time-step
– Incremented when nodes are discovered/finished
• color[u] – similar to BFS
– White before discovery, gray while processing and
black when finished processing
• [u] – predecessor of u
• d[u], f[u] – discovery and finish times
1 ≤ d[u] < f [u] ≤ 2 |V|
WHITE
0
7/6/2004 Lecture 10
BLACK
GRAY
d[u]
f[u]
COSC3101A
2V
16
DFS(G)
1. for each u  V[G]
2.
do color[u] ← WHITE
3.
[u] ← NIL
4. time ← 0
5. for each u  V[G]
6.
do if color[u] = WHITE
7.
then DFS-VISIT(u)
•
u
v
w
x
y
z
Every time DFS-VISIT(u) is called, u becomes the
root of a new tree in the depth-first forest
7/6/2004 Lecture 10
COSC3101A
17
DFS-VISIT(u)
1. color[u] ← GRAY
2. time ← time+1
3. d[u] ← time
4. for each v  Adj[u]
5.
do if color[v] = WHITE
6.
then [v] ← u
7.
DFS-VISIT(v)
8. color[u] ← BLACK
9. time ← time + 1
10. f[u] ← time
7/6/2004 Lecture 10
COSC3101A
u
v
w
x
y
z
time = 1
u
v
w
1/
x
y
z
u
v
w
1/
2/
x
y
z
18
Example
u
v
w
1/
u
v
1/
2/
w
u
v
1/
2/
w
3/
x
y
z
x
y
z
x
y
z
u
v
w
u
v
w
u
v
w
1/
2/
1/
2/
1/
2/
B
4/
3/
x
y
u
v
1/
2/
B
4/
3/
z
x
y
w
u
v
1/
2/7
B
4/5
3/
z
x
y
z
w
u
v
w
1/
2/7
B
4/5
3/6
x
y
7/6/2004 Lecture 10
z
F
4/5
3/6
x
y
COSC3101A
z
B
4/5
3/6
x
y
z
19
Example (cont.)
u
v
1/8
2/7
F
B
u
v
w
u
v
1/8
2/7
9/
1/8
2/7
F
4/5
3/6
x
y
u
v
1/8
2/7
F
w
B
C
B
F
4/5
3/6
z
x
y
w
u
v
9/
1/8
2/7
B
F
C
B
z
x
y
z
w
u
v
w
9/
1/8
2/7
B
F
B
4/5
3/6
10/
4/5
3/6
x
y
z
x
y
z
x
y
u
v
w
1/8
2/7
3/6
x
y
7/6/2004 Lecture 10
z
10/11
z
• The order in which nodes are explored
9/12
10/11
9/
The results of DFS may depend on:
B
4/5
C
B
10/
B
9/
3/6
3/6
F
C
4/5
4/5
C
w
in procedure DFS
• The order in which the neighbors of a
vertex are visited in DFS-VISIT
COSC3101A
20
Edge Classification
• Tree edge (reaches a WHITE
vertex):
– (u, v) is a tree edge if v was first
discovered by exploring edge (u, v)
• Back edge (reaches a GRAY
vertex):
– (u, v), connecting a vertex u to an
ancestor v in a depth first tree
– Self loops (in directed graphs) are
also back edges
7/6/2004 Lecture 10
COSC3101A
u
v
w
x
y
z
u
v
w
1/
2/
1/
B
4/
3/
x
y
z
21
Edge Classification
• Forward edge (reaches a BLACK
vertex & d[u] < d[v]):
– Non-tree edges (u, v) that connect a vertex
u to a descendant v in a depth first tree
• Cross edge (reaches a BLACK vertex
& d[u] > d[v]):
– Can go between vertices in same depth-first
tree (as long as there is no ancestor /
descendant relation) or between different
depth-first trees
7/6/2004 Lecture 10
COSC3101A
u
v
1/
2/7
F
w
B
4/5
3/6
x
y
z
u
v
w
1/8
2/7
F
B
4/5
3/6
x
y
C
9/
z
22
Analysis of DFS(G)
1. for each u  V[G]
2.
do color[u] ← WHITE (V)
3.
[u] ← NIL
4. time ← 0
5. for each u  V[G]
(V) – exclusive
of time for
6.
do if color[u] = WHITE
7.
then DFS-VISIT(u) DFS-VISIT
7/6/2004 Lecture 10
COSC3101A
23
Analysis of DFS-VISIT(u)
1. color[u] ← GRAY
DFS-VISIT is called exactly
once for each vertex
2. time ← time+1
3. d[u] ← time
4. for each v  Adj[u]
5.
do if color[v] = WHITE
Each loop takes
|Adj[v]|
6.
then [v] ← u
7.
DFS-VISIT(v)
8. color[u] ← BLACK
9. time ← time + 1 Total: ΣvV |Adj[v]| + (V) = (V + E)
10. f[u] ← time
(E)
7/6/2004 Lecture 10
COSC3101A
24
Properties of DFS
• u = [v]  DFS-VISIT(v) was called
during a search of u’s adjacency list
• Vertex v is a descendant of vertex u
in the depth first forest  v is
u
v
1/
2/
w
3/
x
y
z
discovered during the time in which
u is gray
7/6/2004 Lecture 10
COSC3101A
25
Parenthesis Theorem
In any DFS of a graph G, for
all u, v, exactly one of the
following holds:
1.
y
z
s
t
3/6
2/9
1/10
11/16
4/5
7/8
12/13
14/15
x
w
v
u
[d[u], f[u]] and [d[v], f[v]] are
disjoint, and neither of u and v
s
is a descendant of the other
z
2. [d[v], f[v]] is entirely within
descendant of v
7/6/2004 Lecture 10
u
w
x
descendant of u
[d[v], f[v]] and u is a
v
y
[d[u], f[u]] and v is a
3. [d[u], f[u]] is entirely within
t
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
u)
(u u)
Well-formed expression: parenthesis are
properly nested
COSC3101A
26
t)
Other Properties of DFS
Corollary
Vertex v is a proper descendant of u
 d[u] < d[v] < f[v] < f[u]
u
1/8
F
4/5
2/7
B
C
9/12
B
3/6
10/11
v
Theorem (White-path Theorem)
In a depth-first forest of a graph G, vertex
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.
7/6/2004 Lecture 10
COSC3101A
u
1/
2/
v
27
Topological Sort
Topological sort of a directed acyclic graph G =
(V, E): a linear order of vertices such that if there
exists an edge (u, v), then u appears before v in
the ordering.
• Directed acyclic graphs (DAGs)
– Used to represent precedence of events or processes
that have a partial order
a before b
b before c
a before c
b before c
a before c
What about
a and b?
Topological sort helps us establish a total order
7/6/2004 Lecture 10
COSC3101A
28
Topological Sort
undershorts 11/ 16
pants
6/7
12/15
shirt
1/8
tie
2/5
belt
jacket
socks
undershorts
TOPOLOGICAL-SORT(V, E)
17/18 socks
3/4
1. Call DFS(V, E) to compute
finishing times f[v] for each
shoes 13/14
vertex v
2. When each vertex is finished,
watch 9/10
insert it onto the front of a
linked list
3. Return the linked list of
vertices
pants
shoes
watch
shirt
belt
tie
jacket
Running time: (V + E)
7/6/2004 Lecture 10
COSC3101A
29
Topological Sort
undershorts 11/ 16
pants
12/15
1/8
belt
watch 9/10
tie
jacket
socks
Topological sort:
an ordering of vertices along a
horizontal line so that all directed
edges go from left to right.
shoes 13/14
shirt
6/7
17/18 socks
undershorts
7/6/2004 Lecture 10
2/5
3/4
pants
shoes
watch
COSC3101A
shirt
belt
tie
jacket
30
Lemma
A directed graph is acyclic  a DFS on G yields
no back edges.
v
Proof:
(u, v)
“”: acyclic  no back edge
– Assume back edge  prove cycle
u
– Assume there is a back edge (u, v)
v is an ancestor of u
 there is a path from v to u in G (v  u)
 v  u + the back edge (u, v) yield a cycle
7/6/2004 Lecture 10
COSC3101A
31
Lemma
A directed graph is acyclic  a DFS on G yields
no back edges.
Proof:
(u, v)
“”: no back edge  acyclic
– Assume cycle  prove back edge
u
– Suppose G contains cycle c
– Let v be the first vertex discovered in c, and (u, v) be
the preceding edge in c
– At time d[v], vertices of c form a white path v  u
– u is descendant of v in depth-first forest (by white-path
theorem)
 (u, v) is a back edge
7/6/2004 Lecture 10
COSC3101A
32
v
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
every pair of vertices u, v  C, we have both
u  v and v  u.
7/6/2004 Lecture 10
COSC3101A
33
The Transpose of a Graph
• GT = transpose of G
– GT is G with all edges reversed
– GT = (V, ET), ET = {(u, v) : (v, u)  E}
• If using adjacency lists: we can create GT in
(V + E) time
1
2
1
2
3
5
7/6/2004 Lecture 10
3
4
5
COSC3101A
4
34
Finding the SCC
• Observation: G and GT have the same SCC’s
– u and v are reachable from each other in G  they are
reachable from each other in GT
• Idea for computing the SCC of a DAG G = (V, E):
– Make two depth first searches: one on G and one on GT
1
2
1
2
3
5
7/6/2004 Lecture 10
3
4
5
COSC3101A
4
35
STRONGLY-CONNECTED-COMPONENTS(G)
1. call DFS(G) to compute finishing times f[u] for
each vertex u
2. compute GT
3. call DFS(GT), but in the main loop of DFS,
consider vertices in order of decreasing f[u]
(as computed in first DFS)
4. output the vertices in each tree of the depthfirst forest formed in second DFS as a separate
SCC
7/6/2004 Lecture 10
COSC3101A
36
Example
a
b
c
d
13/14
11/ 16
1/ 10
8/ 9
12/15
3/ 4
2/ 7
5/ 6
e
f
g
h
a
b
c
d
e
f
g
h
DFS on the initial graph G
b e a c d g h f
16 15 14 10 9 7 6 4
DFS on GT:
• start at b: visit a, e
• start at c: visit d
• start at g: visit f
• start at h
Strongly connected components: C1 = {a, b, e}, C2 = {c, d}, C3 = {f, g}, C4 = {h}
7/6/2004 Lecture 10
COSC3101A
37
Component Graph
a
b
c
d
cd
abe
e
f
g
h
fg
h
• The component graph GSCC = (VSCC, ESCC):
– VSCC = {v1, v2, …, vk}, where vi corresponds to each
strongly connected component Ci
– There is an edge (vi, vj)  ESCC if G contains a
directed edge (x, y) for some x  Ci and y  Cj
• The component graph is a DAG
7/6/2004 Lecture 10
COSC3101A
38
Lemma 1
Let C and C’ be distinct SCC’s in G
Let u, v  C, and u’, v’  C’
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
• There exists u  u’  v’
C
u
C’
u’
v
• There exists v’  v  u
v’
• u and v’ are reachable from
each other, so they are not in
separate SCC’s: contradiction!
7/6/2004 Lecture 10
COSC3101A
39
Notations
• Extend notation for d (starting time) and f
(finishing time) to sets of vertices U  V:
– d(U) = minuU { d[u] } (earliest discovery time)
– f(U) = maxuU { f[u] } (latest finishing time)
C1
d(C1) =11
f(C1) =16
a
b
c
13/14
11/ 16
1/ 10
8/ 9
12/15
3/ 4
2/ 7
5/ 6
e
f
g
h
d(C3) =2
f(C3) =7
7/6/2004 Lecture 10
C2
C3
COSC3101A
d
d(C2) =1
f(C2) =10
C4
d(C4) =5
f(C4) =6
40
Lemma 2
• Let C and C’ be distinct SCCs in a directed
graph G = (V, E). If there is an edge (u, v)  E,
where u  C and v  C’ then f(C) > f(C’).
• Consider C1 and C2, connected by edge (b, c)
C1
d(C1) =11
f(C1) =16
a
b
c
13/14
11/ 16
1/ 10
8/ 9
12/15
3/ 4
2/ 7
5/ 6
e
f
g
h
d(C3) =3
f(C3) =7
7/6/2004 Lecture 10
C2
C3
COSC3101A
d
d(C2) =1
f(C2) =10
C4
d(C4) =5
f(C4) =6
41
Corollary
• Let C and C’ be distinct SCCs in a directed
graph G = (V, E). If there is an edge (u, v)  ET,
where u  C and v  C’ then f(C) < f(C’).
• Consider C2 and C1, connected by edge (c, b)
C1 = C’
a
b
c
C2 = C
d
e
f
g
h
C3
7/6/2004 Lecture 10
C4
COSC3101A
• Since (c, b)  ET
 (b, c)  E
• From previous
lemma:
f(C1) > f(C2)
f(C’) > f(C)
42
Discussion
f(C) < f(C’)
• Each edge in GT that goes between different
components goes from a component with an
earlier finish time (in the DFS) to one with a later
finish time
C1 = C’
a
b
c
C2 = C
d
e
f
g
h
C3
7/6/2004 Lecture 10
COSC3101A
C4
43
Why does SCC Work?
• When we do the second DFS, on GT, we start with a
component C such that f(C) is maximum (b, in our case)
• We start from b and visit all vertices in C1
• From corollary: f(C) > f(C’) for all C  C’  there are no
edges from C to any other SCCs in GT
 DFS will visit only vertices in C1
 The depth-first tree rooted at b contains exactly the
vertices of C1
C1
C2
a
b
c
d
f
g
h
b e a c d g h f
16 15 14 10 9 7 6 4
e
7/6/2004 Lecture 10
C3
COSC3101A
C4
44
Why does SCC Work? (cont.)
• The next root chosen in the second DFS is in SCC C2
such that f(C) is maximum over all SCC’s other than C1
• DFS visits all vertices in C2
– the only edges out of C2 go to C1, which we’ve already visited
 The only tree edges will be to vertices in C2
• Each time we choose a new root it can reach only:
– vertices in its own component
– vertices in components already visited
a
C1
b
c
f
g
C2
d
b e a c d g h f
16 15 14 10 9 7 6 4
7/6/2004 Lecture 10
e
COSC3101A
C3
h
C4
45
Readings
• Chapter 22
• Appendix B
7/6/2004 Lecture 10
COSC3101A
46