1 Chapter 13 Graph AlgorithmsORD DFW SFO LAX 802 1743 1843 1233 337 Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato 1 1
2 Directed Graphs Shortest Path 12/7/2017 7:14 AM BOS ORD JFK SFO DFWMIA ORD LAX DFW SFO
3 Digraphs E A digraph is a graph whose edges are all directed DB D A digraph is a graph whose edges are all directed Short for βdirected graphβ Applications one-way streets flights task scheduling
4 Digraph Properties E D A graph πΊ=(π,πΈ) such that CB D A graph πΊ=(π,πΈ) such that Each edge goes in one direction: Edge (π,π) goes from π to π, but not π to π If πΊ is simple, π < π(πβ1) If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of incoming edges and outgoing edges in time proportional to their size
5 Digraph Application Scheduling: edge (π,π) means task π must be completed before π can be started The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23 ics22 ics21 ics161 ics151 ics171
6 Directed DFS A C E B D We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges discovery edges back edges forward edges cross edges A directed DFS starting at a vertex π determines the vertices reachable from π
7 Reachability DFS tree rooted at π£: vertices reachable from π£ via directed paths A C E D A C E B D F A C E B D F
8 Strong Connectivity Each vertex can reach all other vertices a g c d eb e f g
9 Strong Connectivity AlgorithmPick a vertex π£ in πΊ Perform a DFS from π£ in πΊ If thereβs a π€ not visited, print βnoβ Let πΊβ be πΊ with edges reversed Perform a DFS from π£ in πΊβ Else, print βyesβ Running time: π(π+π) c d e b f a Gβ: g c d e b f
10 Strongly Connected ComponentsMaximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in π(π+π) time using DFS, but is more complicated (similar to biconnectivity). a d c b e f g { a , c , g } { f , d , e , b }
11 Transitive Closure B A D C E GGiven a digraph πΊ, the transitive closure of πΊ is the digraph πΊ β such that G* has the same vertices as G if πΊ has a directed path from π’ to π£ (π’ οΉ π£), πΊ β has a directed edge from π’ to π£ The transitive closure provides reachability information about a digraph B A D C E G*
12 Computing the Transitive ClosureIf 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. We can perform DFS starting at each vertex π(π(π+π)) Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm
13 Floyd-Warshall Transitive ClosureIdea #1: Number the vertices 1, 2, β¦,π. Idea #2: Consider paths that use only vertices numbered 1, 2, β¦,π, as intermediate vertices: Uses only vertices numbered π,β¦,π (add this edge if itβs not already in) i j Uses only vertices numbered π,β¦,πβ1 Uses only vertices numbered π,β¦,π k
14 Floyd-Warshallβs AlgorithmNumber vertices π£ 1 ,β¦, π£ π Compute digraphs πΊ 0 ,β¦, πΊ π πΊ 0 βπΊ πΊ π has directed edge π£ π , π£ π if πΊ has a directed path from π£ π to π£ π We have that πΊ π = πΊ β In phase π, digraph πΊ π is computed from πΊ πβ1 Running time: π π 3 , assuming πΊ.areAdjacent π£ π , π£ π is π 1 (e.g., adjacency matrix) Algorithm FloydWarshall(πΊ) Input: Digraph πΊ Output: Transitive Closure πΊ β of πΊ Name each vertex π£βπΊ.vertices( ) with π=1β¦π πΊ 0 βπΊ for πβ1β¦π do πΊ π β πΊ πβ1 for πβ1β¦π | πβ π do for πβ1β¦π | πβ π,π do if πΊ πβ1 .areAdjacent π£ π , π£ π β§ πΊ πβ1 .areAdjacent π£ π , π£ π β§ Β¬ πΊ π .areAdjacent π£ π , π£ π then πΊ π .insertDirectedEdge v i , v j return πΊ π
15 Floyd-Warshall ExampleBOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
16 Floyd-Warshall, Iteration 1BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
17 Floyd-Warshall, Iteration 2BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
18 Floyd-Warshall, Iteration 3BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
19 Floyd-Warshall, Iteration 4BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
20 Floyd-Warshall, Iteration 5BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
21 Floyd-Warshall, Iteration 6BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
22 Floyd-Warshall, ConclusionBOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
23 DAGs and Topological OrderingB A D C E DAG G A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering π£ 1 ,β¦, π£ π Of the vertices such that for every edge π£ π , π£ π , we have π<π Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem - A digraph admits a topological ordering if and only if it is a DAG B A D C E Topological ordering of G v1 v2 v3 v4 v5
24 Exercise Topological Sortingwrite c.s. program play wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day bake cookies Number vertices, so that π’,π£ in πΈ implies π’<π£
25 Exercise Topological Sortingwrite c.s. program play wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day 1 2 3 4 5 6 7 8 9 10 11 bake cookies Number vertices, so that π’,π£ in πΈ implies π’<π£
26 Algorithm for Topological SortingNote: This algorithm is different than the one in the book Algorithm TopologicalSort πΊ π»βπΊ πβπΊ.numVertices while Β¬π».empty do Let π£ be a vertex with no outgoing edges Label π£βπ πβπβ1 π».eraseVertex π£
27 Implementation with DFSSimulate the algorithm by using depth-first search π(π+π) time. Algorithm topologicalDFS πΊ Input: DAG πΊ Output: Topological ordering of π πβπΊ.numVertices Initialize all vertices as πππΈπππΏππ πΈπ· for each vertex π£βπΊ.vertices do if π£.getLabel =πππΈπππΏππ πΈπ· then topologicalDFS πΊ, π£ Algorithm topologicalDFS πΊ,π£ Input: DAG πΊ, start vertex π£ Output: Labeling of the vertices of πΊ in the connected component of π£ π£.setLabel(ππΌππΌππΈπ·) for each πβπ£.outEdges do π€βπ.dest( ) if π€.getLabel =πππΈπππΏππ πΈπ· then //π is a discovery edge topologicalDFS πΊ, π€ else //π is a forward, cross, or back edge Label π£ with topological number π πβπβ1
28 Topological Sorting Example
29 Topological Sorting Example9
30 Topological Sorting Example8 9
31 Topological Sorting Example7 8 9
32 Topological Sorting Example7 8 6 9
33 Topological Sorting Example7 8 5 6 9
34 Topological Sorting Example7 4 8 5 6 9
35 Topological Sorting Example7 4 8 5 6 3 9
36 Topological Sorting Example2 7 4 8 5 6 3 9
37 Topological Sorting Example2 7 4 8 5 6 1 3 9