Lecture # 5. 1 Overview. 2 DFS Review. 2.1 Pre/Post Order and Edge Types. COMPSCI 330: Design and Analysis of Algorithms September 9, 2014

Similar documents
The Goldberg Rao Algorithm for the Maximum Flow Problem

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Lecture 17 : Equivalence and Order Relations DRAFT

Every tree contains a large induced subgraph with all degrees odd

Connectivity and cuts

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Data Structures and Algorithms Written Examination

Triangle deletion. Ernie Croot. February 3, 2010

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

CMPSCI611: Approximating MAX-CUT Lecture 20

Distributed Computing over Communication Networks: Maximal Independent Set

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

Analysis of Algorithms, I

CSC 505, Fall 2000: Week 8

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

5.1 Bipartite Matching

Determination of the normalization level of database schemas through equivalence classes of attributes

Tree-representation of set families and applications to combinatorial decompositions

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

5 Directed acyclic graphs

Graph Theory Problems and Solutions

GRAPH THEORY LECTURE 4: TREES

All trees contain a large induced subgraph having all degrees 1 (mod k)

6.2 Permutations continued

Max Flow, Min Cut, and Matchings (Solution)

A Practical Scheme for Wireless Network Operation

Approximation Algorithms

Euler Paths and Euler Circuits

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

An Empirical Study of Two MIS Algorithms

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Binary Search Trees CMPSC 122

Scheduling Shop Scheduling. Tim Nieberg

Algorithm Design and Analysis

Topology-based network security

Optimal Index Codes for a Class of Multicast Networks with Receiver Side Information

Network (Tree) Topology Inference Based on Prüfer Sequence

POLYNOMIAL RINGS AND UNIQUE FACTORIZATION DOMAINS

Outline 2.1 Graph Isomorphism 2.2 Automorphisms and Symmetry 2.3 Subgraphs, part 1

The Basics of Graphical Models

1 Definitions. Supplementary Material for: Digraphs. Concept graphs

The Student-Project Allocation Problem

Problem Set 7 Solutions

Part 2: Community Detection

Midterm Practice Problems

8.1 Min Degree Spanning Tree

Approximated Distributed Minimum Vertex Cover Algorithms for Bounded Degree Graphs

The Binary Blocking Flow Algorithm. Andrew V. Goldberg Microsoft Research Silicon Valley goldberg/

On the independence number of graphs with maximum degree 3

Lecture 1: Course overview, circuits, and formulas

Simplified External memory Algorithms for Planar DAGs. July 2004

Finding and counting given length cycles

Applied Algorithm Design Lecture 5

SHARP BOUNDS FOR THE SUM OF THE SQUARES OF THE DEGREES OF A GRAPH

On Integer Additive Set-Indexers of Graphs

Lecture 22: November 10

Two Algorithms for the Student-Project Allocation Problem

Social Media Mining. Graph Essentials

Practical Guide to the Simplex Method of Linear Programming

Markov random fields and Gibbs measures

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Network Flow I. Lecture Overview The Network Flow Problem

Single-Link Failure Detection in All-Optical Networks Using Monitoring Cycles and Paths

Computer Science Department. Technion - IIT, Haifa, Israel. Itai and Rodeh [IR] have proved that for any 2-connected graph G and any vertex s G there

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

Single machine parallel batch scheduling with unbounded capacity

Data Structure [Question Bank]

Large induced subgraphs with all degrees odd

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Introduction to Logic in Computer Science: Autumn 2006

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

One last point: we started off this book by introducing another famously hard search problem:

Cpt S 223. School of EECS, WSU

THE DIMENSION OF A VECTOR SPACE

Extremal Wiener Index of Trees with All Degrees Odd

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

How To Solve A K Path In Time (K)

1. Nondeterministically guess a solution (called a certificate) 2. Check whether the solution solves the problem (called verification)

Class notes Program Analysis course given by Prof. Mooly Sagiv Computer Science Department, Tel Aviv University second lecture 8/3/2007

Introduction to Graph Theory

Practice with Proofs

NP-complete? NP-hard? Some Foundations of Complexity. Prof. Sven Hartmann Clausthal University of Technology Department of Informatics

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

each college c i C has a capacity q i - the maximum number of students it will admit

Institut für Informatik Lehrstuhl Theoretische Informatik I / Komplexitätstheorie. An Iterative Compression Algorithm for Vertex Cover

