Lecture 10 Graph algorithms: testing graph properties

Similar documents
Lecture 3: Graphs and flows

Graph Search Methods. Graph Search Methods

Graph Search Methods. Graph Search Methods

Algorithms: Lecture 10. Chalmers University of Technology

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

3.1 Basic Definitions and Applications

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

Math 485, Graph Theory: Homework #3

Info 2950, Lecture 16

3.1 Basic Definitions and Applications

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

Goals! CSE 417: Algorithms and Computational Complexity!

GRAPHS Lecture 19 CS2110 Spring 2013

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Matchings in Graphs. Definition 1 Let G = (V, E) be a graph. M E is called as a matching of G if v V we have {e M : v is incident on e E} 1.

W4231: Analysis of Algorithms

Copyright 2000, Kevin Wayne 1

Searching in Graphs (cut points)

Graph Representations and Traversal

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.4 Testing Bipartiteness

CSE 421 DFS / Topological Ordering

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Graph Algorithms Using Depth First Search

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

GRAPHS Lecture 17 CS2110 Spring 2014

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

Finding Strongly Connected Components

Strongly Connected Components. Andreas Klappenecker

CSE 417: Algorithms and Computational Complexity

CS261: Problem Set #1

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Broadcast: Befo re 1

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

22.3 Depth First Search

Algorithms and Data Structures

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

DESIGN AND ANALYSIS OF ALGORITHMS

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

CSI 604 Elementary Graph Algorithms

Outline. Graphs. Divide and Conquer.

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

6. Lecture notes on matroid intersection

Graphs and networks Mixed exercise

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graph theory - solutions to problem set 1

Copyright 2000, Kevin Wayne 1

22.1 Representations of graphs

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Reference Sheet for CO142.2 Discrete Mathematics II

PERFECT MATCHING THE CENTRALIZED DEPLOYMENT MOBILE SENSORS THE PROBLEM SECOND PART: WIRELESS NETWORKS 2.B. SENSOR NETWORKS OF MOBILE SENSORS

Mathematics and Statistics, Part A: Graph Theory Problem Sheet 1, lectures 1-4

1 Undirected Vertex Geography UVG

(Refer Slide Time: 05:25)

Lecture 5: Graphs. Rajat Mittal. IIT Kanpur

Math 776 Graph Theory Lecture Note 1 Basic concepts

Announcements Problem Set 4 is out!

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

PERFECT MATCHING THE CENTRALIZED DEPLOYMENT MOBILE SENSORS THE PROBLEM SECOND PART: WIRELESS NETWORKS 2.B. SENSOR NETWORKS OF MOBILE SENSORS

Fundamental Algorithms

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

MAT 145: PROBLEM SET 6

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Graph: representation and traversal

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

On the Relationships between Zero Forcing Numbers and Certain Graph Coverings

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Section 3.1: Nonseparable Graphs Cut vertex of a connected graph G: A vertex x G such that G x is not connected. Theorem 3.1, p. 57: Every connected

Senior Math Circles November 25, 2009 Graph Theory II

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Solution for Homework set 3

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

Exercise set 2 Solutions

Graph. Vertex. edge. Directed Graph. Undirected Graph

Solutions to Exercises 9

Graph Algorithms (part 3 of CSC 282),

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Algorithms and Data Structures

LECTURES 3 and 4: Flows and Matchings

Algorithms and Data Structures

11.4 Bipartite Multigraphs

1 Bipartite maximum matching

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Graphs Introduction and Depth first algorithm

Module 7. Independent sets, coverings. and matchings. Contents

Transcription:

Lecture 10 Graph algorithms: testing graph properties COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Lecture 10: Testing Graph Properties 1

Overview Previous lectures: Representation of graphs (adjacency matrix or list) Breadth-First Search (BFS) Depth-First Search (DFS) This lecture: Testing bipartiteness Testing for (directed) cycles Lecture 10: Testing Graph Properties 2

How to represent graphs? Given graph G = (V,E), how to represent it? 1. Adjacency matrix: i th node is represented as i th row and i th column, edge between i th and j th nodes is represented as 1 in row i and column j, and vice versa (0 if there is no edge between i and j) 2. Adjacency list: nodes are arranged as array/list, each node record has the list of its neighbours attached 2 1 3 4 1 2 3 4 1 2 3 4 0 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 Adjacency matrix Lecture 10: Testing Graph Properties 3 1 2 3 4 2 3 4 1 3 1 2 4 1 3 Adjacency list

