Professional Documents
Culture Documents
3 4
Some Digraph Problems Digraph Representation
Topological sort. Can you draw the graph so that all edges point Orientation of edge matters.
from left to right?
0
PERT/CPM. Given a set of tasks with precedence constraints, 7 8
5 6
Adjacency matrix representation. Adjacency list representation. Vertex indexed array of lists.
! Two-dimensional V ! V boolean array.
! Edge v"w in graph: adj[v][w] = true.
to
0 0 1 2 3 4 5 6 7 8 9 10 11 12 0 0: 5 2 1 6
0 0 1 1 0 0 1 1 0 0 0 0 0 0 1:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 2:
1 2 6 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 6 3:
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 4: 3
from 5 0 0 0 1 1 0 0 0 0 0 0 0 0 5: 4 3
3 4 3 4
6 0 0 0 0 1 0 0 0 0 0 0 0 0
6: 4
7 0 0 0 0 0 0 0 0 1 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0 0 7: 8
5 5
9 0 0 0 0 0 0 0 0 0 0 1 1 1 8:
9 10 9 10
10 0 0 0 0 0 0 0 0 0 0 0 0 0 9: 10 11 12
11 0 0 0 0 0 0 0 0 0 0 0 0 1
10:
12 0 0 0 0 0 0 0 0 0 0 0 0 0
7 8 11 12 7 8 11 12 11: 12
12:
7 8
Adjacency List: Java Implementation Digraph Representations
Implementation. Same as Graph, but only insert one copy of each edge. Digraphs are abstract mathematical objects.
! ADT implementation requires specific representation.
! Efficiency depends on matching algorithms to representations.
public class Digraph {
private int V;
private Sequence<Integer>[] adj;
Edge from Iterate over
Representation Space
v to w? edges leaving v?
public Digraph(int V) {
this.V = V;
List of edges E+V E E
adj = (Sequence<Integer>[]) new Sequence[V];
for (int v = 0; v < V; v++) Adjacency matrix V2 1 V
adj[v] = new Sequence<Integer>();
} Adjacency list E+V outdegree(v) outdegree(v)
E is proportional to V
9 10
Reachability
Digraph Search Goal. Find all vertices reachable from s along a directed path.
11 12
Depth First Search Depth First Search
Depth first search. Same as for undirected graphs. Remark. Same as undirected version, except Digraph instead of Graph.
13 14
Dead code elimination. Find (and remove) code blocks that are
unreachable during execution.
Mark-sweep algorithm. [McCarthy, 1960]
dead code can arise from compiler optimization
(or careless programmer) !Mark: run DFS from roots to mark live objects.
!Sweep: if object is unmarked, it is garbage, so add to free list.
15 16
Depth First Search Breadth First Search
DFS enables direct solution of simple digraph problems. Shortest path. Find the shortest directed path from s to t.
!Reachability.
!Cycle detection. BFS. Analogous to BFS in undirected graphs.
!Topological sort.
!Transitive closure.
!Find path from s to t.
17 18
19 20
Transitive Closure
Transitive closure
21 22
Transitive closure. Is there a directed path from v to w? Implementation. Use an array of DFSearcher objects.
Remark. Directed problem is harder than undirected one. public boolean reachable(int v, int w) {
return tc[v].isReachable(w);
Open research problem. O(1) query, O(V2) preprocessing time. }
} is w reachable from v?
23 24
Topological Sort
25 26
Scheduling. Given a set of tasks to be completed with precedence Topologically sort a DAG.
constraints, in what order should we schedule the tasks? ! Run DFS.
! Reverse postorder numbering yields a topological sort.
Graph model.
! Create a vertex v for each task.
! Create an edge v"w if task v must precede task w. Pf of correctness. When DFS backtracks from a vertex v, A
! Schedule tasks in topological order. all vertices reachable from v have already been explored.
D
Running time. O(E + V).
0. read programming assignment
1. download files E
2. write code
no back edges in DAG
…
12. sleep F
27 28
Topological Sort: Java Implementation Topological Sort: Applications
29 30
Program Evaluation and Review Technique / Critical Path Method Program Evaluation and Review Technique / Critical Path Method
fin[v] 0 4 6 19 25 25
A B C G H I A B C G H I
time[v] 0 4 2 4 6 0 time[v] 0 4 2 4 6 0
31 32
Terminology
33 34
Def. Vertices v and w are strongly connected if there is a path Observation 1. If you run DFS from a vertex in sink strong component,
from v to w and a path from w to v. all reachable vertices constitute a strong component.
Properties. Symmetric, transitive, reflexive. Observation 2. If you run DFS on G, the node with the highest postorder
number is in source strong component.
in order of completion
Strong component. Maximal subset of strongly connected vertices. of recursive calls
Observation 3. If you run DFS on GR, the node with the highest
Brute force. O(EV) time using transitive closure. postorder number is in sink strong component.
0 2 3
7 8
4 5 6
9 10
1 11 12 sink
kernel DAG
35 36
Kosaraju's Algorithm Ecological Food Web
37 38