Testing for Connectedness and Cycles

Slides:



Advertisements
Similar presentations
CSE 211 Discrete Mathematics
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Discrete Mathematics University of Jazeera College of Information Technology & Design Khulood Ghazal Connectivity Lecture _13.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Topological Sort Introduction. Definition of Topological Sort.
Testing for Connectedness and Cycles
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
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.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. 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,5),(6,7)
Graph Terms By Susan Ott. Vertices Here are 7 vertices without any edges Each Vertex is labeled a different color and number.
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
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,
CSC 252 Pallavi Moorthy Homework 5. 1.) Vertices, edges From cd../../handout/demo/graph_alg/gw_shortest_path.
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.
Graphs A New Data Structure
Minimum Spanning Tree What is a Minimum Spanning Tree.
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures and Algorithms I
Introduction to Graphs
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
More Graph Algorithms.
Graphs Graph transversals.
Graphs Chapter 13.
Topological Sort Introduction. Definition of Topological Sort.
Chapter 11 Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2009 Lecture 6
Graphs Chapter 7 Visit for more Learning Resources.
Chapter 16 1 – Graphs Graph Categories Strong Components
Graphs.
Graphs.
Topological Sort Introduction. Definition of Topological Sort.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Paths and Connectivity
Topological Sort Introduction. Definition of Topological Sort.
Graphs.
Richard Anderson Lecture 5 Graph Theory
Topological Sort Introduction. Definition of Topological Sort.
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness Algorithm. Cycles in a Directed Graph. Implementation of a Cycle detection Algorithm. 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; }

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.

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; }

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.