Lectures on Graph Algorithms: searching, testing and sorting

Slides:



Advertisements
Similar presentations
Trees-I Prof. Muhammad Saeed Analysis of Algorithms.
Advertisements

Introduction to Algorithms Graph Algorithms
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Analysis of Algorithms
Lectures on NP-hard problems and Approximation algorithms
Certifying algorithms Algorithms and Networks. Certifying algorithms What is it? Examples: –Euler tour –Bipartite graphs –Flow –Planarity Certifying algorithms2.
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Graphs – Depth First Search ORD DFW SFO LAX
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Data Structures Using C++
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Lectures on Network Flows
Discussion #36 Spanning Trees
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Lectures on Greedy Algorithms and Dynamic Programming
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Great Theoretical Ideas in Computer Science for Some.
Connectivity. Graph Scanning Algorithm Input: A graph G and some vertex s. Output: The set R of vertices reachable from s and a set T ⊆ E(G) such that.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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)
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.
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,
Introduction to Algorithms
Data Structures and Algorithm Analysis Lecture 5
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Lectures on Network Flows
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSE 421: Introduction to Algorithms
Lecture 10 Algorithm Analysis
Search Related Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
3.2 Graph Traversal.
Richard Anderson Winter 2019 Lecture 5
Presentation transcript:

Lectures on Graph Algorithms: searching, testing and sorting COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski Graph Algorithms

Overview Previous lectures: Recursive method (searching, sorting, …) This lecture: algorithms on graphs, in particular Representation of graphs Breadth-First Search (BFS) Depth-First Search (DFS) Testing bipartiteness Testing for (directed) cycles Ordering nodes in acyclic directed graphs Graph Algorithms

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

Adjacency matrix Advantages: Disadvantages: Checking in constant time if an edge belongs to the graph Disadvantages: Representation takes memory O(n2) - versus O(m+n) Examining all neighbors of a given node requires time O(n) - versus O(m/n) in average Disadvantages especially for sparse graphs! 1 2 3 4 1 1 2 3 4 1 1 1 1 2 1 3 2 1 1 3 1 2 4 4 2 3 1 1 1 4 1 3 4 1 1 3 Adjacency matrix Adjacency list Graph Algorithms

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

Breadth-First Search (BFS) Given graph G = (V,E) of diameter D and a root node Goal: find a spanning tree such that the distances between nodes and the root are the same as in graph G Idea of the algorithm: For layers i = 0,1,…,D while there is a node in layer i + 1 not added to the partial BFS tree : add the node and an edge connecting it with a single node in layer i root layer 0 layer 2 layer 1 Graph Algorithms

Implementing BFS root layer 0 layer 2 layer 1 Structures: Algorithm: Adjacency list Lists L0,L1,…,LD Array Discovered[1…n] Algorithm: Set L0 = {root} For layers i = 0,1,…,D Initialize empty list Li+1 For each node v in Li take next edge adjacent to v and if its second end w is not marked as Discovered then add w to Li+1 and {v,w} to partial BFS root layer 0 layer 2 layer 1 Graph Algorithms

Analysis of BFS root layer 0 layer 2 layer 1 Correctness: (observing layer property) By induction on layer number: each node in layer i is in the list Li Each node w in layer i has its predecessor on the distance path in layer i-1, consider the first of such predecessors v in list Li-1: w is added to list Li by the step where it is considered as the neighbor of v Complexity: Time: O(m+n) - each edge is considered at most twice - since it occurs twice on the adjacency list Memory: O(m+n) - adjacency list takes O(m+n), lists L0,L1,…,LD have at most n elements in total, array Discovered has n elements root layer 0 layer 2 layer 1 Graph Algorithms

Depth-First Search (DFS) Given graph G = (V,E) of diameter D and a root node Goal: find a spanning tree such that each edge in graph G corresponds to the ancestor relation in the tree (i.e., no cross edges between different branches) Recursive idea of the algorithm: Repeat until no neighbor of the root is free Select a free neighbor v of the root and add the edge from root to v to partial DFS tree Recursively find a DFS* in graph G restricted to free nodes and node v as the root* and add it to partial DFS tree root* root root** Graph Algorithms

