Directed Graphs Directed Graphs 1 Shortest Path Shortest Path

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
Advertisements

Directed Graphs Directed Graphs Shortest Path 4/10/ :45 AM BOS
Graphs – Depth First Search ORD DFW SFO LAX
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
1 The Graph Abstract Data Type CS 5050 Chapter 6.
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
Minimum Spanning Trees
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
October 22, DFS, BFS, Biconnectivity, Digraphs 1 CS 221 West Virginia University.
Chapter 6 Graphs ORD DFW SFO LAX
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Data Structures and Algorithms1 Data Structures and algorithms (IS ZC361) Weighted Graphs – Shortest path algorithms – MST S.P.Vimal BITS-Pilani
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSC401: Analysis of Algorithms 6-1 CSC401 – Analysis of Algorithms Chapter 6 Graphs Objectives: Introduce graphs and data structures Discuss the graph.
1 Digraphs Reachability Connectivity Transitive Closure Floyd-Warshall Algorithm JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§12.4) Reachability (§12.4.1) Directed DFS Strong connectivity Transitive.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
BCA-II Data Structure Using C Submitted By: Veenu Saini
Breadth-First Search (BFS)
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
14 Graph Algorithms Hongfei Yan June 8, 2016.
Graphs 5/11/2018 8:05 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Lecture 12 Graph Algorithms
Chapter 14 Graph Algorithms
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs 5/1/15 12:25:22 PM
CS120 Graphs.
Graph Algorithms Using Depth First Search
CMSC 341 Lecture 21 Graphs (Introduction)
Biconnectivity SEA PVD ORD FCO SNA MIA 11/16/2018 2:31 AM
Graphs.
Directed Graphs (digraphs)
Graphs.
Graphs.
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Connected Components, Directed Graphs, Topological Sort
14 Graph Algorithms Hongfei Yan June 8, 2016.
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Graphs Part 1 ORD SFO LAX DFW
Richard Anderson Winter 2009 Lecture 6
Data Structures & Algorithms Digraphs and DAGs
Text Book: Introduction to algorithms By C L R S
Chapter 16 1 – Graphs Graph Categories Strong Components
Biconnectivity SEA PVD ORD FCO SNA MIA 5/6/2019 5:08 PM Biconnectivity
Lecture 21: Matrix Operations and All-pair Shortest Paths
Biconnectivity SEA PVD ORD FCO SNA MIA 5/23/ :21 PM
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

Directed Graphs Directed Graphs 1 Shortest Path Shortest Path 07/28/16 16:17 07/28/16 16:17 Directed Graphs BOS ORD JFK SFO DFW LAX MIA Directed Graphs 1 1

Digraphs A digraph is a graph whose edges are all directed. Shortest Path 07/28/16 16:17 Digraphs A digraph is a graph whose edges are all directed. Short for “directed graph”. Applications one-way streets flights task scheduling A C E B D Directed Graphs 2

Shortest Path 07/28/16 16:17 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started. ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151 Directed Graphs 3

Directed DFS E D C B A The DFS and BFS algorithms apply to digraphs. Shortest Path 07/28/16 16:17 Directed DFS The DFS and BFS algorithms apply to digraphs. The directed DFS algorithm has four types of edges: discovery edges, back edges, forward edges, and cross edges. A directed DFS starting at a vertex s determines the vertices reachable from s. E D C B A Directed Graphs 4

Shortest Path 07/28/16 16:17 Reachability DFS tree rooted at v: vertices reachable from v via directed paths. E D E D C A C F E D A B C F A B Directed Graphs 5

Strong Connectivity A path exists between every pair of vertices. a g Shortest Path 07/28/16 16:17 Strong Connectivity A path exists between every pair of vertices. a d c b e f g Directed Graphs 6

Strong Connectivity Algorithm Shortest Path 07/28/16 16:17 Strong Connectivity Algorithm Pick a vertex v in G. Perform a DFS from v in G. If there’s a w not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. Else, print “yes”. Running time: O(n+m). a G: g c d e b f a G’: g c d e b f Directed Graphs 7

Strongly Connected Components Shortest Path 07/28/16 16:17 Strongly Connected Components Maximal subgraphs such that each vertex can reach all other vertices in the subgraph. Can also be done in O(n+m) time using DFS, but is more complicated. a d c b e f g { a , c , g } { f , d , e , b } Directed Graphs 8

