You are on page 1of 10

Directed Graphs

Directed Graphs Digraph. Set of objects with oriented pairwise connections.

Ex. One-way street, hyperlink.

Reference: Chapter 19, Algorithms in Java, 3rd Edition, Robert Sedgewick

Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 2

Digraph Applications Ecological Food Web

Food web graph.


Digraph Vertex Edge
! Vertex = species.
financial stock, currency transaction
! Edge = from prey to predator.
transportation street intersection, airport highway, airway route

scheduling task precedence constraint

WordNet synset hypernym

Web web page hyperlink

game board position legal move

telephone person placed call

food web species predator-prey relation

infectious disease person infection

citation journal article citation

object graph object pointer

inheritance hierarchy class inherits from

control flow code block jump http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff

3 4
Some Digraph Problems Digraph Representation

Transitive closure. Is there a directed path from v to w? Vertex names.


! This lecture: use integers between 0 and V-1.
Strong connectivity. Are all vertices mutually reachable? ! Real world: convert between names and integers with symbol table.

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

what is the earliest that we can complete each task?


1 2 6 9 10

Shortest path. Given a weighted graph, find best route from s to t?


3 4 11 12
PageRank. What is the importance of a web page?
5

5 6

Adjacency Matrix Representation Adjacency List Representation

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)

public void insert(int v, int w) {


adj[v].add(w);
}
Digraphs in practice. # use adjacency list representation
public Iterable<Integer> adj(int v) {
return adj[v]; ! Bottleneck is iterating over edges leaving v.
}
! Real world digraphs are sparse.
}

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.

DFS (to visit a vertex v) public class DFSearcher {


private boolean[] marked;
Mark v as visited.
Visit all unmarked vertices w adjacent to v. public DFSearcher(Digraph G, int s) {
marked = new boolean[G.V()];
dfs(G, s);
}

private void dfs(Digraph G, int v) {


marked[v] = true;
Running time. O(E) since each edge examined at most once. for (int w : G.adj(v))
if (!marked[w]) dfs(G, w);
}

public boolean isReachable(int v) { return marked[v]; }

13 14

Control Flow Graph Mark-Sweep Garbage Collector

Control-flow graph. Roots. Objects known to be accessible by program (e.g., stack).


! Vertex = basic block (straight-line program).
! Edge = jump. Live objects. Objects that the program could get to by starting at a
root and following a chain of pointers.
easy to identify pointers in type-safe language

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.

Infinite loop detection. Exit block is unreachable from entry block.


Caveat. Not all infinite loops are detectable. Extra memory. Uses 1 extra mark bit per object, plus DFS stack.

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.

Basis for solving difficult digraph problems.


! Directed Euler path.
! Strong connected components.

17 18

Application: Web Crawler Web Crawler: Java Implementation

Web graph. Vertex = website, edge = hyperlink.


Queue<String> q = new Queue<String>(); queue of sites to crawl
SET<String> visited = new SET<String>(); set of visited sites
Goal. Crawl Internet, starting from some root website. String s = "http://www.princeton.edu";
Solution. BFS with implicit graph. q.enqueue(s);
start crawling from s
visited.add(s);
while (!q.isEmpty()) {
BFS. String v = q.dequeue();
Start at some root website, say http://www.princeton.edu. System.out.println(v);
! read in raw html
In in = new In(v); http://xxx.yyy.zzz
!Maintain a Queue of websites to explore. String input = in.readAll();
!Maintain a SET of discovered websites. String regexp = "http://(\\w+\\.)*(\\w+)";
Pattern pattern = Pattern.compile(regexp);
!Dequeue the next website, and enqueue websites to which it links
Matcher matcher = pattern.matcher(input);
(provided you haven't done so before). while (matcher.find()) { search using regular expression
String w = matcher.group();
if (!visited.contains(w)) {
visited.add(w); if unvisited, mark as visited
Q. Why not use DFS? q.enqueue(w); and put on queue
}
}
}

19 20
Transitive Closure

Transitive Closure Transitive closure. Is there a directed path from v to w?

Transitive closure

tc[v][w] = 1 iff path from v to w

21 22

Transitive Closure Transitive Closure: Java Implementation

Transitive closure. Is there a directed path from v to w? Implementation. Use an array of DFSearcher objects.

Lazy. Run separate DFS for each query.


Eager. Run DFS from every vertex v; store results.
public class TransitiveClosure {
private DFSearcher[] tc;
Method Preprocess Query Space
public TransitiveClosure(Digraph G) {
DFS (lazy) 1 E+V E+V tc = new Reachability[G.V()];
DFS (eager) EV 1 V2 for (int v = 0; v < G.V(); v++)
tc[v] = new Reachability(G, v);
}

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

Topological Sort DAG. Directed acyclic graph.

Topological sort. Redraw DAG so all edges point left to right.

Observation. Not possible if graph has a directed cycle.

25 26

Application: Scheduling Topological Sort: DFS

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

Q. If not a DAG, how would you identify a cycle? DFS tree

27 28
Topological Sort: Java Implementation Topological Sort: Applications

public class TopologicalSorter { Topological sort applications.


private int count; ! Causalities.
private boolean[] visited; ! Compilation units.
private int[] ts;
! Class inheritance.
public TopologicalSorter(Digraph G) { ! Course prerequisites.
visited = new boolean[G.V()];
! Deadlocking detection.
ts = new int[G.V()];
count = G.V(); ! Temporal dependencies.
for (int v = 0; v < G.V(); v++) ! Pipeline of computing jobs.
if (!visited[v]) tsort(G, v);
! Check for symbolic link loop.
}
! Evaluate formula in spreadsheet.
private void tsort(Digraph G, int v) {
visited[v] = true;
for (int w : G.adj(v))
if (!visited[w]) tsort(G, w);
}
ts[--count] = v;
} assign numbers in reverse DFS postorder
}

29 30

Program Evaluation and Review Technique / Critical Path Method Program Evaluation and Review Technique / Critical Path Method

PERT/CPM. index task time prereq PERT/CPM algorithm.


! Task v takes time[v] units of time. A begin 0 - ! Compute topological order of vertices.
! Can work on jobs in parallel. B framing 4 A ! Initialize fin[v] = 0 for all vertices v.
! Precedence constraints: must finish C roofing 2 B ! Consider vertices v in topological order.
task v before beginning task w. D siding 6 B – for each edge v"w, set fin[w]= max(fin[w], fin[v] + time[w])
windows
! What's earliest we can finish each task? E 5 D
F plumbing 3 D
G electricity 4 C, E
H paint 6 C, E
I finish 0 F, H 13
10
F F
D D
3 3
6 6 15
E E
critical path
5 5

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

Strongly Connected Components

33 34

Strong Components Computing Strongly Connected Components

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

Kosaraju's algorithm. Ecological food web.


! Run DFS on GR and compute postorder. ! Vertex = species.
! Run DFS on G, considering vertices in reverse postorder. ! Edge = from producer to consumer.
! Strong component = subset of species for which energy flows
from one another and back.

Theorem. Trees in second DFS are strong components. (!)


http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff

37 38

You might also like