Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
Breadth-First Search of Graphs Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Applications of graph traversals
Applications of DFS: Articulation Points and Biconnected Components
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Elementary graph algorithms Chapter 22
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
IS 2610: Data Structures Graph April 5, 2004.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Module #19: Graph Theory: part II Rosen 5 th ed., chs. 8-9.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Discrete Structures Trees (Ch. 11)
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Flow Algorithms Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms Week 9, Lecture 2.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Properties and Applications of Depth-First Search Trees and Forests
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Introduction to Algorithms
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Breadth-First Search (BFS)
Trees Chapter 11.
12. Graphs and Trees 2 Summary
Graph Algorithms Using Depth First Search
Lecture 10 Algorithm Analysis
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Advanced Algorithms Analysis and Design
Elementary graph algorithms Chapter 22
Subgraphs, Connected Components, Spanning Trees
Elementary graph algorithms Chapter 22
Elementary Graph Algorithms
Presentation transcript:

Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms Week 8, Lecture 1

Graph Algorithms Using Depth First Search a)Graph Definitions b)DFS of Graphs c)Biconnected Components d)DFS of Digraphs e)Strongly Connected Components

Readings on Graph Algorithms Using Depth First Search Reading Selection: –CLR, Chapter 22

Graph Terminology GraphG=(V,E) Vertex setV Edge setE pairs of vertices which are adjacent

Directed and Undirected Graphs G directed G undirected uv uv

Proper Graphs and Subgraphs Proper graph: –No loops –No multi-edges SubgraphG ‘ of G G ‘ = (V ‘, E ‘ ) where V ‘ is a subset of V, E ‘ is a subset of E

Paths in Graphs Path p P is a sequence of vertices v 0, v 1, …, v k where for i=1,…k, v i-1 is adjacent to v i Equivalently, p is a sequence of edges e 1, …, e k where for i = 2,…k edges e i-1, e i share a vertex V1V1 V2V2 V k-1 VkVk ekek e2e2 e1e1 VoVo

Simple Paths and Cycles Simple path no edge or vertex repeated, except possibly v o = v k Cycle a path p with v o = v k where k>1 V1V1 V2V2 V k-1 V k = V o

A Connected Undirected Graph

Connected Components of an Undirected Graph

Biconnected Undirected Graphs (or G is single edge)

Size of a Graph Graph G = (V,E) n = |V| = # vertices m = |E| = # edges size of G is n+m

Representing a Graph by an Adjacency Matrix Adjacency Matrix A

Adjacency List Representation of a Graph Adjacency ListsAdj (1),…,Adj (n) Adj(v) = list of vertices adjacent to v space cost O(n+m)

Definition of an Undirected Tree Tree T is graph with unique simple path between every pair of vertices n = # vertices n-1 = # edges Forest –set of trees

Definition of a Directed Tree Directed Tree T is digraph with distinguished vertex root r such that each vertex reachable from r by a unique path Family Relationships: - ancestors - descendants - parent - child - siblings r leaves have no proper descendants

An Ordered Tree Ordered Tree –is a directed tree with siblings ordered B A C D E F HI

Preorder Tree Traversal PreorderA,B,C,D,E,F,H,I [1] root (order vertices as pushed on stack) [2] preorder left subtree [3] preorder right subtree B A C D E F HI

Postorder Tree Traversal PostorderB,E,D,H,I,F,C,A [1] postorder left subtree [2] postorder right subtree [3] root (order vertices as popped off stack) B A C D E F HI

Spanning Tree and Forest of a Graph T is a spanning tree of graph G if (1) T is a directed tree with the same vertex set as G (2) each edge of T is a directed version of an edge of G Spanning Forest: forest of spanning trees of connected components of G

Example Spanning Tree of a Graph root tree edge back edge cross edge

Classification of Edges of G with Spanning Tree T An edge (u,v) of T is tree edge An edge (u,v) of G-T is back edge if u is a descendent or ancestor of v. Else (u,v) is a cross edge

Tarjan ’ s Depth First Search Algorithm We assume a RAM machine model Algorithm Depth First Search

Recursive DFS Procedure

Time Cost of Depth First Search Algorithm on a RAM Input size n = |V|, m = |E| Theorem Depth First Search takes total time cost O(n+m) Proof can associate with each edge and vertex a constant number of operations.

Classification of Edges of G via DFS Spanning Tree T G T

Edge notation induced by Depth First Search Tree T

Classification of Edges of G via DFS Spanning Tree T (cont ’ d) Note DFS tree T has no cross edges will number vertices by order visited in DFS (preorder of T)

Verifying Vertices are Descendants via Preordering of Tree Suppose we preorder number a Tree T Let D v = # of descendants of v Lemma u is descendant of v iff v < u < v + D v v u DvDv W *

Testing for Proper Ancestors via Preordering Lemma If u is descendant of v and (u,w) is back edge s.t. w < v then w is a proper ancestor of v

Low Values For each vertex v, define low(v) = min ( {v} ∪ {w | v→ w} ) Can prove by induction: Lemma can be computed during DFS in postorder. *

Graph with DFS Numbering and Low Values in Brackets 7[5] 1[1] 2[2] 4[2] 3[2] 5[1] 8[1] 6[5] v[low(v)] Number vertices by DFS number

