1、Applications of Graph Traversal,Algorithm : Design & Analysis 13,In the last class,Depth-First and Breadth-First Search Finding Connected Components General Depth-First Search Skeleton Depth-First Search Trace,Applications of Graph Traversal,Directed Acyclic Graph Topological Order Critical Path Ana
2、lysis Strongly Connected Component Strong Component and Condensation Leader of Strong Component The Algorihtm,Directed Acyclic Graph(DAG),A Directed Acyclic Graph,Not a DAG,Topological Order,G=(V,E) is a directed graph with n vertices. A topological order for G is an assignment of distinct interger
3、1,2, n to the vertices of V as their topological number, such that, for every vwE, the topological number of v is less than that of w. Reverse topological order can be defined similarily, (“greater than” ),1,2,3,4,5,6,7,8,9,Existence of Topological Order - a Negative Result,If a directed graph G has
4、 a cycle, then G has no topological order Proof By contradiction,x,y,yx-path,xy-path,For any given topological order, all the vertices on both paths must be in increasing order. Contradiction results for any assignments for x and y.,Reverse Topological Ordering using DFS Skeleton,Specialized paramet
5、ers Array topo, keeps the topological number assigned to each vertex. Counter topoNum to provide the integer to be used for topological number assignments Output Array topo as filled.,Reverse Topological Ordering using DFS Skeleton,void dfsTopoSweep(IntList adjVertices,int n, int topo)int topoNum=0F
6、or each vertex v of G, in some orderif (colorv=white)dfsTopo(adjVertices, color, v, topo, topoNum);/ Continue loopreturn;,For non-reverse topological ordering, initialized as n+1,void dfsTopo(IntList adjVertices, int color, int v, int topo, int topoNum)int w; IntList remAdj; colorv=gray; remAdj=adjV
7、erticesv;while (remAdjnil)w=first(remAdj);if (colorw=white)dfsTopo(adjVertices, color, w, topo, topoNum);remAdj=rest(remAdj);topoNum+; topov=topoNumcolorv=black;return;,Reverse Topological Ordering using DFS Skeleton,Filling topo is a post-order processing, so, the earlier discovered vertex has rela
8、tively greater topo number,Obviouly, in (m+n),Correctness of the Algorithm,If G is a DAG with n vertices, the procedure dfsTopoSweep computes a reverse topological order for G in the array topo. Proof The procedur dfsTopo is called exactly once for a vertex, so, the numbers in topo must be distinct
9、in the range 1,2,n. For any edge vw, vw cant be a back edge(otherwise, a cycle is formed). For any other edge types, we have finishTime(v)finishTime(w), so, topo(w) is assigned earlier than topo(v). Note that topoNum is incremented monotonically, so, topo(v)topo(w).,Existence of Topological Order -
10、A Better Result,In fact, the proof of correctness of topological ordering has proved that: DAG always has a topological order.So, G has a topological ordering, if and only if G is a directed acyclic graph.,Task Scheduling,Problem: Scheduling a project consisting of a set of interdependent tasks to b
11、e done by one person. Solution: Establishing a dependency graph, the vertices are tasks, and edge vw is included iff. the execution of v depends on the completion of w, Making task scheduling according to the topological order of the graph(if existing).,Task Scheduling: an Example,Tasks(No.) Depends
12、 on - choose clothes(1) 9 dress(2) 1,8 eat breakfast(3) 5,6,7 leave(4) 2,3 make coffee(5) 9 make toast(6) 9 pour juice(7) 9 shower(8) 9 wake up(9) -,1/4/2,17/18/9,5/8/4,9/16/8,12/13/6,10/11/5,14/15/7,6/7/3,2/3/1,A reverse topological order 9, 1, 8, 2, 5, 6, 7, 3, 4,The DAG,Critical Path in a Task Gr
13、aph,Earliest start time(est) for a task v If v has no dependencies, the est is 0 If v has dependencies, the est is the maximum of the earliest finish time of its dependencies. Earliest finish time(eft) for a task v For any task: eft = est + duration Critical path in a project is a sequence of tasks:
14、 v0, v1, , vk, satisfying: v0 has no dependencies; For any vi(i=1,2,k), vi-1 is a dependency of vi, such that est of vi equals eft of vi-1; eft of vk, is maximum for all tasks in the project.,Project Optimization Problem,Assuming that parallel executions of tasks are possible except for prohibited b
15、y interdependency. Oberservation In a critical path, vi-1, is a critical dependency of vi, i.e. any delay in vi-1will result in delay in vi. The time for entire project depends on the time for the critical path. Reducing the time of a off-critical-path task is no help for reducing the total time for
16、 the project. The problems Find the critical path in a DAG (And try to reduce the time for the critical path),This is a precondition.,Weighted DAG with done Vertex,dress: 6.5,leave: 1,eat: 6,choose: 3,shower: 8.5,wake:0,coffee: 4.5,toast: 2,juice: 0.5,done,0,0,8.5,0.5,4.5,6.0,6.5,3,1,0,0,0,2,Critica
17、l Path,Critical Subpath,Critical Path Finding using DFS,Specialized parameters Array duration, keeps the execution time of each vertex. Array critDep, keeps the critical dependency of each vertex. Array eft, keeps the earliest finished time of each vertex. Output Array topo, critDep, eft as filled.
18、Critical path is built by tracing the output.,void dfsCritSweep(IntList adjVertices,int n, int duration, int critDep, int eft)For each vertex v of G, in some orderif (colorv=white)dfsCrit(adjVertices, color, v, duration, critDep, eft);/ Continue loopreturn;,Critical Path Finding using DFS,void dfsCr
19、it( adjVertices, color, v, int duration, int critDep, int eft)int w; IntList remAdj; int est=0;colorv=gray; critDepv=-1; remAdj=adjVerticesv;while (remAdjnil) w=first(remAdj);if (colorw=white)dfsTopo(adjVertices, color, w, duration, critDep, efs);if (eftwest) est=eftw; critDepv=welse/checking for no
20、ntree edgeif (eftwest) est=eftw; critDepv=wremAdj=rest(remAdj);eftv=est+durationv; colorv=black;return;,Critical Path Finding using DFS,When is the eftw initialized?,Only black vertex,Analysis of Critical Path Algorithm,Correctness: When eftw is accessed in the while-loop, the w must not be gray(oth
21、erwise, there is a cycle), so, it must be black, with eft initialized. According to DFS, each entry in the eft array is assigned a value exactly once. The value satisfies the definition of eft. Complexity Simply same as DFS, that is (n+m).,Strongly Connected and Condensation,G,Graph G 3 Strongly Con
22、nected Components,ABDF,C,EG,Condensation Graph G,Its acyclic, Why?,Note: two SCC in one DFS tree,Basic Idea of SCC,a DFS tree,C1,C2,C3,C4,Leader of a Strong Component,For a DFS, the first vertex discovered in a strong component Si is called the leader of Si . Each DFS tree of a digraph G contains on
23、ly complete strong components of G, one or more. Proof: Applying White Path Theorem whenever the leader of Si (i=1,2,p) is discovered, starting with all vertices being white. The leader of Si is the last vertex to finish among all vertices of Si. (since all of them in the same DFS tree),Path between
24、 Strong Components,Si,Sj1,vi,The leader of Si At discovering,Whats the color?,y,x,Gray,Existing a xvi-path, so x must be in a different strong component. No vix-path can exist.,z,1. y cant be gray. 2. vix-path is a White Path. 3. otherwise, y is black (consider the possible last non-white vertex z o
25、n the vix-path),Sj2,Active Intervals,If there is an edge from Si to Sj, then it is impossible that the active interval of vj is entirely after that of vi. There is no path from a leader of a strong component to any gray vertex. If there is a path from the leader v of a strong component to any x in a
26、 different strong component, v finishes later than x.,Strong Component Algorithm: Outline,Note: G and GT have the same SCC sets,void strongComponents(IntList adjVertices, int n, int scc) /Phase 11. IntStack finishStack=create(n);2. Perform a depth-first search on G, using the DFS skeleton. At postor
27、der processing for vertex v, insert the statement: push(finishStack, v) /Phase 23. Compute GT, the transpose graph, represented as array adjTrans of adjacency list.4. dfsTsweep(adjTrans, n, finishStack, scc);return,Strong Component Algorithm: Core,void dfsTsweep(IntList adjTrans, int n, IntStack fin
28、ishStack, int scc)while (finishStack is not empty)int v=top(finishStack);pop(finishStack);if (colorv=white)dfsT(adjTrans, color, v, v, scc);return; void dfsT(IntList adjTrans, int color, int v, int leader, int scc)Use the standard depth-first search skeleton. At postorder processing for vertex v ins
29、ert the statement:sccv=leader;Pass leader and scc into recursive calls.,SCC: an Example,C D B F A G E,push/pop,F,E,D,C,B,A,G,transposed,F,E,D,C,B,A,G,F,E,D,C,B,A,G,Correctness of Strong Component Algorithm(1),In phase 2, each time a white vertex is popped from finishStack, that vertex is the Phase 1
30、 leader of a strong component. The later finished, the earlier popped The leader is the first to get popped in the strong component it belongs to If x popped is not a leader, then some other vertex in the strong component has been visited proviously. But not a partial strong component can be in a DF
31、S tree, so, x must be in a completed DFS tree, and is not white.,Correctness of Strong Component Algorithm(2),In phase 2, each depth-first search tree contains exactly one strong component of vertices Only “exactly one” need to be proved Assume that vi, a phase 1 leader is popped. If another compone
32、nt Sj is reachable from vi in GT, there is a path in G from vj to vi. So, in phase 1, vj finished later than vi, and popped earlier than vi in phase 2. So, when vi popped, all vertices in Sj are black. So, Sj are not contained in DFS tree containing vi(Si).,Home Assignment,pp.378- 7.14 7.17 7.22 7.25 7.26,