290 likes | 465 Views
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.
E N D
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 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
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 & $$$$$
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
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
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*
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
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!
Floyd-Warshall Example BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Floyd-Warshall, Iteration 1 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Floyd-Warshall, Iteration 2 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Floyd-Warshall, Iteration 3 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Floyd-Warshall, Iteration 4 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
BOS Floyd-Warshall, Iteration 5 v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
BOS Floyd-Warshall, Iteration 6 v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
BOS Floyd-Warshall, Conclusion v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
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
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
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
Topological Sorting Example 6 7 8 9
Topological Sorting Example 6 5 7 8 9
Topological Sorting Example 4 6 5 7 8 9
Topological Sorting Example 3 4 6 5 7 8 9
Topological Sorting Example 1 2 3 4 6 5 7 8 9