Algorithm Design and Analysis

Similar documents
Algorithm Design and Analysis

Strongly Connected Components

Graph Representation

Design and Analysis of Algorithms

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

Algorithm Design and Analysis

Algorithm Design and Analysis

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Elementary Graph Algorithms

22.3 Depth First Search

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

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

Finding Strongly Connected Components

Announcements. HW3 is graded. Average is 81%

Graph representation

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

Depth First Search (DFS)

Graph: representation and traversal

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Chapter 22. Elementary Graph Algorithms

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

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

DFS & STRONGLY CONNECTED COMPONENTS

Inf 2B: Graphs II - Applications of DFS

Copyright 2000, Kevin Wayne 1

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

Design and Analysis of Algorithms

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

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

3.1 Basic Definitions and Applications

Basic Graph Algorithms

CS 473: Algorithms. Chandra Chekuri 3228 Siebel Center. Fall University of Illinois, Urbana-Champaign

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

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

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

Lecture 3: Graphs and flows

Algorithms Activity 6: Applications of BFS

W4231: Analysis of Algorithms

1 Connected components in undirected graphs

Strongly Connected Components. Andreas Klappenecker

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

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Graph Algorithms Using Depth First Search

Chapter 22 Elementary Graph Algorithms

Fundamental Graph Algorithms Part Three

Data Structures and Algorithms. Chapter 7. Graphs

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

CS483 Design and Analysis of Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Basic Graph Algorithms (CLRS B.4-B.5, )

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

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

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

Chapter 22: Elementary Graph Algorithms. Definitions:

Graph Representations and Traversal

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Copyright 2000, Kevin Wayne 1

Representations of Graphs

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

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

Lecture 16: Directed Graphs

Depth First Search (DFS)

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS4800: Algorithms & Data Jonathan Ullman

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Graph. Vertex. edge. Directed Graph. Undirected Graph

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Lecture 22 Tuesday, April 10

Homework Assignment #3 Graph

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CSE 421 Applications of DFS(?) Topological sort

Homework No. 4 Answers

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

3.4 Testing Bipartiteness

Fundamental Graph Algorithms Part Four

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Data Structures and Algorithms. Werner Nutt

Minimum Spanning Trees Ch 23 Traversing graphs

Graph Algorithms. Definition

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Lecture 10. Finding strongly connected components

CSE 421 DFS / Topological Ordering

COT 6405 Introduction to Theory of Algorithms

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Design and Analysis of Algorithms

3.1 Basic Definitions and Applications

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

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

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Solutions to relevant spring 2000 exam problems

CHAPTER 13 GRAPH ALGORITHMS ORD SFO LAX DFW

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

Transcription:

Algorithm Design and Analysis LECTURE 5 Graphs Applications of DFS Topological sort Strongly connected components Sofya Raskhodnikova S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.1

Review If we run DFS on an undirected graph, can there be an edge (u,v) where v is an ancestor of u? ( back edge ) where v is a sibling of u? ( cross edge ) Same questions with a directed graph? Same questions with a BFS tree directed? undirected? S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.3

Application 1 of DFS: Topological Sort S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.4

Directed Acyclic Graphs Def. A topological order of a directed graph G = (V, E) is an ordering of its nodes as v 1, v 2,, v n so that for every edge (v i, v j ) we have i < j. v 2 v 3 v 6 v 5 v 4 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 7 v 1 a DAG a topological ordering S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.5

Precedence Constraints Def. An DAG is a directed graph that contains no directed cycles. Typical meaning : Precedence constraints. Edge (v i, v j ) means task v i must occur before v j. Applications. Course prerequisite graph: course v i must be taken before v j. Compilation: module v i must be compiled before v j. Pipeline of computing jobs: output of job v i needed to determine input of job v j. Getting dressed mittens shirt socks boots jacket underwear pants S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.6

Recall from book Every DAG has a topological order If G graph has a topological order, then G is a DAG. S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.7

Review Suppose your run DFS on a DAG G=(V,E) True or false? Sorting by discovery time gives a topological order Sorting by finish time gives a topological order Proof of correctness: Lemma: If G is a DAG and (u,v) is an edge, then u.f > v.f. Proof on board. S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.12

Generalizations Which of the following is always true in an arbitrary graph? If u v and v u then u. f > v. f If u v and not(v u) then u. f > v. f If u. f > v. f then u v Key Lemma: In any graph G, if u v but u is not reachable from v, then u.f > v.f. Proof: Same as for DAGs. S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.13

Application 2 of DFS: Strongly Connected Components S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.14

Then! is already finished. Since we re exploring.u;! /,wehave not yet finished u. Therefore,! :f <u:f. Strongly Connected Components Undirected graphs: u,v are connected if there is a path between them. ngly connected components Directed graphs: Given directed graph G D.V; E /. A strongly connected component (SCC)of G is a maximal set of vertice u,v are strongly connected if there are paths u such v that and for v all u;! u 2 C,both u! and! u. SCC(u): set of vertices strongly connected to u Example Observation:Two [Just show SCC s first. SCC s Do DFSeither a little later.] disjoint or equal. 14/19 15/16 3/4 1/12 6/9 17/18 13/20 2/5 10/11 7/8 L5.15

How do we find all SCC s? First idea: Pick a vertex u Run DFS (or BFS) from u to find all vertices reachable from u How do we find vertices that can reach u? Look at reverse graph G rev Same vertices: V All edges are reversed: (u, v) becomes (v, u) Run DFS or BFS in G rev to find all vertices that can reach u S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.16

Overall algorithm Maintain function Comp:V {0,,n} An array, or a field for each vertex Initialize to 0 for all v i = 1 For each vertex v if v.scc=0 BFS(G,v) BFS(G rev,v) For all vertices reachable from v in both G and G rev v.scc=i i = i + 1 Time O n m + n in the worst case S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.17

Fast SCC Algorithm SCC fast (G) Call DFS(G) to get finishing times u.f for all u Compute G rev Call DFS(G rev ), with one modification: in main loop, consider vertices in decreasing order of u.f Output vertices of each tree in DFS forest as separate SCC Running time? Correctness? Could we use BFS For the first pass (on G)? For the second pass (on G rev )? S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.18

Example Numbers: discover/finish times of first DFS Red arrows: Forest of DFS(G rev ) Red ovals: roots of second DFS forest S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.19

Proof of Correctness Fix graph G on n vertices For each SCC C in G, define f(c) = latest finish time (from first DFS) in C Order the SCC s C 1, C 2, in decreasing order of f(c) Theorem: The algorithm outputs each of the C i correctly. Proof by induction on i i = 1: Second DFS will start at a vertex x in C 1 There are no edges in G rev leaving C 1 (by key lemma) So DFS-Visit(x) will visit exactly the vertices of C 1 For i > 1: Suppose C 1, C 2, C i 1 are correctly output. Then ith DFS call starts from within C i. All vertices of C i will be reached. Edges in G rev only leave C i towards C j with j < i. So C i is output correctly. QED. L5.20

Exercise Consider the SCC graph G SCC of G: vertices are SCC s of G edge (C,C ) means G has an edge (u,v) with u in C and v in C Prove that G SCC is a DAG. S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.21

Exercise Consider the following modification to the algorithm for SCC: Use G instead of G rev in 2 nd DFS, but scan vertices in order of increasing finish times from the 1 st DFS. Is this algorithm correct? S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne L5.22