1 Digraphs
DIGRAPHS
wake up eat work cs16 meditation more cs16 play cxhextris make cookies for cs16 HTA sleep dream of cs16 cs16 program A typical student day 1 2 3 4 5 6 7 8 9 10 11
Whats a Digraph? a) A small burrowing animal with long sharp teeth - - PDF document
D IGRAPHS 1 A typical student day wake up 3 2 eat cs16 meditation 5 4 work more cs16 7 play 8 cs16 program 6 cxhextris 9 make cookies for cs16 HTA 10 sleep 11 dream of cs16 Digraphs 1 Whats a Digraph? a) A small
1 Digraphs
wake up eat work cs16 meditation more cs16 play cxhextris make cookies for cs16 HTA sleep dream of cs16 cs16 program A typical student day 1 2 3 4 5 6 7 8 9 10 11
2 Digraphs
a) A small burrowing animal with long sharp teeth and a unquenchable lust for the blood of computer science majors b) A distressed graph c) A directed graph Each edge goes in one direction Edge (a,b) goes from a to b, but not b to a You’re saying, “Yo, how about an example of how we might be enlightened by the use of digraphs!!” − Well, if you insist. . . a b
3 Digraphs
Maps: digraphs handle one-way streets (especially helpful in Providence) Thayer Waterman Brook Angell
SciLi Thomas J. Watson Jr. Center for Information Technology
ME!
143
Store 24
Bookstore
D’Angelo’s!
Tunnel O’ Doom
4 Digraphs
Scheduling: edge (a,b) means task a must be completed before b can be started cs16 cs15 cs31 cs126 cs32 cs127 cs167 cs141 cs22 Old programmers never die - they just fall into black holes
5 Digraphs
dag: (noun) dÂ-g
2.“man’s best friend”
person’s
directed graph with no directed cycles a b c d e a b c d e DAG not a DAG
6 Digraphs
Same algorithm as for undirected graphs On a connected digraph, may yield unconnected DFS trees (i.e., a DFS forest) a b c d e f a b c d e f
7 Digraphs
DFS tree rooted at v: vertices reachable from v via directed paths a b c d e f c a b d e b f d c a
8 Digraphs
Each vertex can reach all other vertices a b d c e f g
9 Digraphs
a b d c e f g
10 Digraphs
Digraph G* is obtained from G using the rule: If there is a directed path in G from a to b, then add the edge (a,b) to G* G G*
11 Digraphs
We can perform DFS starting at each vertex Time: O(n(n+m)) Alternatively ... Floyd-Warshall Algorithm: If there’s a way to get from a to b, and from b to c, then there’s a way to get from a to c
12 Digraphs
JFK BOS MIA ORD LAX DFW SFO
v2 v1 v3 v4 v5 v6 v7
JFK BOS MIA ORD LAX DFW SFO
v2 v1 v3 v4 v5 v6 v7
13 Digraphs
and insertDirectedEdge take O(1) time (e.g., adjacency matrix structure) Algorithm FloydWarshall(G) let v1 ... vn be an arbitrary ordering of the vertices G0 = G for k = 1 to n do // consider all possible routing vertices vk Gk = Gk-1 for each (i, j = 1, ..., n) (i != j) (i, j != k) do // for each pair of vertices vi and vj if Gk-1.areAdjacent(vi,vk) and Gk-1.areAdjacent(vk,vj) then Gk.insertDirectedEdge(vi,vj,null) return G0
the set { v1, ..., vk }
14 Digraphs
JFK BOS MIA ORD LAX DFW SFO
v2 v1 v3 v4 v5 v6 v7
15 Digraphs
JFK BOS MIA ORD LAX DFW SFO
v2 v1 v3 v4 v5 v6 v7
16 Digraphs
For each edge (u,v), vertex u is visited before vertex v wake up eat work cs16 meditation more cs16 play cxhextris make cookies for cs16 HTA sleep dream of cs16 cs16 program A typical student day 1 2 3 4 5 6 7 8 9 10 11
17 Digraphs
18 Digraphs
Labels are increasing along a directed path A digraph has a topological sorting if and
1 2 3 4 5
19 Digraphs
method TopologicalSort if there are more vertices let v be a source; // a vertex w/o incoming edges label and remove v;
TopologicalSort;
20 Digraphs
Simulate deletion of sources using indegree counters
if v not labeled and indeg(v) = 0 then TopSort(v) TopSort(Vertex v); label v; foreach edge(v,w) indeg(w) = indeg(w) − 1; if indeg(w) = 0 TopSort(w);
21 Digraphs
A C E H G D F I B ? 0 ? 0 ? 1 ? 3 ? 1 ? 2 ? 2 ? 1 ? 3
22 Digraphs
RevTopSort(Vertex v) mark v; foreach edge(v,w) if v not marked RevTopSort(w); label v;