Shortest Path 07/28/16 16:17 Transitive Closure D E The transitive closure of a digraph G is the digraph G* such that: G* has the same vertices as G, and if G has a directed path from u to v, G* has a directed edge from u to v. The transitive closure provides reachability information about a digraph. B G C A D E B C A G* Directed Graphs 9

Floyd-Warshall Algorithm Shortest Path 07/28/16 16:17 Floyd-Warshall Algorithm If edges ij and jk exist, add edge ik. Repeat as necessary. i k j Directed Graphs 10

Floyd-Warshall’s Algorithm Shortest Path 07/28/16 16:17 Floyd-Warshall’s Algorithm Number vertices v1 , …, vn Compute digraphs G0, …, Gn G0=G Gk has directed edge (vi, vj) if G has a directed path from vi to vj with intermediate vertices in {v1 , …, vk} We have that Gn = G* In phase k, digraph Gk is computed from Gk  1 Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix) Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i  1 for all v  G.vertices() denote v as vi i  i + 1 G0  G for k  1 to n do Gk  Gk  1 for i  1 to n (i  k) do for j  1 to n (j  i, k) do if Gk  1.areAdjacent(vi, vk)  Gk  1.areAdjacent(vk, vj) if Gk.areAdjacent(vi, vj) Gk.insertDirectedEdge(vi, vj , k) return Gn Directed Graphs 11

Floyd-Warshall Example Shortest Path 07/28/16 16:17 Floyd-Warshall Example BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 12

Floyd-Warshall, Iteration 1 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 1 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 13

Floyd-Warshall, Iteration 2 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 2 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 14

Floyd-Warshall, Iteration 3 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 3 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 15

Floyd-Warshall, Iteration 4 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 4 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 16

Floyd-Warshall, Iteration 5 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 5 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 17

Floyd-Warshall, Iteration 6 Shortest Path 07/28/16 16:17 Floyd-Warshall, Iteration 6 BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 18

Floyd-Warshall, Conclusion Shortest Path 07/28/16 16:17 Floyd-Warshall, Conclusion BOS v ORD 4 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5 Directed Graphs 19

Topological ordering of G Shortest Path 07/28/16 16:17 DAGs and Topological Ordering D E A directed acyclic graph (DAG) is a digraph that has no directed cycles. A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), i  j. Example: in a task scheduling digraph, a topological ordering is a task sequence that satisfies the precedence constraints. Theorem A digraph admits a topological ordering if and only if it is a DAG. B C A DAG G v4 v5 D E v2 B v3 C v1 Topological ordering of G A Directed Graphs 20

Shortest Path 07/28/16 16:17 Topological Sorting Number vertices so that (u,v) in E implies u < v. 1 A typical student day wake up 2 3 eat study computer sci. 4 5 nap more c.s. 7 play 8 write c.s. program 6 9 work out bake cookies 10 sleep 11 dream about graphs Directed Graphs 21

Algorithm for Topological Sorting Shortest Path 07/28/16 16:17 Algorithm for Topological Sorting Note: not in the book. Running time: O(n + m) Algorithm TopologicalSort(G) H  G // Temporary copy of G n  G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v  n n  n  1 Remove v from H Directed Graphs 22

Implementation with DFS Shortest Path Implementation with DFS 07/28/16 16:17 Simulate the algorithm by using depth-first search O(n+m) time. Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Output labeling of the vertices of G in the connected component of v v.setLabel(VISITED) for all e  v.outEdges() { outgoing edges } w  e.opposite(v) if w.getLabel() UNEXPLORED { e is a discovery edge } topologicalDFS(G, w) else { e is a forward or cross edge } Label v with topological number n n  n - 1 Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n  G.numVertices() for all u  G.vertices() u.setLabel(UNEXPLORED) for all v  G.vertices() if v.getLabel() UNEXPLORED topologicalDFS(G, v) Directed Graphs 23

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example Directed Graphs 24

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 9 Directed Graphs 25

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 8 9 Directed Graphs 26

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 7 8 9 Directed Graphs 27

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 6 7 8 9 Directed Graphs 28

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 6 5 7 8 9 Directed Graphs 29

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 4 6 5 7 8 9 Directed Graphs 30

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 3 4 6 5 7 8 9 Directed Graphs 31

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 2 3 4 6 5 7 8 9 Directed Graphs 32

Topological Sorting Example Shortest Path 07/28/16 16:17 Topological Sorting Example 2 1 3 4 6 5 7 8 9 Directed Graphs 33