Adjacency list Advantages: Representation takes memory O(m+n) - versus O(n 2 ) Examining all neighbours of a given node requires time O(m/n) in average - versus O(n) Disadvantages: Check if an edge belongs to the graph requires time O(m/n) in average - versus O(1) Advantages especially for sparse graphs! 2 1 3 4 1 2 3 4 1 2 3 4 0 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 Adjacency matrix Lecture 10: Testing Graph Properties 4 1 2 3 4 2 3 4 1 3 1 2 4 1 3 Adjacency list

Bipartiteness Graph G = (V,E) is bipartite iff it can be partitioned into two sets of nodes A and B such that each edge has one end in A and the other end in B. Alternatively: Graph G = (V,E) is bipartite iff all its cycles have even length. Graph G = (V,E) is bipartite iff nodes can be coloured using two colours. Question: given a graph represented as the adjacency list, how to test if graph is bipartite? Note: graphs without cycles (trees) are bipartite. bipartite: not bipartite Lecture 10: Testing Graph Properties 5

Testing bipartiteness We use following definition: Graph G = (V,E) is bipartite iff all its cycles have even length, or Graph G = (V,E) is bipartite iff it has no odd cycle. Method: use BFS search tree. Recall: BFS is a rooted spanning tree having the same layers as the original graph G (each node has the same distance from the root in BFS tree and in graph G) Algorithm: Perform BFS search and colour all nodes in odd layers red, others blue Go through all edges in adjacency list and check if each of them has two different colours at its ends - if so then G is bipartite not bipartite bipartite Lecture 10: Testing Graph Properties 6

Testing bipartiteness - why it works Property of layers: Every edge is either between two consecutive layers or within one layer (two ends are in the same layer) - it follows from BFS property 1. Suppose that graph G is not bipartite. Hence there is an odd cycle. This cycle must have an edge in one layer, and so the ends of this edge have the same colour, so the algorithm answers correctly. 2. Suppose that graph G is bipartite. Hence all its cycles have even length. Suppose, to the contrary, that there is uni-coloured edge. This edge must be in one layer. Consider one of such edges which is in the smallest possible layer. Consider a cycle containing this edge and the BFS path between its two ends. The length of this path is odd, which is a contradiction. Thus all edges are coloured properly (in sense of their ends) and algorithm is correct. not bipartite bipartite Lecture 10: Testing Graph Properties 7

Testing cycles Directed graph G (edges have direction - one end is the beginning, the other one is the end of the edge). Reversed graph G r has the same nodes but each edge is a reversed edge from graph G. Representing directed graph: adjacency list - each node has two lists: of in-coming and out-coming edges/neighbours Problem: does the graph has a directed cycle? Lecture 10: Testing Graph Properties 8

Testing cycles Technique: use directed DFS tree for graph G. Algorithm: Find a node with no incoming edges - if not found answer cycled Build a DFS directed tree - consider only edges in proper direction. If during building DFS two nodes which are already in the stack are considered then answer cycled, otherwise answer acyclic. General remark: if graph G is not connected then do the same until no free node remains. Lecture 10: Testing Graph Properties 9

Testing cycles - analysis Property of acyclic graph: There is a node with no incoming edges 1. Suppose that graph G is acyclic. Hence there is no directed cycle and then while building DFS directed tree we never try to explore the already visited node. Answer acyclic is then proper. 2. Suppose that graph G is not acyclic. It follows that there is a cycle in it. Let v be the first node in a cycle visited by DFS search. By the property of DFS, all nodes from this cycle will be reached during the search rooted in v, and so the out-neighbour of v from this cycle will attempt to visit v also, which causes that the algorithm stops with the correct answer cycled. Lecture 10: Testing Graph Properties 10

Conclusions Testing graph properties bipartiteness (based on BFS), if a directed graph is acyclic (based on DFS), in time O(m + n). Lecture 10: Testing Graph Properties 11

Textbook and Exercises Chapter 3, Sections 3.4, 3.5 and 3.6. Prove that if a DFS and BFS trees in graph G are the same, than G is also the same like they (does not contain any other edge). Prove that if G has n nodes, where n is even, and each node has at least n/2 neighbours then G is connected. Prove the property of layers: every edge is either between two consecutive layers or within one layer (two ends are in the same layer). Prove the property of acyclic graphs: there is a node with no incoming edges. Is this property true for out-coming edges? Design and analyze time and memory taken by BFS for directed graphs. What happens if you try to use a different method of searching to check bipartiteness or acyclicity? Lecture 10: Testing Graph Properties 12