Directed Graphs BOS Goodrich, Tamassia Directed Graphs 1 ORD JFK SFO DFW LAX MIA

Similar documents
CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

Digraphs ( 12.4) Directed Graphs. Digraph Application. Digraph Properties. A digraph is a graph whose edges are all directed.

Lecture 16: Directed Graphs

DFS on Directed Graphs BOS. Outline and Reading ( 6.4) Digraphs. Reachability ( 6.4.1) Directed Acyclic Graphs (DAG s) ( 6.4.4)

Graph Terminology and Representations

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Directed Graphs BOS SFO

Graphs ORD SFO LAX DFW. Data structures and Algorithms

Graphs ORD SFO LAX DFW. Graphs 1

CHAPTER 14 GRAPH ALGORITHMS ORD SFO LAX DFW

Graphs ORD SFO LAX DFW Goodrich, Tamassia Graphs 1

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms shortest paths, minimum spanning trees, etc.

Graph Algorithms Using Depth First Search

Copyright 2000, Kevin Wayne 1

Graphs. CS16: Introduction to Data Structures & Algorithms Spring 2018

Graph Algorithms shortest paths, minimum spanning trees, etc.

Breadth-First Search L 1 L Goodrich, Tamassia, Goldwasser Breadth-First Search 1

Graphs ADTs and Implementations

CHAPTER 13 GRAPH ALGORITHMS

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 16, March 10, 2016

Graph Traversals. CS200 - Graphs 1

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Graphs ORD SFO LAX DFW

CS4800: Algorithms & Data Jonathan Ullman

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Algorithm Design and Analysis

Depth-First Search A B D E C

CS 361 Data Structures & Algs Lecture 15. Prof. Tom Hayes University of New Mexico

Konigsberg Bridge Problem

CS 225. April 16 Graph Traversal. Data Structures. Wade Fagen-Ulmschneider

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph Algorithms (part 3 of CSC 282),

Shortest Paths D E. Shortest Paths 1

Depth-First Search and Template Patterns

Graph Algorithms. Textbook reading. Chapter 3 Chapter 4. CSci 3110 Graph Algorithms 1/41

CS 206 Introduction to Computer Science II

CHAPTER 13 GRAPH ALGORITHMS

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Introduction to Algorithms

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Design and Analysis of Algorithms

Graphs & Digraphs Tuesday, November 06, 2007

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Graph Algorithms (part 3 of CSC 282),

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Applications of BDF and DFS

CSE 331: Introduction to Algorithm Analysis and Design Graphs

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering


Data Structures Lecture 14

Directed Graphs (II) Hwansoo Han

Lecture 9 Graph Traversal

Outline. Graphs. Divide and Conquer.

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

3.1 Basic Definitions and Applications

CMSC 132: Object-Oriented Programming II

Elementary Graph Algorithms CSE 6331

Strongly Connected Components

Lecture 14: Graph Representation, DFS and Applications

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Algorithm Design and Analysis

Inf 2B: Graphs II - Applications of DFS

Elementary Graph Algorithms

More Graph Algorithms: Topological Sort and Shortest Distance

Lecture 3: Graphs and flows

CSE 417: Algorithms and Computational Complexity

Shortest Path Problem

Lecture 22 Tuesday, April 10

GRAPHS Lecture 17 CS2110 Spring 2014

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graphs 3/25/14 15:37 SFO LAX Goodrich, Tamassia, Goldwasser Graphs 2

Info 2950, Lecture 16

Homework Assignment #3 Graph

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

CS302 - Data Structures using C++

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

Topic 12: Elementary Graph Algorithms

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

Algorithm Design (8) Graph Algorithms 1/2

Graph Representations and Traversal

Advanced Data Management

GRAPHS Lecture 19 CS2110 Spring 2013

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Transcription:

Directed Graphs BOS ORD JFK SFO LAX DFW MIA 2010 Goodrich, Tamassia Directed Graphs 1

Digraphs A digraph is a graph whose edges are all directed Short for directed graph Applications one-way streets flights task scheduling C E A D B 2010 Goodrich, Tamassia Directed Graphs 2

