algorithms - umd
TRANSCRIPT
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
http://algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
4.2 DIRECTED GRAPHS
‣ introduction
‣ digraph API
‣ digraph search
‣ topological sort
‣ strong components
Digraph. Set of vertices connected pairwise by directed edges.
directedcycle
directed pathfrom 0 to 2
vertex of outdegree 4
and indegree 2
3
Directed graphs
4
Road network
Vertex = intersection; edge = one-way street.Address Holland Tunnel
New York, NY 10013
©2008 Google - Map data ©2008 Sanborn, NAVTEQ™ - Terms of Use
To see all the details that are visible on the screen,use the"Print" link next to the map.
11
Digraph applications
digraph vertex directed edge
transportation street intersection one-way street
web web page hyperlink
food web species predator-prey relationship
WordNet synset hypernym
scheduling task precedence constraint
financial bank transaction
cell phone person placed call
infectious disease person infection
game board position legal move
citation journal article citation
object graph object pointer
inheritance hierarchy class inherits from
control flow code block jump
15
Digraph API
public class Digraph public class Digraph
Digraph(int V)Digraph(int V) create an empty digraph with V vertices
Digraph(In in)Digraph(In in) create a digraph from input stream
void addEdge(int v, int w)addEdge(int v, int w) add a directed edge v→w
Iterable<Integer> adj(int v)adj(int v) vertices pointing from v
int V()V() number of vertices
int E()E() number of edges
Digraph reverse()reverse() reverse of this digraph
String toString()toString() string representation
In in = new In(args[0]);Digraph G = new Digraph(in);
for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "->" + w);
read digraph from input stream
print out each edge (once)
Maintain vertex-indexed array of lists.
adj[]0
1
2
3
4
5
6
7
8
9
10
11
12
5 2
3 2
0 3
5 1
6
0
11 10
6 9
4 12
4
12
9
9 4 8
1322 4 2 2 3 3 2 6 0 0 1 2 011 1212 9 9 10 9 11 7 910 1211 4 4 3 3 5 6 8 8 6 5 4 0 5 6 4 6 9 7 6
tinyDG.txtV
E
17
Adjacency-lists digraph representation
adj[]0
1
2
3
4
5
6
7
8
9
10
11
12
5 2
3 2
0 3
5 1
6
0
11 10
6 9
4 12
4
12
9
9 4 8
1322 4 2 2 3 3 2 6 0 0 1 2 011 1212 9 9 10 9 11 7 910 1211 4 4 3 3 5 6 8 8 6 5 4 0 5 6 4 6 9 7 6
tinyDG.txtV
E
18
Adjacency-lists graph representation (review): Java implementation
public class Graph{ private final int V; private final Bag<Integer>[] adj;
public Graph(int V) { this.V = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Integer>(); }
public void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); }
public Iterable<Integer> adj(int v) { return adj[v]; }}
adjacency lists
create empty graphwith V vertices
iterator for vertices adjacent to v
add edge v–w
19
Adjacency-lists digraph representation: Java implementation
public class Digraph{ private final int V; private final Bag<Integer>[] adj;
public Digraph(int V) { this.V = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Integer>(); }
public void addEdge(int v, int w) { adj[v].add(w);
}
public Iterable<Integer> adj(int v) { return adj[v]; }}
adjacency lists
create empty digraphwith V vertices
add edge v→w
iterator for vertices pointing from v
In practice. Use adjacency-lists representation.
・Algorithms based on iterating over vertices pointing from v.
・Real-world digraphs tend to be sparse.
20
Digraph representations
representation space insert edgefrom v to w
edge fromv to w?
iterate over vertices pointing from v?
list of edges E 1 E E
adjacency matrix V2 1† 1 V
adjacency lists E + V 1 outdegree(v) outdegree(v)
huge number of vertices,small average vertex degree
† disallows parallel edges
Same method as for undirected graphs.
・Every undirected graph is a digraph (with edges in both directions).
・DFS is a digraph algorithm.
24
Depth-first search in digraphs
Mark v as visited.Recursively visit all unmarked
vertices w pointing from v.
DFS (to visit a vertex v)
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices pointing from v.
1
4
9
2
5
3
0
1211
10
Depth-first search demo
25
a directed graph
4→2
2→3
3→2
6→0
0→1
2→0
11→12
12→9
9→10
9→11
8→9
10→12
11→4
4→3
3→5
6→8
8→6
5→4
0→5
6→4
6→9
7→6
1
4
9
2
5
3
0
1211
10
8 76
To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices pointing from v.
TT TTT
TFFFF
FFF
marked[]
1
9
2
5
3
0
1211
10
8 76
Depth-first search demo
26
reachable from 0
reachablefrom vertex 0
0 1 234
56789
101112
–0 345
0––––
–––
v edgeTo[]
4
Code for directed graphs identical to undirected one.
[substitute Digraph for Graph]
public class DirectedDFS{ private boolean[] marked;
public DirectedDFS(Digraph G, int s) { marked = new boolean[G.V()]; dfs(G, s); }
private void dfs(Digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); }
public boolean visited(int v) { return marked[v]; }}
28
Depth-first search (in directed graphs)
true if path from s
constructor marks vertices reachable from s
recursive DFS does the work
client can ask whether any vertex is reachable from s
31
Reachability application: mark-sweep garbage collector
Mark-sweep algorithm. [McCarthy, 1960]
・Mark: mark all reachable objects.
・Sweep: if object is unmarked, it is garbage (so add to free list).
Memory cost. Uses 1 extra mark bit per object (plus DFS stack).
roots
DFS enables direct solution of simple digraph problems.
・Reachability.
・Path finding.
・Topological sort.
・Directed cycle detection.
Basis for solving difficult digraph problems.
・2-satisfiability.
・Directed Euler path.
・Strongly-connected components.
32
Depth-first search in digraphs summary
✓
SIAM J. COMPUT.Vol. 1, No. 2, June 1972
DEPTH-FIRST SEARCH AND LINEAR GRAPH ALGORITHMS*
ROBERT TARJAN"
Abstract. The value of depth-first search or "bacltracking" as a technique for solving problems isillustrated by two examples. An improved version of an algorithm for finding the strongly connectedcomponents of a directed graph and ar algorithm for finding the biconnected components of an un-direct graph are presented. The space and time requirements of both algorithms are bounded byk1V + k2E d- k for some constants kl, k2, and ka, where Vis the number of vertices and E is the numberof edges of the graph being examined.
Key words. Algorithm, backtracking, biconnectivity, connectivity, depth-first, graph, search,spanning tree, strong-connectivity.
1. Introduction. Consider a graph G, consisting of a set of vertices U and aset of edges g. The graph may either be directed (the edges are ordered pairs (v, w)of vertices; v is the tail and w is the head of the edge) or undirected (the edges areunordered pairs of vertices, also represented as (v, w)). Graphs form a suitableabstraction for problems in many areas; chemistry, electrical engineering, andsociology, for example. Thus it is important to have the most economical algo-rithms for answering graph-theoretical questions.
In studying graph algorithms we cannot avoid at least a few definitions.These definitions are more-or-less standard in the literature. (See Harary [3],for instance.) If G (, g) is a graph, a path p’v w in G is a sequence of verticesand edges leading from v to w. A path is simple if all its vertices are distinct. A pathp’v v is called a closed path. A closed path p’v v is a cycle if all its edges aredistinct and the only vertex to occur twice in p is v, which occurs exactly twice.Two cycles which are cyclic permutations of each other are considered to be thesame cycle. The undirected version of a directed graph is the graph formed byconverting each edge of the directed graph into an undirected edge and removingduplicate edges. An undirected graph is connected if there is a path between everypair of vertices.
A (directed rooted) tree T is a directed graph whose undirected version isconnected, having one vertex which is the head of no edges (called the root),and such that all vertices except the root are the head of exactly one edge. Therelation "(v, w) is an edge of T" is denoted by v- w. The relation "There is apath from v to w in T" is denoted by v w. If v - w, v is the father ofw and w is ason of v. If v w, v is an ancestor ofw and w is a descendant of v. Every vertex is anancestor and a descendant of itself. If v is a vertex in a tree T, T is the subtree of Thaving as vertices all the descendants of v in T. If G is a directed graph, a tree Tis a spanning tree of G if T is a subgraph of G and T contains all the vertices of G.
If R and S are binary relations, R* is the transitive closure of R, R-1 is theinverse of R, and
RS {(u, w)lZlv((u, v) R & (v, w) e S)}.
* Received by the editors August 30, 1971, and in revised form March 9, 1972.
" Department of Computer Science, Cornell University, Ithaca, New York 14850. This researchwas supported by the Hertz Foundation and the National Science Foundation under Grant GJ-992.
146
Same method as for undirected graphs.
・Every undirected graph is a digraph (with edges in both directions).
・BFS is a digraph algorithm.
Proposition. BFS computes shortest paths (fewest number of edges)
from s to all other vertices in a digraph in time proportional to E + V.
33
Breadth-first search in digraphs
Put s onto a FIFO queue, and mark s as visited.Repeat until the queue is empty:
- remove the least recently added vertex v
- for each unmarked vertex pointing from v:
add to queue and mark as visited.
BFS (from source vertex s)
Repeat until queue is empty:
・Remove vertex v from queue.
・Add to queue all unmarked vertices pointing from v and mark them.
Directed breadth-first search demo
34
graph G
0
4
2
1
5
3
0
4
2
1
5
3
685 02 43 21 20 14 33 50 2
tinyDG2.txtV
E
Repeat until queue is empty:
・Remove vertex v from queue.
・Add to queue all unmarked vertices pointing from v and mark them.
Directed breadth-first search demo
35
done
0
4
2
1
5
3
0 1 234
5
v edgeTo[] distTo[]
–0 042
3
01 132
4
42
Topological sort
DAG. Directed acyclic graph.
Topological sort. Redraw DAG so all edges point upwards.
Solution. DFS. What else?
directed edges
0→5 0→2
0→1 3→6
3→5 3→4
5→4 6→4
6→0 3→2
1→4
DAG
0
1
4
52
6
3
topological order
・Run depth-first search.
・Return vertices in reverse postorder.
Topological sort demo
44
4 1 2 5 0 6 3
postorder
done
0
1
4
52
6
3
0
1
4
52
6
3
3 6 0 5 2 1 4
topological order
45
Depth-first search order
public class DepthFirstOrder{ private boolean[] marked; private Stack<Integer> reversePost;
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); }
private void dfs(Digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); reversePost.push(v); } public Iterable<Integer> reversePost() { return reversePost; }}
returns all vertices in“reverse DFS postorder”
3
Given. Undirected graph G with positive edge weights (connected).
Def. A spanning tree of G is a subgraph T that is connected and acyclic.
Goal. Find a min weight spanning tree.
Minimum spanning tree
graph G
23
10
21
14
244
189
7
118
6
165
5
Given. Undirected graph G with positive edge weights (connected).
Def. A spanning tree of G is a subgraph T that is connected and acyclic.
Goal. Find a min weight spanning tree.
Minimum spanning tree
not acyclic
23
10
21
14
24
16
4
189
7
118
5
6
6
Given. Undirected graph G with positive edge weights (connected).
Def. A spanning tree of G is a subgraph T that is connected and acyclic.
Goal. Find a min weight spanning tree.
Brute force. Try all spanning trees?
Minimum spanning tree
spanning tree T: cost = 50 = 4 + 6 + 8 + 5 + 11 + 9 + 7
23
10
21
14
24
16
4
189
7
118
6
5
25
Weighted edge API
Edge abstraction needed for weighted edges.
Idiom for processing an edge e: int v = e.either(), w = e.other(v);
public class Edge implements Comparable<Edge> public class Edge implements Comparable<Edge> public class Edge implements Comparable<Edge>
Edge(int v, int w, double weight) create a weighted edge v-w
int either() either endpoint
int other(int v) the endpoint that's not v
int compareTo(Edge that) compare this edge to that edge
double weight() the weight
String toString() string representation
vweight
w
26
public class Edge implements Comparable<Edge>{ private final int v, w; private final double weight;
public Edge(int v, int w, double weight) { this.v = v; this.w = w; this.weight = weight; }
public int either() { return v; }
public int other(int vertex) { if (vertex == v) return w; else return v; }
public int compareTo(Edge that) { if (this.weight < that.weight) return -1; else if (this.weight > that.weight) return +1; else return 0; }}
Weighted edge: Java implementation
constructor
either endpoint
other endpoint
compare edges by weight
27
Conventions. Allow self-loops and parallel edges.
Edge-weighted graph API
public class EdgeWeightedGraph public class EdgeWeightedGraph
EdgeWeightedGraph(int V)EdgeWeightedGraph(int V) create an empty graph with V vertices
EdgeWeightedGraph(In in)EdgeWeightedGraph(In in) create a graph from input stream
void addEdge(Edge e)addEdge(Edge e) add weighted edge e to this graph
Iterable<Edge> adj(int v)adj(int v) edges incident to v
Iterable<Edge> edges()edges() all edges in this graph
int V()V() number of vertices
int E()E() number of edges
String toString()toString() string representation
28
Edge-weighted graph: adjacency-lists representation
Maintain vertex-indexed array of Edge lists.
Edge-weighted graph representation
adj[]0
1
2
3
4
5
6
7
6 0 .58 0 2 .26 0 4 .38 0 7 .16 Bagobjects
8164 5 0.35 4 7 0.37 5 7 0.28 0 7 0.161 5 0.32 0 4 0.382 3 0.171 7 0.19 0 2 0.26 1 2 0.36 1 3 0.29 2 7 0.346 2 0.40 3 6 0.526 0 0.586 4 0.93
1 3 .29 1 2 .36 1 7 .19 1 5 .32
6 2 .40 2 7 .34 1 2 .36 0 2 .26 2 3 .17
3 6 .52 1 3 .29 2 3 .17
6 4 .93 0 4 .38 4 7 .37 4 5 .35
1 5 .32 5 7 .28 4 5 .35
6 4 .93 6 0 .58 3 6 .52 6 2 .40
2 7 .34 1 7 .19 0 7 .16 5 7 .28 5 7 .28
references to the same Edge object
tinyEWG.txtV
E
29
public class EdgeWeightedGraph{ private final int V; private final Bag<Edge>[] adj;
public EdgeWeightedGraph(int V) { this.V = V; adj = (Bag<Edge>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Edge>(); }
public void addEdge(Edge e) { int v = e.either(), w = e.other(v); adj[v].add(e); adj[w].add(e); }
public Iterable<Edge> adj(int v) { return adj[v]; }}
Edge-weighted graph: adjacency-lists implementation
add edge to both adjacency lists
constructor
same as Graph, but adjacency lists of Edges instead of integers
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
34
Kruskal's algorithm demo
5
4
7
13
0
2
6
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
graph edgessorted by weight
an edge-weighted graph
Consider edges in ascending order of weight.
・Add next edge to tree T unless doing so would create a cycle.
35
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1-7 0.19
0-2 0.26
5-7 0.28
1-3 0.29
1-5 0.32
2-7 0.34
4-5 0.35
1-2 0.36
4-7 0.37
0-4 0.38
6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
5
4
7
13
0
2
6
a minimum spanning tree
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
44
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 0.162-3 0.17
1-7 0.190-2 0.26
5-7 0.281-3 0.29
1-5 0.322-7 0.34
4-5 0.351-2 0.36
4-7 0.370-4 0.38
6-2 0.403-6 0.52
6-0 0.586-4 0.93
an edge-weighted graph
・Start with vertex 0 and greedily grow tree T.
・Add to T the min weight edge with exactly one endpoint in T.
・Repeat until V - 1 edges.
45
Prim's algorithm demo
5
4
7
13
0
2
6
0-7 1-7 0-2 2-3 5-7 4-5 6-2
MST edges
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
http://algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
4.4 SHORTEST PATHS
‣ APIs
‣ shortest-paths properties
‣ Dijkstra's algorithm
‣ edge-weighted DAGs
‣ negative weights
11
Edge-weighted digraph: adjacency-lists representation
Edge-weighted digraph representation
adj0
1
2
3
4
5
6
7
0 2 .26 0 4 .38
Bag objects
reference to aDirectedEdge
object
8154 5 0.35 5 4 0.35 4 7 0.37 5 7 0.28 7 5 0.28 5 1 0.32 0 4 0.380 2 0.26 7 3 0.39 1 3 0.29 2 7 0.346 2 0.40 3 6 0.526 0 0.586 4 0.93
1 3 .29
2 7 .34
3 6 .52
4 7 .37 4 5 .35
5 1 .32 5 7 .28 5 4 .35
6 4 .93 6 0 .58 6 2 .40
7 3 .39 7 5 .28
tinyEWD.txtV
E
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
29
0
4
7
1 3
5
2
6
s
69
8
4
5
7
1
54
15
312
20
13
11
9
an edge-weighted digraph
0→1 5.0
0→4 9.0
0→7 8.0
1→2 12.0
1→3 15.0
1→7 4.0
2→3 3.0
2→6 11.0
3→6 9.0
4→5 4.0
4→6 20.0
4→7 5.0
5→2 1.0
5→6 13.0
7→5 6.0
7→2 7.0
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
30
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
shortest-paths tree from vertex s
s
37
Depends on PQ implementation: V insert, V delete-min, E decrease-key.
Bottom line.
・Array implementation optimal for dense graphs.
・Binary heap much faster for sparse graphs.
・4-way heap worth the trouble in performance-critical situations.
・Fibonacci heap best in theory, but not worth implementing.
Dijkstra's algorithm: which priority queue?
† amortized
PQ implementation insert delete-min decrease-key total
unordered array 1 V 1 V2
binary heap log V log V log V E log V
d-way heap(Johnson 1975)
d logd V d logd V logd V E logE/V V
Fibonacci heap(Fredman-Tarjan 1984)
1 † log V † 1 † E + V log V
65
Single source shortest-paths implementation: cost summary
Remark 1. Directed cycles make the problem harder.
Remark 2. Negative weights make the problem harder.
Remark 3. Negative cycles makes the problem intractable.
algorithm restriction typical case worst case extra space
topological sortno directed
cyclesE + V E + V V
Dijkstra(binary heap)
no negative
weightsE log V E log V V
Bellman-Fordno negative
E V E V V
Bellman-Ford(queue-based)
cyclesE + V E V V