1 / 29

CSC 213

CSC 213. BOS. ORD. Lecture 22: Directed Graphs. JFK. SFO. DFW. LAX. MIA. E. D. C. B. A. Directed Graph ( § 12.4). Directed graph contains only directed edges Used to show relationship only goes in one direction one-way streets flights task scheduling. E. D. C. B. A.

Download Presentation

CSC 213

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 BOS ORD Lecture 22: Directed Graphs JFK SFO DFW LAX MIA

  2. E D C B A Directed Graph (§ 12.4) • Directed graph contains only directed edges • Used to show relationship only goes in one direction • one-way streets • flights • task scheduling

  3. E D C B A Properties of a Directed Graph • Edges goes in one direction • Edge (a,b) connects a to b • Edge (a,b) does notconnectb to a • Separate idea of an in-edge and out-edge • (a,b) is an out-edge of vertex a • (a,b) is an in-edge of vertex b • Usually keep vertex’s in-edges and out-edges in separate lists in adjacency-list implementation of Graph ADT

  4. Application • Scheduling: edge means source vertex must be completed before target vertex can be started csc111 csc212 csc213 csc253 csc281 csc310 csc395 csc360 csc330 csc351 Graduation & $$$$$

  5. Directed Searches • DFS & BFS traverse edges only from source to target (“sink”) • Directed DFS has four types of edges • discovery edges (a,c) (a,b) (c,d) (c,e) • back edges (e,a) • forward edges (a,d) • cross edges (b,d) (d,e) E D C B A

  6. Reachability • Vertices e, a, & d are reachable from Vertex c because there exist paths from c to each of them E D E D C A C F E D A B C F A B

  7. Transitive Closure D E • Transitive closure of directed graph G is directed graph G* such that • Vertex set is identical • Edges in Galso inG* • G* also includes edge (x,y) if y reachable from x in G B G C A D E B C A G*

  8. Computing the Transitive Closure • Perform DFS from each vertex • Add edge from starting vertex to every vertex reached • Takes O(n*(n+m)) • Or could try dynamic programming • Called the Floyd-Warshall Algorithm

  9. Floyd-Warshall’s Algorithm • First, number G vertices from 1 to n • Compute series of n directed graphs • G0=G • Gkhas edge (vi, vj) if (vi, vj) is in Gk-1or a vertex in Gk-1 is target of edge from vi and source of an edge to vj • Gn = G* • Takes O(n3) time using adjacency matrix • Can be slower than “brute force” approach!

  10. Floyd-Warshall Example BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  11. Floyd-Warshall, Iteration 1 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  12. Floyd-Warshall, Iteration 2 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  13. Floyd-Warshall, Iteration 3 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  14. Floyd-Warshall, Iteration 4 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  15. BOS Floyd-Warshall, Iteration 5 v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  16. BOS Floyd-Warshall, Iteration 6 v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  17. BOS Floyd-Warshall, Conclusion v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5

  18. DAG D E • Stands for directed acyclic graph • Topological ordering numbers vertices so every edge (vi , vj) has i < j • Solves problems that require finding a valid schedule B C A DAG G v4 v5 D E v2 B v3 C v1 Topological ordering of G A

  19. Topological Sorting • Edges must connect smaller to larger number vertex 1 Professor’s expectation of student’s day wake up 3 2 eat study 5 4 study class 7 homework 6 work 8 study 9 10 dream about classwork go to bed

  20. Algorithm for Topological Sorting AlgorithmTopologicalSort(Graph G) Graph H G Map m new MyMap() int n G.numVertices() whileH.numVertices() ≠ 0 do Vertex v vertex in H with no outgoing edges m.insert(v,n) n  n – 1 H.removeVertex(v) end while return m

  21. Topological Sorting Example

  22. Topological Sorting Example 9

  23. Topological Sorting Example 8 9

  24. Topological Sorting Example 7 8 9

  25. Topological Sorting Example 6 7 8 9

  26. Topological Sorting Example 6 5 7 8 9

  27. Topological Sorting Example 4 6 5 7 8 9

  28. Topological Sorting Example 3 4 6 5 7 8 9

  29. Topological Sorting Example 1 2 3 4 6 5 7 8 9

More Related