42DirectedGraphs
42DirectedGraphs
vertex of
outdegree 4
and indegree 2
0
6 8 7
directed path
from 0 to 2
1 2
3 9 10
4 directed cycle
5
11 12
3
Road network
To see all the details that are visible on the screen,use the
Address Holland Tunnel "Print" link next to the map.
New York, NY 10013
Vertex = intersection; edge = one-way street.
4
Political blogosphere graph
The Political Blogosphere and the 2004 U.S. Election: Divided They Blog, Adamic and Glance, 2005
Figure 1: Community structure of political blogs (expanded set), shown using utilizing a GEM
layout [11] in the GUESS[3] visualization and analysis tool. The colors reflect political orientation, 5
red for conservative, and blue for liberal. Orange links go from liberal to conservative, and purple
Overnight interbank loan graph
GSCC
GWCC
GIN
GOUT
DC
Tendril
!"#$%& '( !&)&%*+ ,$-). -&/01%2 ,1% 3&4/&56&% 7'8 799:; <=>> ? #"*-/ 0&*2+@ A1--&A/&) A1541-&-/8
The Topology
B> ? )".A1--&A/&) A1541-&-/8 <3>> of?
the Federal
#"*-/ Funds
./%1-#+@ Market, A1541-&-/8
A1--&A/&) Bech and Atalay,
<CD ? 2008
#"*-/ "-EA1541-&-/8
<FGH ? #"*-/ 1$/E A1541-&-/; F- /I". )*@ /I&%& 0&%& JK -1)&. "- /I& <3>>8 L9L -1)&. "- /I& <CD8 :K
6
-1)&. "- <FGH8 J9 -1)&. "- /I& /&-)%"+. *-) 7 -1)&. "- * )".A1--&A/&) A1541-&-/;
Implication graph
if x5 is true,
then x0 is true
~x2 x0
x6 ~x4 x3
~x5 ~x1 x1 x5
~x3 x4 ~x6
~x0 x2
7
Combinational circuit
8
WordNet graph
event
group_ac on
damage harm ..
impairment transi on increase forfeit forfeiture ac on
demo on varia on
mo on movement move
9
The McChrystal Afghanistan PowerPoint slide
http://www.guardian.co.uk/news/datablog/2010/apr/29/mcchrystal-afghanistan-powerpoint-slide
10
Digraph applications
11
Some digraph problems
s
Path. Is there a directed path from s to t ?
Topological sort. Can you draw a digraph so that all edges point upwards?
tinyDG.txt
% java Digraph tinyDG.txt
V 0->5 5 1
13
E 0->1
22
4 2 2->0
2 3 adj[] 0 3
3 2
2->3
0
6 0 3->5
0 1 1 5 2
3->2
2 0 2
11 12
4->3 3 2
3
12 9 4->2
9 10 4 5->4 4
9 11 5
7 9
⋮
10 12 6 9 4 8 0
11->4
11 4 7 11->12
4 3 6 9
8 12-9
3 5
6 8 9
6
8 6 10
⋮5 4 11 11 10
0 5
6 4 12
6 9=
In in new In(args[0]); 12
read digraph from
7 6
Digraph G = new Digraph(in); input stream
4 12
public Graph(int V)
{ create empty graph
with V vertices
this.V = V;
adj = (Bag<Integer>[]) new Bag[V];
for (int v = 0; v < V; v++)
adj[v] = new Bag<Integer>();
}
public Digraph(int V)
{ create empty digraph
with V vertices
this.V = V;
adj = (Bag<Integer>[]) new Bag[V];
for (int v = 0; v < V; v++)
adj[v] = new Bag<Integer>();
}
}
iterator for vertices
public Iterable<Integer> adj(int v) pointing from v
{ return adj[v]; }
}
19
Digraph representations
list of edges E 1 E E
adjacency matrix V2 1† 1 V
20
4.2 D IRECTED G RAPHS
‣ introduction
‣ digraph API
‣ digraph search
Algorithms
‣ topological sort
23
Depth-first search in digraphs
Mark v as visited.
Recursively visit all unmarked
vertices w pointing from v.
24
Depth-first search demo
1 2 9→11
8→9
10→12
11→4
3 9 10 4→3
3→5
4 6→8
8→6
5 5→4
11 12
0→5
6→4
a directed graph 6→9
7→6
25
Depth-first search demo
To visit a vertex v :
Mark vertex v as visited.
Recursively visit all unmarked vertices pointing from v.
0
v marked[] edgeTo[]
6 8 7 0 T –
1 T 0
1 2 reachable 2 T 3
from vertex 0 3 T 4
4 T 5
5 T 0
3 9 10 6 F –
7 F –
4 8 F –
9 F –
5 10 F –
11 12
11 F –
12 F –
reachable from 0
26
Depth-first search (in undirected graphs)
27
Depth-first search (in directed graphs)
28
Reachability application: program control-flow analysis
Dead-code elimination.
Find (and remove) unreachable code.
Infinite-loop detection.
Determine whether exit is unreachable.
29
Reachability application: mark-sweep garbage collector
roots
30
Reachability application: mark-sweep garbage collector
Memory cost. Uses 1 extra mark bit per object (plus DFS stack).
roots
31
Depth-first search in digraphs summary
SIAM J. COMPUT.
Vol. 1, No. 2, June 1972
tinyDG2.txt
V
0 2 6 E
8
5 0
2 4
1 3 2
1 2
0 1
3 4 3
5 4 3 5
0 2
graph G
34
Directed breadth-first search demo
0 2
v edgeTo[] distTo[]
0 – 0
1 0 1
1 2 0 1
3 4 3
4 2 2
3 5 3 4
5 4
done
35
Multiple-source shortest paths
tinyDG.txt
V
Multiple-source shortest paths.13Given a digraph and a set of source
E
22
vertices, find shortest path from any vertex in the set to each other vertex.
4 2
2 3 adj
Ex. S = { 1, 7, 10 }. 3 2 0
6 0
Shortest path to 4 is 7→6→4. 1
0 1
Shortest path to 5 is 7→6→0→5.
2 0 2
11 12
Shortest path to 12 is 10→12. 3
12 9
… 9 10 4
9 11 5
7 9
10 12 6
11 4 7
4 3
8
3 5
6 8 9
8 6 10
5 4
Q. How to implement multi-source shortest paths algorithm? 11
0 5
A. Use BFS, but initialize by enqueuing
6 4 all source vertices. 12
6 9 36
7 6
Breadth-first search in digraphs application: web crawler
Goal. Crawl web, starting from some root web page, say www.princeton.edu.
25
0 34
Solution. [BFS with implicit digraph]
7
Choose root web page as source s. 10
2
40
18 6 21
24
4
38
3 17
35
36
46
Q. Why not use DFS? 20
while (!queue.isEmpty())
{
String v = queue.dequeue();
read in raw html from next
StdOut.println(v);
website in queue
In in = new In(v);
String input = in.readAll();
0. Algorithms 0
1. Complexity Theory
2. Artificial Intelligence 2 5 1
3. Intro to CS
4. Cryptography 3 4
5. Scientific Computing
6. Advanced Programming 6
feasible schedule 41
Topological sort
0→5 0→2 0
0→1 3→6
3→5 3→4 2 5 1
5→2 6→4
3 4
6→0 3→2
1→4 6
0
0→5
0→2
0→1
3→6
2 5 1 3→5
3→4
5→2
6→4
3 4
6→0
3→2
1→4
6
43
Topological sort demo
postorder
4 1 2 5 0 6 3
2 5 1 topological order
3 6 0 5 2 1 4
3 4
done
44
Depth-first search order
public DepthFirstOrder(Digraph G)
{
reversePost = new Stack<Integer>();
marked = new boolean[G.V()];
for (int v = 0; v < G.V(); v++)
if (!marked[v]) dfs(G, v);
}
45
Topological sort in a DAG: correctness proof
marked[]
0 1 2 3 4
dfs(0)
dfs(5) 1 0 0 0 0
dfs(4) 1 0 0 0 0
dfs(3) 1 0 0 0 1
check 5 1 0 0 1 1
http://xkcd.com/754
49
Directed cycle detection application: spreadsheet recalculation
Microsoft Excel does cycle detection (and has a circular reference toolbar!)
50
4.2 D IRECTED G RAPHS
‣ introduction
‣ digraph API
‣ digraph search
Algorithms
‣ topological sort
Def. Vertices v and w are strongly connected if there is both a directed path
from v to w and a directed path from w to v.
53
A digraph and its strong components
Connected components vs. strongly-connected components
v and w are connected if there is v and w are strongly connected if there is both a directed
a path between v and w path from v to w and a directed path from w to v
connected component id (easy to compute with DFS) strongly-connected component id (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 1 1 1 2 2 2 2 scc[] 1 0 1 1 1 1 3 4 3 2 2 2 2
http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.gif
how to compute?
Idea.
Compute topological order (reverse postorder) in kernel DAG.
Run DFS, considering vertices in reverse topological order.
6 8 7
1 2
3 9 10
5
11 12
digraph G
59
Kosaraju-Sharir algorithm demo
6 8 7
1 2
3 9 10
5
11 12
reverse digraph GR
60
Kosaraju-Sharir algorithm demo
0
v scc[]
6 8 7 0 1
1 0
1 2 2 1
3 1
4 1
5 1
3 9 10 6 3
7 4
4 8 3
9 2
5 10 2
11 12
11 2
12 2
done
61
Kosaraju-Sharir algorithm
check unmarked vertices in the order reverse postorder for use in second dfs()
0 1 2 3 4 5 6 7 8 9 10 11 12 1 0 2 4 5 3 11 9 12 10 6 7 8
dfs(0)
dfs(6)
dfs(8)
check 6
8 done
dfs(7)
7 done
6 done
dfs(2)
dfs(4)
dfs(11)
dfs(9)
dfs(12)
check 11
dfs(10)
check 9
10 done
12 done
check 7
check 6
... 9 done 62
11 done
check 6
Kosaraju-Sharir algorithm
63
Kosaraju-Sharir algorithm
Pf.
Running time: bottleneck is running DFS twice (and computing GR).
Correctness: tricky, see textbook (2nd printing).
Implementation: easy!
64
Connected components in an undirected graph (with DFS)
public class CC
{
private boolean marked[];
private int[] id;
private int count;
public CC(Graph G)
{
marked = new boolean[G.V()];
id = new int[G.V()];
public KosarajuSharirSCC(Digraph G)
{
marked = new boolean[G.V()];
id = new int[G.V()];
DepthFirstOrder dfs = new DepthFirstOrder(G.reverse());
for (int v : dfs.reversePost())
{
if (!marked[v])
{
dfs(G, v);
count++;
}
}
}
single-source
reachability DFS
in a digraph
topological sort
DFS
in a DAG
0
6
7 8
strong 1 2 Kosaraju-Sharir
components 9 10
DFS (twice)
in a digraph 3
4
11 12
5
67
4.2 D IRECTED G RAPHS
‣ introduction
‣ digraph API
‣ digraph search
Algorithms
‣ topological sort