Implementing DFS root* root root** Data structures: Algorithm: Adjacency list with pointer .next List (stack) S Array Discovered[1…n] Algorithm: Set S = {root} Repeat until S is empty Consider the top element v in S if v.next is not Discovered then put v.next into the stack S and set v := v.next and v.next to be the first neighbour of v if v.next is Discovered then set v.next to be the next neighbour of v if such neighbour exists otherwise remove v from the stack, add edge {v,z} to partial DFS, where z is the current top element in S Remark: after considering the neighbor of node v we remove this neighbor from adjacency list to avoid considering it many times! root* root root** Graph Algorithms

Analysis of DFS root* root root** Correctness: (observing ancestor property) By induction on the number of recursive calls: Consider edge {v,w} in G : suppose v joints DFS before w. Then w is free and since it is in a connected component with v it will be in DFS* with root* (which is v), so v is the ancestor of w Complexity: Time: O(m+n) - each edge is considered at most twice - while adding or removing from the stack Memory: O(m+n) - adjacency list consumes O(m+n), stack S and array Discovered have at most n elements each root* root root** Graph Algorithms

Textbook and Exercises READING: Chapter 3 “Graphs”, Sections 3.2 and 3.3 OBLIGATORY EXERCISES: Prove that obtained DFS and BFS trees are spanning trees. Prove that if DFS and BFS trees in graph G are the same, then 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. Graph Algorithms

Testing graph properties Testing bipartiteness Testing for directed cycles Graph Algorithms

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

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

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 an adjacency list, how to test if the graph is bipartite? Note: graphs without cycles (trees) are bipartite bipartite: non bipartite Graph Algorithms

Testing bipartiteness 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: Run 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, otherwise it is not We use the following alternative definitions in the analysis: 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 bipartite non bipartite Graph Algorithms

Testing bipartiteness - why it works Property of layers: Every edge is either between two consecutive layers or in a single layer (two ends are in the same layer) - it follows from BFS property 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. Thus the algorithm answers not bipartite correctly. Suppose that graph G is bipartite. Hence all its cycles have even length. Suppose, to the contrary, that the algorithm answers incorrectly that G is not bipartite. Hence it found a monochromatic 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 cycle is odd, thus G is not bipartite. Contradiction! bipartite not bipartite Graph Algorithms

Complexity Time: O(m+n) BFS search: O(m+n) Checking colours of all edges: O(m+n) Memory: O(m+n) Adjacency list with colour labels: O(m+n) Array Active and lists Li for BFS algorithm: O(n) Graph Algorithms

Testing for directed cycles Directed graph G (edges have direction - one end is the beginning, the other one is the end of the edge) Reversed graph Gr 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 have a directed cycle? Graph Algorithms

How to represent directed graphs? Given a directed graph G = (V,E) , how to represent it? Adjacency matrix: ith node is represented as ith row and ith column, edge from ith to jth node is represented as 1 in row i and column j, (0 if there is no directed edge from i to j) Adjacency list: nodes are arranged as array/list, each node record has two lists: one stores in-neighbours, the other stores out-neighbours 1 2 3 4 1 4 1 2 3 4 1 1 1 1 1 2 3 2 1 4 4 2 1 3 2 3 1 4 1 3 4 1 1 3 Adjacency matrix Adjacency list Graph Algorithms

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 directed DFS tree, with the following modification: consider only edges in proper direction; if during building a DFS tree two nodes which are already in the stack are considered then answer cycled; otherwise answer acyclic at the end General remark: if graph G is not connected then do the same until no free node remains Graph Algorithms

Testing cycles - analysis Property of acyclic graph (to be proved later): There is a node with no incoming edges Analysis of algorithm correctness: Suppose that graph G is acyclic. Hence there is no directed cycle and then while building a directed DFS tree we never try to explore the already visited node. Answer acyclic is then proper. 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 in-neighbour of v from this cycle (or even other node before) will attempt to visit v, which causes that the algorithm stops with the correct answer cycled. Graph Algorithms