Biconnected Components G is Biconnected iff either (1) G is a single edge, or (2) for each triple of vertices u,v,w

Example of Biconnected Components of Graph G Maximal subgraphs of G which are biconnected  G

Biconnected Components Meet at Articulation Points The intersection of two biconnected components consists of at most one vertex called an Articulation Point. Example: 1,2,5 are articulation points G

Discovery of Biconnected Components via Articulation Points –If can find articulation points then can compute biconnected components: Algorithm: During DFS, use auxiliary stack to store visited edges. Each time we complete the DFS of a tree child of an articulation point, pop all stacked edges currently in stack These popped off edges form a biconnected component

Characterization of an Articulation Point Theorem a is an articulation point iff either (1) a is root with ≥ 2 tree children or (2) a is not root but a has a tree child v with low (v) ≥ a (note easy to check given low computation)

Proof of Characterization of an Articulation Point Proof The conditions are sufficient since any a-avoiding path from v remains in the subtree T v rooted at v, if v is a child of a To show condition necessary, assume a is an articulation point.

Characterization of an Articulation Point (cont ’ d) Case (1) If a is a root and is articulation point, a must have ≥ 2 tree edges to two distinct biconnected components. Case(2) If a is not root, consider graph G - {a} which must have a connected component C consisting of only descendants of a, and with no backedge from C to an ancestor of v. Hence a has a tree child v in C and low (v) ≥ a

Proof of Characterization of an Articulation Point (cont ’ d) a v root Articulation Point a is not the root subtree T v C no back edges Case (2)

Computing Biconnected Components Theorem The Biconnected Components of G = (V,E) can be computed in time O(|V|+|E|) using a RAM Proof idea Use characterization of Bicoconnected components via articulation points Identify these articulation points dynamically during depth first search Use a secondary stack to store the edges of the biconnected components as they are visited When an articulation point is discovered, pop the edges of this stack off to output a biconnected component

Biconnected Components Algorithm [0] initialize a STACK to empty During a DFS traversal do [1] add visited edge to STACK [2] compute low of visited vertex v using Lemma [3] test if v is an articulation point [4] if so, for each u ∈ children (v) in order where low (u) > v do Pop all edges in STACK upto and including tree edge (v,u) Output popped edges as a biconnected component of G od

Time Bounds of Biconnected Components Algorithm Time Bounds: Each edge and vertex can be associated with 0(1) operations. So time O(|V|+|E|).

Depth First Search in a Directed Graph Depth first search tree T of Directed Graph cross cycle forward cycle G = (V,E) i = DFS number v i i = postorder = order vertex popped off DFS stack edge set E partitioned

Classification of Directed Edge (u, v) via DFS Spanning Tree T Tree edge Cycle edge Forward edge Cross edge

Topological Order of Acyclic Directed Graph Digraph G = (V,E) is acyclic if it has no cycles

Characterization of an Acyclic Digraph Proof Case (1): Suppose (u,v) ∈ E is a cycle edge, so v → u. But let e 1,…e k be the tree edges from v to u. Then (u,v), e 1,…e k is a cycle. *

Characterization of an Acyclic Digraph (cont ’ d) Case (2): Suppose G has no a cycle edge Then order vertices in postorder of DFS spanning forest (i.e. in order vertices are popped off DFS stack). This is a reverse topological order of G. So, G can have no cycles. Note: Gives an O(|V|+|E|) algorithm for computing Topological Ordering of an acyclic graph G = (V,E)

Strong Components of Directed Graph Strong Component Collapsed Graph G* derived by collapsing each strong component into a single vertex. note: G* is acyclic.

Directed Graph with Strong Components Circled Directed Graph G = (V,E)

Algorithm Strong Components Input digraph G [1] Perform DFS on G. Renumber vertices by postorder. [2] Let G - be the reverse digraph derived from G by reversing direction of each edge.

Algorithm Strong Components (cont ’ d) [3] Perform DFS on G -, starting at highest numbered vertex. Output resulting DFS tree of G - as a strongly connected component of G. [4] repeat [3], starting at highest numbered vertex not so for visited (halt when all vertices visited)

Time Bounds of Algorithm Strong Components Time Bounds each DFS costs time c(|V|+|E|) So total time = 2*c(|V|+|E|) ≤ O(|V|+|E|)

Example of Step [1] of Strong Components Algorithm Example Input Digraph G

Example of Step [2] of Strong Components Algorithm Reverse Digraph G -

Proof of Strong Components Algorithm Theorem The Algorithm outputs the strong components of G. Proof We must show these are exactly the vertices in each DFS spanning forest of G -

Proof of Strong Components Algorithm (cont ’ d) Suppose: v,w in the same strong component and DFS search in G - starts at vertex r and reaches v. Then w will also be reached. So v,w are output together in same spanning tree of G -.

Proof of Strong Components Algorithm (cont ’ d) Suppose: v,w output in same spanning tree of G -. Let r be the root of that spanning tree. Then there exists paths in G - from r to each of v and w. So there exists paths in G to r from each of v and w.

Proof of Strong Components Algorithm (cont ’ d) Suppose: no path in G to r from v. Then since r has a higher postorder than v, there is not path in G from v to r, a contradiction. Hence, there exists path in G from r to v, and similar argument gives path from r to w. So v and w are in a cycle of G and must be in the same strong component.

Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms Week 8, Lecture 1