1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.

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

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Algorithms and Data Structures
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
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)
Applications of graph traversals
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Graph & BFS.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Testing for Connectedness and Cycles
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Introduction to Graphs
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
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 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
16-Graphs Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
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.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs and Paths : Chapter 15 Saurav Karmakar
CMSC 341 Graphs – DFS Expanded. 2 Depth First Traversal with Finish Times dfs(Graph G) { for (each v  V) d[v] = 0// d = discovery “time” time = 0// “global”
Chapter 05 Introduction to Graph And Search Algorithms.
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.
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)
CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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,
Unit 5 - Graph Suyash Bhardwaj FET, GKV. Overview Graph: Definitions of Isomorphism, Components. Circuits, Fundamental Circuits. Cut-Vertices Planer and.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CMSC 341 Graphs.
Csc 2720 Instructor: Zhuojun Duan
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.
CMSC 341 Graphs.
Presentation transcript:

1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation of Strong Connectedness Algorithm. Cycles in Directed Graphs. Implementation of Cycle detection Algorithm for Directed Graphs. Cycles in Undirected Graphs Review Questions.

2 Connectedness of Undirected Graphs 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. For example there is no path between A and D. G consists of two unconnected parts, each of which is a maximal connected sub-graph (i.e., a connected component).

3 Implementation of Connectedness Algorithm for Undirected Graphs A simple way to test for connectedness in an undirected graph is to use either depth-first or breadth-first traversal starting from any vertex - Only if all the vertices are visited is the graph connected. The algorithm uses the following visitor: public class CountingVisitor extends AbstractVisitor { protected int counter; public int getCount(){ return counter;} public void visit(Object obj) {counter++;} } Using the CountingVisitor, the isConnected method is implemented as follows: public boolean isConnected() { if(this.isDirected()) throw new InvalidOperationException( "Invalid for Directed Graphs"); CountingVisitor visitor = new CountingVisitor(); Iterator i = getVertices(); Vertex start = (Vertex) i.next(); breadthFirstTraversal(visitor, start); return visitor.getCount() == numberOfVertices; }

4 Connectedness of Directed Graphs A directed graph G = (V, E) is strongly connected if there is a directed path between every pair of vertices [i.e, given any pair of vertices v 1 and v 2, there is a directed path from v 1 to v 2, and there is a directed path from v 2 to v 1 ]. A directed graph G =(V, E) is weakly connected if: –The underlying undirected graph is connected, and if –The is at least one pair of vertices v 1 and v 2 that are not reachable from one another. A directed graph G = (V, E) is disconnected if the underlying undirected graph is disconnected. Example: A weakly connected directed graph

5 Implementation of Strong Connectedness Algorithm A simple way to test for strong connectedness in a directed graph G = (V, E) is to use |V| traversals, each traversal starting from a different graph vertex - The graph is strongly connected if in each traversal all vertices are visited; otherwise it is either disconnected or it is weakly connected. public boolean isStronglyConnected() { if (!this.isDirected()) throw new InvalidOperationException( "Invalid for Undirected Graphs"); Iterator it = getVertices(); while(it.hasNext()) { CountingVisitor visitor = new CountingVisitor(); breadthFirstTraversal(visitor, (Vertex) it.next()); if(visitor.getCount() != numberOfVertices) return false; } return true; } Note: The implementation of weak connectedness algorithm is a task in the Lab.

6 Detection of Cycles in Directed Graphs One algorithm to detect the presence of cycles in a directed graph uses source-removal topological order traversal algorithm. –This algorithm visits all the vertices of a directed graph if the graph has no cycles; otherwise not all vertices are visited. Example: In the graph below, 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() { if (!this.isDirected()) throw new InvalidOperationException( "Invalid for Undirected Graphs"); CountingVisitor visitor = new CountingVisitor(); topologicalOrderTraversal(visitor); return visitor.getCount() != numberOfVertices; }

7 Detection of Cycles in Directed Graphs: DFS based Algorithm A cycle exits in a directed graph if a back edge is detected during a DFS traversal. A back edge exists in a directed graph if the currently explored vertex has an adjacent vertex that was already colored gray dfsAllVertices(G){ color all vertices white; for( each v  V) if (v is white){ if (DFS(v)) Output(“cycle detected”); exit;} Output(“No cycle detected”); } boolean DFS(v){ color[v] = “gray”; [previsit(v)] for (each w adjacent to v){ if(color[w] == “gray”){ return true;} // Cycle exists if (color[w] == “white”){ [ Add edge (v, w) to DFS tree] DFS(w); } [postvisit(v)] color[v] = black; return false; // No cycle exists in the group of vertices reachable from current v }

8 Detection of Cycles in Undirected Graphs: Algorithm1 The algorithm traverses the graph, ignoring each back edge from a vertex to the parent of that vertex. If an edge (v, w) is encountered where both v and w are colored gray, and v is not the parent of w in the DFS tree, a cycle exists in the graph. dfsAll(G){ for each vertex v of G{ color[v] = “white”; parent[v] = null; } for each vertex v of G { if( color[v] == “white”) if(DFS(v)) {Output(“Cycle detected”); exit;}; } Output(“No cycle detected”); } boolean DFS(v){ color[v] = “gray”; for( each vertex w adjacent to v ){ if (color[w] == “white”){ parent[w] = v; DFS(w); } else if( color[w] == “gray” and parent[w] != v) return true; // cycle detected } color[v] = black; return false; // no cycle detected in this component }

9 Detection of Cycles in Undirected Graphs: Algorithm1 (Cont’d) Exercise: Trace the algorithm in the previous slide on the graph given below starting at vertex A : Note: The graph is represented as shown below. The representation is not a multi-graph:

10 Detection of Cycles in Undirected Graphs: Algorithm2 This algorithm is based on the fact that an undirected acyclic graph G = (V, E) is a free tree, in which |E| = |V| - 1; however since each edge in an undirected graph is represented by two edges; the relationship used in the algorithm is |E|/2 = |V| - 1 Thus a connected component of an undirected graph has a cycle if |E component | / 2  |V component | The algorithm is given in the next slide

11 Detection of Cycles in Undirected Graphs: Algorithm2 (Cont’d) DFSAll(G){ Mark all vertices as unvisited; for(i = 0; i < numberOfVertices; i++){ if(vertex i is not visited){ ncce = 0; // global variable, numberOfConnectedComponentEdges nccv = 0; // global variable, numberOfConnectedComponentVertices DFS(vertex i, nccv, ncce); if(ncce/2  nccv){ Output("There is a cycle"); return; } Output("There is no cycle"); } DFS(v, nccv, ncce){ mark v visited; nccv++; for(each vertex w adjacent to v){ ncce++; if(w is not visited) DFS(w, nccv, ncce); }

12 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.