Testing for Connectedness and Cycles

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Discrete Mathematics University of Jazeera College of Information Technology & Design Khulood Ghazal Connectivity Lecture _13.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
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
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Algorithms and Data Structures
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
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 Data Structures DFS, Topological Sort Dana Shapira.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Introduction to Graphs
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
CSC 331: Algorithm Analysis Decompositions of Graphs.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Depth-First Search Lecture 21: Graph Traversals
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Properties and Applications of Depth-First Search Trees and Forests
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Graphs and Paths : Chapter 15 Saurav Karmakar
 2004 SDU 1 Lecture5-Strongly Connected Components.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
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.
Spanning Trees Alyce Brady CS 510: Computer Algorithms.
Leda Demos By: Kelley Louie Credits: definitions from Algorithms Lectures and Discrete Mathematics with Algorithms by Albertson and Hutchinson graphics.
Unit 5 - Graph Suyash Bhardwaj FET, GKV. Overview Graph: Definitions of Isomorphism, Components. Circuits, Fundamental Circuits. Cut-Vertices Planer and.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Breadth-First Search (BFS)
Csc 2720 Instructor: Zhuojun Duan
CS120 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Graph transversals.
Testing for Connectedness and Cycles
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Connectedness of a directed Graph Implementation of Strong Connectedness Algorithm. Algorithm for finding strongly connected components. Cycle detection algorithm in Directed Graphs. Review Questions.

Connectedness of an Undirected Graph An undirected graph G = (V, E) is connected if there is a path between every pair of vertices. Although the figure below appears to be two graphs, it is actually a single graph. Clearly, G is not connected. e.g. no path between A and D. G consists of two unconnected parts, each of which is a connected sub-graph --- connected components. V = {A, B, C, D, E, F} E = {{A, B}, {A, C}, {B, C}, {D, E}, {E, F}}

Implementation of Connectedness Algorithm A simple way to test for connectedness in an undirected graph is to use either depth-first or breadth-first traversal - Only if all the vertices are visited is the graph connected. The algorithm uses the following visitor: public class CountingVisitor extends AbstractVisitor { protected int count; public int getCount(){ return count;} public void visit(Object obj) {count++;} } Using the CountingVisitor, the isConnected method is implemented as follows: public boolean isConnected() { CountingVisitor visitor = new CountingVisitor(); Iterator i = getVertices(); Vertex start = (Vertex) i.next(); breadthFirstTraversal(visitor, start); return visitor.getCount() == numberOfVertices; } What is the time complexity of this algorithm in O()?

Connectedness of a Directed Graph A directed graph G = (V, E) is strongly connected if there is a directed path between every pair of vertices. Is the directed graph below connected? G is not strongly connected. No path between any of the vertices in {D, E, F} However, G is weakly connected since the underlying undirected graph is connected. V = {A, B, C, D, E, F} E = {(A, B), (B, C), (C, A), (B, E), (D, E), (E, F), (F, D)

Implementation of Strong Connectedness Algorithm A simple way to test for strong connectedness is to use |V| traversals - The graph is strongly connected if all the vertices are visited in each traversal. public boolean isStronglyConnected() { if (!this.isDirected()) throw new InvalidOperationException( "Invalid for Undirected Graph"); Iterator it = getVertices(); while(it.hasNext()) { CountingVisitor visitor = new CountingVisitor(); breadthFirstTraversal(visitor, (Vertex) it.next()); if(visitor.getCount() != numberOfVertices) return false; } return true; Implementation of weak connectedness is done in the Lab. What is the time complexity of this algorithm in O()?

Strongly Connected Components A strongly connected component in a directed graph DG=<V,E> is a set of vertices C (which is a subset of V) such that for every pair of vertices x,y in C, there exists a directed path from x to y and a directed path from y to x. Thus, starting at any vertex in a strongly connected component C, it is possible to reach every other vertex in C. The number of strongly connected components in a strongly connected directed graph is …..

Example 3 7 1 5 8 2 6 4

SCC Algorithm Perform a post-order depth first search on the directed graph DG. Number the depth first search tree (or forest) using the visit method of the postorder traversal. Form a new graph DGr=<V,Er> by reversing every edge in E to form Er. Perform a depth first search on the directed graph DGr, with vertices ordered in decreasing order of their numbers generated in 2. Assign a distinct component number to all visited vertices every time the DFS procedure is called from within the general traversal algorithm. What is the time complexity of the SCC Algorithm in O()?

Example 3 7 1 5 8 2 6 4

Cycles in a Directed Graph An easy way to detect the presence of cycles in a directed graph is to attempt a topological order traversal. This algorithm visits all the vertices of a directed graph if the graph has no cycles. In the following graph, after A is visited and removed, all the remaining vertices have in-degree of one. Thus, a topological order traversal cannot complete. This is because of the presence of the cycle { B, C, D, B}. public boolean isCyclic() { CountingVisitor visitor = new CountingVisitor(); topologicalOrderTraversal(visitor); return visitor.getCount() != numberOfVertices; } What is the time complexity of this algorithm in O()?

Review Questions 1. Every tree is a directed, acyclic graph (DAG), but there exist DAGs that are not trees. a) How can we tell whether a given DAG is a tree? b) Devise an algorithm to test whether a given DAG is a tree. 2. Consider an acyclic, connected, undirected graph G that has n vertices. How many edges does G have? 3. In general, an undirected graph contains one or more connected components. a) Devise an algorithm that counts the number of connected components in a graph. b) Devise an algorithm that labels the vertices of a graph in such a way that all the vertices in a given connected component get the same label and vertices in different connected components get different labels. 4. Devise an algorithm that takes as input a graph, and a pair of vertices, v and w, and determines whether w is reachable from v.