E Digraph Properties D A graph G=(V,E) such that Each edge goes in one direction: A Edge (a,b) goes from a to b, but not b to a If G is simple, m < n(n - 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 C B 2010 Goodrich, Tamassia Directed Graphs 3

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 ics151 The good life 2010 Goodrich, Tamassia Directed Graphs 4

Directed DFS 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 E D discovery edges back edges C forward edges B cross edges A directed DFS starting at a vertex s determines the vertices reachable from s A 2010 Goodrich, Tamassia Directed Graphs 5

Reachability DFS tree rooted at v: vertices reachable from v via directed paths E D E D C A C B F A E C D F 2010 Goodrich, Tamassia Directed Graphs 6 A B

Strong Connectivity Each vertex can reach all other vertices a c g d e f b 2010 Goodrich, Tamassia Directed Graphs 7

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 G: a c g Let G be G with edges reversed d e Perform a DFS from v in G If there s a w not visited, print no f b Else, print yes Running time: O(n+m) G : a c g d e f b 2010 Goodrich, Tamassia Directed Graphs 8

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 (similar to biconnectivity). a c g { a, c, g } f d e b { f, d, e, b } 2010 Goodrich, Tamassia Directed Graphs 9

Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that G* has the same vertices as G if G has a directed path from u to v (u v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B A B A D C D C E G E G* 2010 Goodrich, Tamassia Directed Graphs 10

Computing the Transitive Closure We can perform DFS starting at each vertex O(n(n+m)) If 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. Alternatively... Use dynamic programming: The Floyd-Warshall Algorithm 2010 Goodrich, Tamassia Directed Graphs 11

Floyd-Warshall Transitive Closure Idea #1: Number the vertices 1, 2,, n. Idea #2: Consider paths that use only vertices numbered 1, 2,, k, as intermediate vertices: i Uses only vertices numbered 1,,k (add this edge if it s not already in) Uses only vertices numbered 1,,k-1 k j Uses only vertices numbered 1,,k-1 2010 Goodrich, Tamassia Directed Graphs 12

Floyd-Warshall s Algorithm Number vertices v 1,, v n Compute digraphs G 0,, G n G 0 =G G k has directed edge (v i, v j ) if G has a directed path from v i to v j with intermediate vertices in {v 1,, v k } We have that G n = G* In phase k, digraph G k is computed from G k - 1 Running time: O(n 3 ), 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 v i i i + 1 G 0 G for k 1 to n do G k G k - 1 for i 1 to n (i k) do for j 1 to n (j i, k) do if G k - 1.areAdjacent(v i, v k ) G k - 1.areAdjacent(v k, v j ) if G k.areadjacent(v i, v j ) G k.insertdirectededge(v i, v j, k) return G n 2010 Goodrich, Tamassia Directed Graphs 13

Floyd-Warshall Example BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 14

Floyd-Warshall, Iteration 1 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 15

Floyd-Warshall, Iteration 2 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 16

Floyd-Warshall, Iteration 3 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 17

Floyd-Warshall, Iteration 4 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 18

Floyd-Warshall, Iteration 5 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 19

Floyd-Warshall, Iteration 6 BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 20

Floyd-Warshall, Conclusion BOS v 7 ORD v 4 v 2 SFO JFK v6 v 1 LAX DFW v3 MIA v5 2010 Goodrich, Tamassia Directed Graphs 21

DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1,, v n of the vertices such that for every edge (v i, v j ), we have i < j 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 2010 Goodrich, Tamassia Directed Graphs 22 v 2 v 1 B A B A D C E DAG G v 4 v 5 D E C v3 Topological ordering of G

Topological Sorting Number vertices, so that (u,v) in E implies u < v 1 wake up A typical student day 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 2010 Goodrich, Tamassia Directed Graphs 23

Algorithm for Topological Sorting Note: This algorithm is different than the one in the book 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 Running time: O(n + m) 2010 Goodrich, Tamassia Directed Graphs 24

Implementation with DFS Simulate the algorithm by using depth-first search O(n+m) time. Algorithm topologicaldfs(g) Input dag G Output topological ordering of G n G.numVertices() for all u G.vertices() setlabel(u, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED topologicaldfs(g, v) 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 setlabel(v, VISITED) for all e G.outEdges(v) { outgoing edges } w opposite(v,e) if getlabel(w) = 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 2010 Goodrich, Tamassia Directed Graphs 25

Topological Sorting Example 2010 Goodrich, Tamassia Directed Graphs 26

Topological Sorting Example 9 2010 Goodrich, Tamassia Directed Graphs 27

Topological Sorting Example 8 9 2010 Goodrich, Tamassia Directed Graphs 28

Topological Sorting Example 8 7 9 2010 Goodrich, Tamassia Directed Graphs 29

Topological Sorting Example 6 8 7 9 2010 Goodrich, Tamassia Directed Graphs 30

Topological Sorting Example 6 5 8 7 9 2010 Goodrich, Tamassia Directed Graphs 31

Topological Sorting Example 6 4 5 8 7 9 2010 Goodrich, Tamassia Directed Graphs 32

Topological Sorting Example 3 6 4 5 8 7 9 2010 Goodrich, Tamassia Directed Graphs 33

Topological Sorting Example 2 3 6 4 5 8 7 9 2010 Goodrich, Tamassia Directed Graphs 34

Topological Sorting Example 2 1 3 6 4 5 8 7 9 2010 Goodrich, Tamassia Directed Graphs 35