Semi-Matchings for Bipartite Graphs and Load Balancing

Chapter 6: Graph Theory

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

Data Structures Fibonacci Heaps, Amortized Analysis

Lecture 4: BK inequality 27th August and 6th September, 2007

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Generating models of a matched formula with a polynomial delay

CIS570 Lecture 9 Reuse Optimization II 3

Approximating Minimum Bounded Degree Spanning Trees to within One of Optimal

Transcription:

COMPSCI 330: Design and Analysis of Algorithms September 9, 2014 Lecturer: Debmalya Panigrahi Lecture # 5 Scribe: Roger Zou 1 Overview Previously we introduced basic definitions and algorithms in graph theory, with a focus on Depth-First Search (DFS). This lecture, after a brief review of DFS, covers applications of DFS in identifying graph connectivity structure. Content covered include applying DFS for detecting cycles, definitions for Directed Acyclic Graphs (DAG) and (Strongly) Connected Components (SCC), and algorithms for efficiently computing Topological Sort and SCC. 2 DFS Review We will use the following running example. Let G = (V,E) be a directed graph shown bottom left. Suppose DFS at each iteration alphabetically picks the next vertex to visit. Then T is the path (tree) traversed by DFS shown bottom right. Note that the ordering of vertices visited by this DFS at each step of the algorithm is: s a b b a c c a s d d s. 2.1 Pre/Post Order and Edge Types Recall a pre order of a vertex is the first time a vertex is discovered in the ordered list of DFS visits. A post order of a vertex is the last time a vertex appears in the list of DFS visits. In the example above, pre(a) = 2, and post(a) = 8. We are able to classify edges traversed by DFS from looking at their pre/post orders. Please be aware that the tree types and pre/post order values are dependent on the exact DFS traversal order. In other words, there are other valid DFS traversals with different traversal orders and edge types. Below is a chart of edge types with their corresponding pre/post orders. # 5-1

3 Acyclicity in Directed Graphs Table 1: Edge Type and Pre / Post Order Edge Type (u,v) pre/post order Tree/forward pre(u) < pre(v) < post(v) < post(u) Back pre(v) < pre(u) < post(u) < post(v) Cross pre(v) < post(v) < pre(u) < post(u) Using the properties of edge types and pre/post ordering, we can come up with an algorithm to detect acyclicity in directed graphs. Our approach uses the properties of back edges. The claim is that a graph with no back edges is equivalent to an acyclic graph. Lets first prove a lemma that will help prove the claim. Lemma 1. For directed graph G = (V,E) if there exists a cycle u 1,u 2,...,u k, then at least one of these edges is a back edge in ANY DFS. Proof. Suppose you run DFS and have a table of pre/post values. Now sort the vertices in decreasing order of post values. Observe that both tree/forward edges and cross edges point forward in this ordering (you can verify this by looking at the table of edge types and pre/post ordering above). However, back edges point backwards to a previous vertex in this ordering. Since by assumption these vertices form a cycle, there must be at least one back edge. Otherwise, there is no way to go from a vertex later in the ordering to a vertex earlier, which contradicts the assumption of the existence of a cycle. Theorem 2. For directed graph G = (V,E), G has no back edges G is acyclic. Proof. To prove an if and only if statement, we must show two things: (1) if G has no back edges, then G is acyclic and (2) if G is acyclic, then G has no back edges. (As a pedagogical statement on general proof strategy, observe that any statement p = q is true if and only if its contrapositive q = p is also true.) Thus, lets prove the contrapositive of (1), that if G has a cycle, then G has a back edge. But we just proved this in Lemma 1. The second step is proving the contrapositive of (2), that if G has a back edge, then G has a cycle. Suppose e(u,v) E is a back edge. Then by definition of back edges there exists a path P from v to u in the DFS tree. Simply set P P + {e(u,v)}, and so P is a cycle in G. Remark 1. Theorem 2 provides good intuition for an efficient algorithm to detect cycles in directed graphs: Simply do DFS. If at any point you encounter a back edge (which can be determined from pre/post values), declare the existence of a cycle. 4 Topological Sort and Directed Acyclic Graphs Definition 1. A Directed Acyclic Graph (DAG) is a directed graph with no cycles. Definition 2. A Topological Sort of a DAG G = (V,E) is a linear ordering of all v V. # 5-2