Complexity Time: O(m+n) Finding a node with no incoming edges: O(m+n) DFS search with checking cycle condition: O(m+n) Memory: O(m+n) Adjacency list: O(m+n) Array Active and stack S for DFS algorithm: O(n) Graph Algorithms

Textbook and Exercises READING: Chapter 3, Sections 3.4 and 3.5 OBLIGATORY EXERCISES: Prove the following property of layers: every edge is either between two consecutive layers or in a single layer (two ends are in the same layer). Prove the following property of acyclic graphs: there is a node with no incoming edges. Is this property true for out-going edges? OTHER EXERCISES: Design and analyse 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? Graph Algorithms

Ordering nodes in acyclic graphs (topological order) Graph Algorithms

Directed Acyclic Graphs (DAG) Directed graph G (edges have directions - one end is the beginning, the other one is the end of the edge) Reversed graph Gr has the same nodes but each edge is a reversed edge from graph G Test if a given direct graph is acyclic (DAG): done already, directed DFS approach in time and memory O(m+n) Problem: is it possible to order all nodes topologically, which means that if (v,w) is a directed edge in G then v is before w? Note: there may be many topological orders graph G reversed graph Gr Graph Algorithms

G has a topological order and it also has a cycle. Opposite property Fact: If graph G has a topological order then it is DAG. Proof: Suppose, to the contrary, that G has a topological order and it also has a cycle. Let v be the smallest element in the cycle according to the topological order. It follows that its predecessor in the cycle must be smaller (in the final order) than v, but this contradicts the fact that v is the smallest element on the cycle. We get a contradiction with our assumption, which means that G must not have a cycle. Graph Algorithms

Key property Property: In DAG there is a node with no incoming edges After removing this node with the incident edges, the remaining graph is DAG Proof: Node with no incoming edges: Suppose, to the contrary, that every node has at least one incoming edge. It follows that starting from any node we can always “leave” a node by incoming edge (in opposite direction) until we return to a visited node, which creates a cycle. This is a contradiction. After removing node still a DAG remains: There could not be a cycle in smaller graph, since it would be in the original graph. Graph Algorithms

Algorithm finding topological order Repeat until all nodes are in the ordered list Find a node with no incoming edges and place the node next in the order Remove this node and all its outgoing edges from the graph Efficient implementation: Preprocessing: for each node keep info if it is active or not (additional array Active of size n needed) - at the beginning all nodes are active go through the list and for every node store the number of incoming edges from other active nodes (additional array Counter of size n needed) select from list S those nodes that have no incoming edges During the algorithm: While removing the node (make inactive), go through its outgoing active neighbours and decrease their counters by one - if some becomes zero put this active node into list S Node with no incoming edges is taken (and then removed) from list S 3 3 4 2 2 2 1 1 1 1 Graph Algorithms

Complexity Time: O(m+n) Preprocessing: During the algorithm: Initialization of array Active: O(n) Filling array Counter - counting edges: O(m+n) Initialization of list S - checking array Counter: O(n) During the algorithm: While removing a node - go through its out-neighbours: total O(m+n) Selecting from list S: total O(n) Memory: O(m+n) Adjacency list: O(m+n) Arrays Active and Counter, list S: O(n) Graph Algorithms

Conclusions Graph algorithms in time O(m + n) Searching in graphs BFS DFS Testing graph properties bipartiteness (based on BFS) if a directed graph is acyclic (based on DFS) Topological order Graph Algorithms

Textbook and Exercises READING: Chapter 3, Section 3.6 EXERCISES: (reminder) Prove that if DFS and BFS trees in graph G are the same, then 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 following property of layers: every edge is either between two consecutive layers or in a single layer (two ends are in the same layer). Prove the following property of acyclic graphs: there is a node with no incoming edges. Is this property true for out-going 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? Graph Algorithms