Directed Graphs: Digraph Search Transitive Closure Topological Sort Strong Components
Directed Graphs: Digraph Search Transitive Closure Topological Sort Strong Components
digraph search
transitive closure
topological sort
strong components
References:
Algorithms in Java, Chapter 19
http://www.cs.princeton.edu/introalgsds/52directed
1
Directed graphs (digraphs)
6 22
2
Digraph applications
3
Some digraph problems
Transitive closure.
Is there a directed path from v to w?
Strong connectivity.
Are all vertices mutually reachable?
Topological sort.
Can you draw the digraph so that all edges point
from left to right?
PERT/CPM.
Given a set of tasks with precedence constraints,
how we can we best complete them all?
Vertices
• this lecture: use integers between 0 and V-1.
• real world: convert between names and integers with symbol table.
1 2 6 9 10
Same as undirected graph
BUT
orientation of edges is significant.
3 4 11 12
5
Adjacency matrix digraph representation
to
0 0 1 2 3 4 5 6 7 8 9 10 11 12
one entry 0 0 1 1 0 0 1 1 0 0 0 0 0 0
for each
edge 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 6 2 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0 0 0 0 0 0
3 4 5 0 0 0 1 1 0 0 0 0 0 0 0 0
from 6 0 0 0 0 1 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 1 0 0 0 0
5
9 10 8 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 1 1 1
10 0 0 0 0 0 0 0 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0 1
7 8 11 12
12 0 0 0 0 0 0 0 0 0 0 0 0 0
6
Adjacency-list digraph representation
0: 5 2 1 6
0
1:
2:
one entry
3:
1 2 6 for each
4: 3 edge
5: 4 3
3 4
6: 4
5 7: 8
9 10
8:
9: 10 11 12
7 8 11 12 10:
11: 12
12:
7
Adjacency-SET digraph representation
0: { 1 2 5 6 }
0
1: { }
2: { }
1 2 6 3: { } one entry
for each
4: { 3 } edge
5: { 3 4 }
3 4
6: { 4 }
7: { 8 }
5
9 10 { }
8:
9: { 10 11 12 }
7 8 11 12 10: { }
11: { 12 }
12: { }
8
Adjacency-SET digraph representation: Java implementation
public Digraph(int V)
{
this.V = V;
adj = (SET<Integer>[]) new SET[V]; create empty
for (int v = 0; v < V; v++) V-vertex graph
adj[v] = new SET<Integer>();
}
adjacency matrix V2 1 V
10 40
2
14
22 48
39
18 6 21
24
4
38
3 17
35
36
46
20
11
digraph search
transitive closure
topological sort
strong components
12
Digraph application: program control-flow analysis
13
Digraph application: mark-sweep garbage collector
Reachable objects.
Objects indirectly accessible by
program (starting at a root and
following a chain of pointers).
Memory cost: Uses 1 extra mark bit per object, plus DFS stack. 14
Reachability
15
Reachability
16
Digraph-processing challenge 1:
How difficult?
1) any COS126 student could do it
2) need to be a typical diligent COS226 student
3) hire an expert 0-1
4) intractable 0 0-6
0-2
5) no one knows 3-4
1 2 6 3-2
5-4
5-0
3 4 3-5
2-1
5 6-4
3-1
17
Depth-first search in digraphs
recursive
18
Depth-first search (single-source reachability)
20
Breadth-first search in digraphs
BFS. 7
4
5
.0
.0
•
6 .0
2 7 .0
( say http://www.princeton.edu.).
12 .0
8
44 13 .0
14 .0
•
45 15 .0
•
19 .0
•
23 .0
A. Internet is not fixed (some pages generate new ones when visited)
6 22
subtle point:
Page ranks think
with histogram for a largerabout
example it!
22
Web crawler: BFS-based Java implementation
String s = "http://www.princeton.edu";
q.enqueue(s); start crawling from s
visited.add(s);
while (!q.isEmpty())
{
String v = q.dequeue(); read in raw html for next site in queue
System.out.println(v);
In in = new In(v);
String input = in.readAll();
http://xxx.yyy.zzz
24
Graph-processing challenge (revisited)
25
Digraph-processing challenge 2
How difficult?
1) any COS126 student could do it
2) need to be a typical diligent COS226 student
3) hire an expert
4) intractable 0-1
5) no one knows 0 0-6
0-2
6) impossible 3-4
1 2 6 3-2
5-4
5-0
3 4 3-5
2-1
5 6-4
1-3
26
Transitive Closure
TC is usually dense
so adjacency matrix
Transitive closure representation is OK
of G
27
Digraph-processing challenge 2 (revised)
How difficult?
1) any COS126 student could do it
2) need to be a typical diligent COS226 student
3) hire an expert
4) intractable 0-1
5) no one knows 0 0-6
0-2
6) impossible 3-4
1 2 6 3-2
5-4
5-0
3 4 3-5
2-1
5 6-4
1-3
28
Digraph-processing challenge 2 (revised again)
How difficult?
1) any COS126 student could do it
2) need to be a typical diligent COS226 student
3) hire an expert 0-1
4) intractable 0 0-6
0-2
5) no one knows 3-4
6) impossible 1 2 6 3-2
5-4
5-0
3 4 3-5
2-1
5 6-4
1-3
29
Transitive closure: Java implementation
31
Digraph application: Scheduling
Graph model.
• Create a vertex v for each task.
• Create an edge vw if task v must precede task w.
• Schedule tasks in topological order.
tasks
feasible
schedule
32
Topological Sort
33
Digraph-processing challenge 3
How difficult?
1) any CS126 student could do it 0-1
2) need to be a typical diligent CS226 student 0-6
0-2
3) hire an expert 0-5
4) intractable 2-3
4-9
5) no one knows 6-4
6) impossible
6-9
7-6
8-7
9-10
9-11
9-12
11-12
34
Topological sort in a DAG: Java implementation
public TopologicalSorter(Digraph G)
{
marked = new boolean[G.V()];
ts = new int[G.V()];
count = G.V();
for (int v = 0; v < G.V(); v++)
if (!marked[v]) tsort(G, v);
}
“visit” means “call tsort()” and “leave” means “return from tsort()
marked[] ts[]
adj SETs
visit 0: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0: 1 2 5
visit 1: 1 1 0 0 0 0 0 0 0 0 0 0 0 0
1: 4
visit 4: 1 1 0 0 1 0 0 0 0 0 0 0 0 0 2:
2 5 1
leave 4: 1 1 0 0 1 0 0 0 0 0 0 0 0 4 3: 2 4 5 6
leave 1: 1 1 0 0 1 0 0 0 0 0 0 0 1 4 4:
visit 2: 1 1 1 0 1 0 0 0 0 0 0 0 1 4 3 4 5: 2
leave 2: 1 1 1 0 1 0 0 0 0 0 0 2 1 4 6: 0 4
visit 5: 1 1 1 0 1 1 0 0 0 0 0 2 1 4 6
check 2: 1 1 1 0 1 1 0 0 0 0 0 2 1 4
leave 5: 1 1 1 0 1 1 0 0 0 0 5 2 1 4
leave 0: 1 1 1 0 1 1 0 0 0 0 5 2 1 4 0 3
check 1: 1 1 1 0 1 1 0 0 0 0 5 2 1 4
check 2: 1 1 1 0 1 1 0 0 0 0 5 2 1 4
1 2 5 6
visit 3: 1 1 1 1 1 1 0 0 0 0 5 2 1 4
check 2: 1 1 1 1 1 1 0 0 0 0 5 2 1 4
check 4: 1 1 1 1 1 1 0 0 0 0 5 2 1 4 4
check 5: 1 1 1 1 1 1 0 0 0 0 5 2 1 4
visit 6: 1 1 1 1 1 1 1 0 0 0 5 2 1 4
leave 6: 1 1 1 1 1 1 1 0 6 0 5 2 1 4
leave 3: 1 1 1 1 1 1 1 3 6 0 5 2 1 4
check 4: 1 1 1 1 1 1 0 3 6 0 5 2 1 4
check 5: 1 1 1 1 1 1 0 3 6 0 5 2 1 4 3 6 0 5 2 1 4
check 6: 1 1 1 1 1 1 0 3 6 0 5 2 1 4
36
Topological sort in a DAG: correctness proof
public TopologicalSorter(Digraph G)
{
Proof by induction:
marked = new boolean[G.V()];
ts = new int[G.V()];
Q. How to tell whether the digraph has a cycle (is not a DAG)?
A. Use TopologicalSorter (exercise)
37
Topological sort applications.
• Causalities.
• Compilation units.
• Class inheritance.
• Course prerequisites.
• Deadlocking detection.
• Temporal dependencies.
• Pipeline of computing jobs.
• Check for symbolic link loop.
• Evaluate formula in spreadsheet.
• Program Evaluation and Review Technique / Critical Path Method
38
Topological sort application (weighted DAG)
A B G H I
0 4 C 4 6 0
2
vertices labelled
A-I in topological order
39
Program Evaluation and Review Technique / Critical Path Method
PERT/CPM algorithm.
• compute topological order of vertices.
• initialize fin[v] = 0 for all vertices v.
• consider vertices v in topologically sorted order.
for each edge vw, set fin[w]= max(fin[w], fin[v] + time[w])
13
10
F
D
critical 6 15 3
path E
5 25
4 19 25 13
A B 6 G H I
0 4 C 4 6 0
2
Critical path
• remember vertex that set value.
• work backwards from sink
40
digraph search
transitive closure
topological sort
strong components
41
Strong connectivity in digraphs
0 0
6 6
7 8 7 8
1 2 1 2
9 10 9 10
3 4 3
4
11 12 11 12
5 5
Connectivity table (easy to compute with DFS) Strong connectivity table (how to compute?)
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12
cc 0 0 0 0 0 0 0 1 1 2 2 2 2 sc 2 1 2 2 2 2 2 3 3 0 0 0 0
How difficult?
1) any COS126 student could do it
2) need to be a typical diligent COS226 student
3) hire an expert
4) intractable
5) no one knows
6) impossible
43
Typical strong components applications
Internet explorer
Strong component: subset with common energy flow Strong component: subset of mutually interacting modules
• source in kernel DAG: needs outside energy? • approach 1: package strong components together
• sink in kernel DAG: heading for growth? • approach 2: use to improve design!
44
Strong components algorithms: brief history
GR
Single-source
DFS
reachability
topological sort
DFS
(DAG)
Kosaraju
strong components
DFS (twice)
47