4.1 Algorithm for Topological sorting of DAGs Recall the first step of the proof for Lemma 1 orders the vertices of a cycle by decreasing order of post values in directed graph G = (V,E) However, since DAGs have no cycles, and thus no back edges (as proven in Theorem 2), we can order the vertices by decreasing post value. This follows from the property that in DAGs for every e(u, v) E, post(u) > post(v), since only tree/forward and cross edges exist. This provides intuition for the following algorithm for computing a topological ordering in a DAG: 1. Given DAG G = (V,E), find its source v s (vertex with largest post value). v s is next in the ordering. 2. V V \ {v s } 3. Goto Step 1 until G = {/0} 5 (Strong) Connectivity Definition 3. In a directed graph G = (V,E), vertex u is connected to vertex v if there exists a path from u to v. Definition 4. In a directed graph G = (V,E), vertex u is strongly connected to v if there exists a path from u to v, and path from v to u. Strong connectivity of vertices in a directed graph G = (V,E) can be thought of as an equivalence relation. This is seen by demonstrating the reflexivity, transitivity, and symmetry properties of strongly connected vertices. Let u,v,w V be any vertices in a strongly connected component. Then: Reflexivity: trivial path from u to itself. Transitivity: if there exists path from u to v, and from v to w, then there exist path from u to w. Symmetry: if there exists path from u to v, there exists path from v to u. Which of these properties does not hold for connectivity in general (not strongly connected)? 5.1 Strong Connected Components (SCC) Definition 5. In a directed graph G = (V,E), a Strongly Connected Component (SCC) is a maximal subset C V s.t. any two vertices u, v C are strongly connected. Informally, we define maximal set in general to mean the largest subset one can construct to satisfy a certain property. We can partition groups of vertices that are strongly connected into SCCs. Thus ANY directed graph can be decomposed into a DAG on SCCs! From the example directed graph shown above, its SCC is shown below. # 5-3

For directed graph G = (V,E), and its decomposition into a DAG of SCCs, a source SCC C src has no edges from other SCCs going into C src. A sink SCC C sink has no edges from C sink going out to other SCCs in G. 5.2 An Efficient Algorithm for SCCs To come up with an efficient algorithm to find all the SCCs of a directed graph, note that DFS from any vertex v in a sink SCC C sink only visits vertices in C sink. Thus, if we have some way of finding such a v, we can do DFS starting from v, delete all nodes DFS visits, and repeat. However, the problem is finding such a v. The solution arises when one observes that the vertex of highest post value in the DFS of any graph is a source vertex, thus residing in a source SCC. By switching the direction of all edges in G to construct new graph G R, the source vertex in G R (found using DFS) corresponds to a vertex in the sink SCC in G. Then algorithm is as follows: Algorithm to compute SCCs in directed graph G = (V,E) 1. Reverse all edges in G to construct graph G R. (Note that this process depends on graph implementation, and can be nontrivial to implement efficiently). 2. DFS on G R, keeping a table of post values for all vertices. Name the vertex with largest post value v. This is a vertex in sink SCC in G. 3. DFS from v in G. 4. Let C i be the set of all vertices visited by DFS starting from v. Set V V \C i 5. Check the post value table (of the vertices remaining in G) and identify the vertex v corresponding to the largest post value. Goto Step 3 until V = {/0} Running Time: Constructing G R from G (Step 1) can be performed in O(m). Finding a vertex in the sink SCC can be done in O(m). These two steps can be done before the main loop (Steps 3-5). Each iteration i of Steps 3-5 take O(m i ), where m i is the size of C i. Because once a vertex (and associated edges) are deleted they can never be visited again, this means that O( i m i ) = O(m). Thus the worst case running time is O(m). 6 Summary In this lecture we first reviewed edge types and pre/post values obtained from the Depth-First Search (DFS) algorithm. Through theoretical results, we proved strong relationships between back edges, acyclicity, and # 5-4

topological ordering in directed graphs. Then we demonstrated various applications of DFS in revealing the connectivity structure of a directed graph. Specifically, we used DFS to efficiently detect cycles in directed graphs, compute Topological ordering on a Directed Acyclic Graph (DAG), identify Strongly Connected Components (SCCs), and decompose a general directed graph to a DAG of SCCs. # 5-5