cpsc 411 design and analysis of algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1...
TRANSCRIPT
![Page 1: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/1.jpg)
1
Graph Algorithms
Andreas Klappenecker
[based on slides by Prof. Welch]
Monday, October 1, 2012
![Page 2: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/2.jpg)
2
Directed Graphs
Let V be a finite set and E a binary relation on V, that is, E⊆VxV. Then the pair G=(V,E) is called a directed graph.
• The elements in V are called vertices.• The elements in E are called edges. • Self-loops are allowed, i.e., E may contain (v,v).
Monday, October 1, 2012
![Page 3: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/3.jpg)
3
Undirected Graphs
Let V be a finite set and E a subset of { e | e ⊆ V, |e|=2 }. Then the pair G=(V,E) is called an undirected graph.
• The elements in V are called vertices.• The elements in E are called edges, e={u,v}.
• Self-loops are not allowed, e≠{u,u}={u}.
Monday, October 1, 2012
![Page 4: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/4.jpg)
4
Adjacency
By abuse of notation, we will write (u,v) for an edge {u,v} in an undirected graph.
If (u,v) in E, then we say that the vertex v is adjacent to the vertex u.
For undirected graphs, adjacency is a symmetric relation.
Monday, October 1, 2012
![Page 5: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/5.jpg)
5
Graph Representations
• Adjacency lists• Adjacency matrix
Monday, October 1, 2012
![Page 6: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/6.jpg)
6
Adjacency List Representation
a
c d
b
eb ca
b
c
d
e
a d e
+ Space-efficient: just O(|V|) space for sparse graphs
- Testing adjacency is O(|V|) in the worst case
Monday, October 1, 2012
![Page 7: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/7.jpg)
7
Adjacency Matrix
a
c d
b
eabcde
a b c d e0 1 1 0 01 0 0 1 11 0 0 1 00 1 1 0 10 1 0 1 0
+ Can check adjacency in constant time
- Needs Ω(|V|2) space
Monday, October 1, 2012
![Page 8: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/8.jpg)
8
Graph Traversals
Ways to traverse or search a graph such that every node is visited exactly once
Monday, October 1, 2012
![Page 9: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/9.jpg)
9
Breadth-First Search
Monday, October 1, 2012
![Page 10: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/10.jpg)
10
Breadth First Search (BFS)Input: A graph G = (V,E) and source node s in Vfor each node v do
mark v as unvisitedodmark s as visitedenq(Q,s) // first-in first-out queue Q
while Q is not empty dou := deq(Q)for each unvisited neighbor v of u do
mark v as visited; enq(Q,v);od
odMonday, October 1, 2012
![Page 11: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/11.jpg)
11
BFS Example
a
c
d
b
s
Monday, October 1, 2012
![Page 12: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/12.jpg)
11
BFS Example
Visit the nodes in the order: a
c
d
b
s
Monday, October 1, 2012
![Page 13: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/13.jpg)
11
BFS Example
Visit the nodes in the order: s
a
c
d
b
s
Monday, October 1, 2012
![Page 14: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/14.jpg)
11
BFS Example
Visit the nodes in the order: sa, d
a
c
d
b
s
Monday, October 1, 2012
![Page 15: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/15.jpg)
11
BFS Example
Visit the nodes in the order: sa, db, c
a
c
d
b
s
Monday, October 1, 2012
![Page 16: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/16.jpg)
11
BFS Example
Visit the nodes in the order: sa, db, cWorkout the evolution of the state of queue.
a
c
d
b
s
Monday, October 1, 2012
![Page 17: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/17.jpg)
12
BFS Tree
• We can make a spanning tree rooted at s by remembering the "parent" of each node
Monday, October 1, 2012
![Page 18: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/18.jpg)
13
Breadth First Search #2
• Input: G = (V,E) and source s in V• for each node v do
• mark v as unvisited• parent[v] := nil
• mark s as visited• parent[s] := s• enq(Q,s) // FIFO queue Q
Monday, October 1, 2012
![Page 19: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/19.jpg)
14
Breadth First Search #2
• while Q is not empty do• u := deq(Q)• for each unvisited neighbor v of u do
• mark v as visited• parent[v] := u• enq(Q,v)
Monday, October 1, 2012
![Page 20: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/20.jpg)
15
BFS Tree Example
a
c
d
b
s
Monday, October 1, 2012
![Page 21: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/21.jpg)
16
BFS Trees
• BFS tree is not necessarily unique for a given graph
• Depends on the order in which neighboring nodes are processed
Monday, October 1, 2012
![Page 22: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/22.jpg)
17
BFS Numbering
• During the breadth-first search, assign an integer to each node
• Indicate the distance of each node from the source s
Monday, October 1, 2012
![Page 23: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/23.jpg)
18
Breadth First Search #3
• Input: G = (V,E) and source s in V• for each node v do
• mark v as unvisited• parent[v] := nil• d[v] := infinity
• mark s as visited• parent[s] := s• d[s] := 0• enq(Q,s) // FIFO queue Q
Monday, October 1, 2012
![Page 24: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/24.jpg)
19
Breadth First Search #3
• while Q is not empty do• u := deq(Q)• for each unvisited neighbor v of u do
• mark v as visited• parent[v] := u• d[v] := d[u] + 1• enq(Q,v)
Monday, October 1, 2012
![Page 25: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/25.jpg)
20
BFS Numbering Example
a
c
d
b
sd = 0
d = 1
d = 1
d = 2
d = 2
Monday, October 1, 2012
![Page 26: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/26.jpg)
21
Shortest Path Tree
• Theorem: BFS algorithm• visits all and only nodes reachable from s• sets d[v] equal to the shortest path
distance from s to v, for all nodes v, and• sets parent variables to form a shortest
path tree
Monday, October 1, 2012
![Page 27: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/27.jpg)
22
Proof Ideas
• Use induction on distance from s to show that the d-values are set properly.
• Basis: distance 0. d[s] is set to 0.• Induction: Assume true for all nodes at
distance x-1 and show for every node v at distance x.
• Since v is at distance x, it has at least one neighbor at distance x-1. Let u be the first of these neighbors that is enqueued.
Monday, October 1, 2012
![Page 28: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/28.jpg)
23
Proof Ideas
u
c
d
vsdist=x-1
dist=x
dist=x-1
dist=x+1
Key property of shortest path distances: If v has distance x,• it must have a neighbor with distance x-1, • no neighbor has distance less than x-1, and • no neighbor has distance more than x+1
Monday, October 1, 2012
![Page 29: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/29.jpg)
24
Proof Ideas
• Fact: When u is dequeued, v is still unvisited.• because of how queue operates and since d
never underestimates the distance
• By induction, d[u] = x-1.• When v is enqueued, d[v] is set to d[u] + 1= x
Monday, October 1, 2012
![Page 30: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/30.jpg)
25
BFS Running Time
• Initialization of each node takes O(V) time• Every node is enqueued once and dequeued
once, taking O(V) time• When a node is dequeued, all its neighbors
are checked to see if they are unvisited, taking time proportional to number of neighbors of the node, and summing to O(E) over all iterations
• Total time is O(V+E)Monday, October 1, 2012
![Page 31: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/31.jpg)
26
Depth-First Search
Monday, October 1, 2012
![Page 32: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/32.jpg)
27
Depth-First Search
Input: G = (V,E)
for each node u do
mark u as unvisited
od;
for each unvisited node u
recursiveDFS(u): mark u as visited; for each unvisited neighbor v of u do recursiveDFS(v)od
Monday, October 1, 2012
![Page 33: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/33.jpg)
28
DFS Example
Example taken from http://atcp07.cs.brown.edu/courses/cs016/Resource/old_lectures/DFS.pdf
Monday, October 1, 2012
![Page 34: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/34.jpg)
29
Example taken from http://atcp07.cs.brown.edu/courses/cs016/Resource/old_lectures/DFS.pdf
Monday, October 1, 2012
![Page 35: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/35.jpg)
30
Disconnected Graphs
What if the graph is disconnected or is directed?We call DFS on several nodes to visit all nodes
• purpose of second for-loop in non-recursive wrapper
a
cb
d
e
Monday, October 1, 2012
![Page 36: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/36.jpg)
31
DFS Forest
By keeping track of parents, we want to construct a forest resulting from the DFS traversal.
Monday, October 1, 2012
![Page 37: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/37.jpg)
32
Depth-First Search #2
• Input: G = (V,E)• for each node u do
• mark u as unvisited• parent[u] := nil
• for each unvisited node u do• parent[u] := u // a root
• call recursive DFS(u)
• recursiveDFS(u):• mark u as visited• for each unvisited
neighbor v of u do• parent[v] := u• call recursiveDFS(v)
Monday, October 1, 2012
![Page 38: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/38.jpg)
33
Further Properties of DFS
Let us keep track of some interesting information for each node.
We will timestamp the steps and record the• discovery time, when the recursive call starts• finish time, when its recursive call ends
Monday, October 1, 2012
![Page 39: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/39.jpg)
34
Depth-First Search #3
• Input: G = (V,E)• for each node u do
• mark u as unvisited• parent[u] := nil
• time := 0• for each unvisited node
u do• parent[u] := u // a root • call recursive DFS(u)
• recursiveDFS(u):• mark u as visited• time++• disc[u] := time• for each unvisited
neighbor v of u do• parent[v] := u• call recursiveDFS(v)
• time++• fin[u] := time
Monday, October 1, 2012
![Page 40: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/40.jpg)
35
Running Time of DFS
• initialization takes O(V) time• second for loop in non-recursive
wrapper considers each node, so O(V) iterations
• one recursive call is made for each node• in recursive call for node u, all its
neighbors are checked; total time in all recursive calls is O(E)
Monday, October 1, 2012
![Page 41: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/41.jpg)
36
Nested Intervals
• Let interval for node v be [disc[v],fin[v]].• Fact: For any two nodes, either one interval
precedes the other or one is enclosed in the other [Reason: recursive calls are nested.]
• Corollary: v is a descendant of u in the DFS forest iff the interval of v is inside the interval of u.
Monday, October 1, 2012
![Page 42: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/42.jpg)
37
Classifying Edges
• Consider edge (u,v) in directed graph G = (V,E) w.r.t. DFS forest
• tree edge: v is a child of u• back edge: v is an ancestor of u• forward edge: v is a descendant of u
but not a child• cross edge: none of the above
Monday, October 1, 2012
![Page 43: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/43.jpg)
38
Example of Classifying Edges
a
c
b e
fd
in DFS forest
not in DFS forest
tree
tree
tree
treeforward
back
back
cross
1/8
4/5
2/7
3/6 10/11
9/12
a/b disc./finish. time
tree edge: v child of uback edge: v ancestor of u forward edge: v descendant of u, but not childcross edge: none of the above
Monday, October 1, 2012
![Page 44: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/44.jpg)
39
DFS Application: Topological Sort
• Given a directed acyclic graph (DAG), find a linear ordering of the nodes such that if (u,v) is an edge, then u precedes v.
• DAG indicates precedence among events:• events are graph nodes, edge from u to v means
event u has precedence over event v
• Partial order because not all events have to be done in a certain order
Monday, October 1, 2012
![Page 45: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/45.jpg)
40
Precedence Example
• Tasks that have to be done to eat breakfast:• get glass, pour juice, get bowl, pour cereal,
pour milk, get spoon, eat.
• Certain events must happen in a certain order (ex: get bowl before pouring milk)
• For other events, it doesn't matter (ex: get bowl and get spoon)
Monday, October 1, 2012
![Page 46: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/46.jpg)
41
Precedence Example
get glass
pour juice
get bowl
pour cereal
pour milk get spoon
eat breakfast
Order: glass, juice, bowl, cereal, milk, spoon, eat.
Monday, October 1, 2012
![Page 47: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/47.jpg)
42
Why Acyclic?
• Why must a directed graph be acyclic for the topological sort problem?
• Otherwise, no way to order events linearly without violating a precedence constraint.
Monday, October 1, 2012
![Page 48: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/48.jpg)
43
Idea for Topological Sort Alg.
• What does DFS do on a DAG?
eatjuiceglass
milkcereal
bowl
spoon
consider reverse order of finishing times:spoon, bowl, cereal, milk, glass, juice, eat
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Monday, October 1, 2012
![Page 49: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/49.jpg)
44
Topological Sort Algorithm
input: DAG G = (V,E)1. call DFS on G to compute finish[v] for
all nodes v2. when each node's recursive call
finishes, insert it on the front of a linked list
3. return the linked list
Monday, October 1, 2012
![Page 50: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/50.jpg)
45
Correctness of T.S. Algorithm
• Show that if (u,v) is an edge, then v finishes before u.
Case 1: v is finished when u is discovered. Then v finishes before u finishes.
Case 2: v is not yet discovered when u is discovered. Claim: v will become a descendant of u and
thus v will finish before u finishes.Case 3: v is discovered but not yet finished
Monday, October 1, 2012
![Page 51: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/51.jpg)
46
Correctness of T.S. Algorithm
• v is discovered but not yet finished when u is discovered.
• Then u is a descendant of v.• But that would make (u,v) a back edge
and a DAG cannot have a back edge (the back edge would form a cycle).
• Thus Case 3 is not possible.
Monday, October 1, 2012
![Page 52: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/52.jpg)
47
DFS Application: Strongly Connected Components
• Consider a directed graph.• A strongly connected component (SCC)
of the graph is a maximal set of nodes with a (directed) path between every pair of nodes
• Problem: Find all the SCCs of the graph.
Monday, October 1, 2012
![Page 53: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/53.jpg)
48
What Are SCCs Good For?
• packaging software modules• construct directed graph of which
modules call which other modules• A SCC is a set of mutually interacting
modules• pack together those in the same SCC
from http://www.cs.princeton.edu/courses/archive/fall07/cos226/lectures.html
Monday, October 1, 2012
![Page 54: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/54.jpg)
49
SCC Example
h f a e
g c b d
Monday, October 1, 2012
![Page 55: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/55.jpg)
49
SCC Example
h f a e
g c b d
Monday, October 1, 2012
![Page 56: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/56.jpg)
49
SCC Example
h f a e
g c b d
Monday, October 1, 2012
![Page 57: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/57.jpg)
49
SCC Example
h f a e
g c b d
Monday, October 1, 2012
![Page 58: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/58.jpg)
49
SCC Example
h f a e
g c b d
Monday, October 1, 2012
![Page 59: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/59.jpg)
49
SCC Example
h f a e
g c b d
four SCCs
Monday, October 1, 2012
![Page 60: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/60.jpg)
50
How Can DFS Help?
• Suppose we run DFS on the directed graph.
• All nodes in the same SCC are in the same DFS tree.
• But there might be several different SCCs in the same DFS tree.• Example: start DFS from node h in
previous graphMonday, October 1, 2012
![Page 61: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/61.jpg)
51
Main Idea of SCC Algorithm
• DFS tells us which nodes are reachable from the roots of the individual trees
• Also need information in the "other direction": is the root reachable from its descendants?
• Run DFS again on the "transpose" graph (reverse the directions of the edges)
Monday, October 1, 2012
![Page 62: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/62.jpg)
52
SCC Algorithm
input: directed graph G = (V,E)1. call DFS(G) to compute finishing times2. compute GT // transpose graph3. call DFS(GT), considering nodes in
decreasing order of finishing times4. each tree from Step 3 is a separate
SCC of G
Monday, October 1, 2012
![Page 63: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/63.jpg)
53
SCC Algorithm Example
h f a e
g c b d
input graph - run DFS
Monday, October 1, 2012
![Page 64: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/64.jpg)
54
After Step 1
cb g
f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
de
h
fin(c
)
fin(d
)
fin(b
)
fin(e
)fin
(a)
fin(h
)
fin(g
)
fin(f)
Order of nodes for Step 3: f, g, h, a, e, b, d, c
a
f reaches g reaches h; a reaches b, e; b reaches c,d
Monday, October 1, 2012
![Page 65: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/65.jpg)
55
After Step 2
h f a e
g c b d
transposed input graph - run DFS with specified order of nodes:f can be reached from h, h can be reached from g, ...
f, g, h, a, e, b, d, c
Monday, October 1, 2012
![Page 66: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/66.jpg)
56
After Step 3
gh ef a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
d
SCCs are {f,h,g} and {a,e} and {b,c} and {d}.
bc
Monday, October 1, 2012
![Page 67: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/67.jpg)
57
Running Time of SCC Algorithm
• Step 1: O(V+E) to run DFS• Step 2: O(V+E) to construct transpose
graph, assuming adjacency list rep.• Step 3: O(V+E) to run DFS again• Step 4: O(V) to output result• Total: O(V+E)
Monday, October 1, 2012
![Page 68: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/68.jpg)
58
Correctness of SCC Algorithm
• Proof uses concept of component graph, GSCC, of G.
• Nodes are the SCCs of G; call them C1, C2, …, Ck
• Put an edge from Ci to Cj iff G has an edge from a node in Ci to a node in Cj
Monday, October 1, 2012
![Page 69: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/69.jpg)
59
Example of Component Graph
{a,e}
{f,h,g} {d}
{b,c}
based on example graph from before
Monday, October 1, 2012
![Page 70: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/70.jpg)
60
Facts About Component Graph
• Claim: GSCC is a directed acyclic graph.• Why? • Suppose there is a cycle in GSCC such
that component Ci is reachable from component Cj and vice versa.
• Then Ci and Cj would not be separate SCCs.
Monday, October 1, 2012
![Page 71: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/71.jpg)
61
Facts About Component Graph
• Consider any component C during Step 1 (running DFS on G)
• Let d(C) be earliest discovery time of any node in C
• Let f(C) be latest finishing time of any node in C
• Lemma: If there is an edge in GSCC from component C' to component C, then f(C') > f(C).
Monday, October 1, 2012
![Page 72: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/72.jpg)
62
Proof of Lemma
• Case 1: d(C') < d(C).• Suppose x is first node discovered in
C'.• By the way DFS works, all nodes in C'
and C become descendants of x.• Then x is last node in C' to finish and
finishes after all nodes in C.• Thus f(C') > f(C).
C' C
Monday, October 1, 2012
![Page 73: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/73.jpg)
63
Proof of Lemma
• Case 2: d(C') > d(C).• Suppose y is first node discovered in C.• By the way DFS works, all nodes in C become
descendants of y.• Then y is last node in C to finish. • Since C' → C, no node in C' is reachable from
y, so y finishes before any node in C' is discovered.
C' C
Monday, October 1, 2012
![Page 74: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/74.jpg)
64
SCC Algorithm is Correct
• Prove this theorem by induction on number of trees found in Step 3 (calling DFS on GT).
• Hypothesis is that the first k trees found constitute k SCCs of G.
• Basis: k = 0. No work to do !
Monday, October 1, 2012
![Page 75: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/75.jpg)
65
SCC Algorithm is Correct
• Induction: Assume the first k trees constructed in Step 3 correspond to k SCCs, and consider the (k+1)st tree.
• Let u be the root of the (k+1)st tree.• u is part of some SCC, call it C.• By the inductive hypothesis, C is not one of
the k SCCs already found and all nodes in C are unvisited when u is discovered.• By the way DFS works, all nodes in C become part
of u's treeMonday, October 1, 2012
![Page 76: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/76.jpg)
66
SCC Algorithm is Correct
• Show only nodes in C become part of u's tree. Consider an outgoing edge from C.
w
z
u
C'
CGT:
Monday, October 1, 2012
![Page 77: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/77.jpg)
66
SCC Algorithm is Correct
• Show only nodes in C become part of u's tree. Consider an outgoing edge from C.
w
z
u
C'
CGT:
w
z
u
C'
CG:
Monday, October 1, 2012
![Page 78: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/78.jpg)
67
SCC Algorithm is Correct
• By lemma, in Step 1 the last node in C' finishes after the last node in C finishes.
• Thus in Step 3, some node in C' is discovered before any node in C is discovered.
• Thus all of C', including w, is already visited before u's DFS tree starts
w
z
u
C'
CG:
Monday, October 1, 2012
![Page 79: CPSC 411 Design and Analysis of Algorithmsfaculty.cse.tamu.edu/klappi/csce411-f12/csce411-set10.pdf1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch] Monday,](https://reader034.vdocuments.net/reader034/viewer/2022042708/5aebbeff7f8b9ab24d8f2289/html5/thumbnails/79.jpg)
68
Conclusion
• The proof that the algorithm does indeed find the strongly connected components is rather typical.
• The main ideas are quite simple: • the DFS forest of G specifies which nodes can be
reached from their roots• the DFS forest of Gt specifies from where the
root can be reached. • You need to have a good grasp of the algorithm
before you can attempt to prove it correct. The formalization of the proof can be difficult.
68
Monday, October 1, 2012