1 Chapter 13 Graph AlgorithmsORD DFW SFO LAX 802 1743 1843 1233 337 Chapter 13 Graph Algorithms 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 And Jory Denny 1 1
2 Directed Graphs Shortest Path 12/7/2017 7:14 AM BOS ORD JFK SFO DFWMIA ORD LAX DFW SFO Directed Graphs
3 Digraphs E D C B A A digraph is a graph whose edges are all directedShort for βdirected graphβ Applications one-way streets flights task scheduling
4 Digraph Properties E D C B A A graph πΊ=(π,πΈ) such thatEach 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 Scheduling: edge (π,π) means task π must be completed before π can be startedDigraph Application 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 A C E B D F A C E D A C E B D FDFS 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 a g c d e b fEach vertex can reach all other vertices a d c b 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: π(π+π) g 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 G G* has the same vertices as 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 ExampleJFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6
16 Floyd-Warshall, Iteration 1JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6
17 Floyd-Warshall, Iteration 2JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6
18 Floyd-Warshall, Iteration 3JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6
19 Floyd-Warshall, Iteration 4JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6
20 Floyd-Warshall, Iteration 5JFK MIA ORD LAX DFW SFO v 2 1 3 4 5 6 BOS
21 Floyd-Warshall, Iteration 6JFK MIA ORD LAX DFW SFO v 2 1 3 4 5 6 BOS
22 Floyd-Warshall, ConclusionJFK MIA ORD LAX DFW SFO v 2 1 3 4 5 6 BOS
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