W4231: Analysis of Algorithms

Similar documents
DFS & STRONGLY CONNECTED COMPONENTS

Graph Representation

Graph: representation and traversal

Announcements. HW3 is graded. Average is 81%

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

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Representations of Graphs

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

Basic Graph Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

CSI 604 Elementary Graph Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Basic Graph Algorithms (CLRS B.4-B.5, )

Graph representation

22.3 Depth First Search

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Graph Algorithms. Definition

Graph Algorithms Using Depth First Search

Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

Algorithm Design and Analysis

COT 6405 Introduction to Theory of Algorithms

Lecture 3: Graphs and flows

Elementary Graph Algorithms

Graph Representations and Traversal

Algorithms: Lecture 10. Chalmers University of Technology

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

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

Copyright 2000, Kevin Wayne 1

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

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

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Design and Analysis of Algorithms

Data Structures and Algorithms. Chapter 7. Graphs

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

Graph Search. Adnan Aziz

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

CS 310 Advanced Data Structures and Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Algorithm Design and Analysis

Homework Assignment #3 Graph

CSE 421 DFS / Topological Ordering

Graph. Vertex. edge. Directed Graph. Undirected Graph

Chapter 22 Elementary Graph Algorithms


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

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

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

Outlines: Graphs Part-2

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Figure 1: A directed graph.

3.1 Basic Definitions and Applications

Data Structures and Algorithms. Werner Nutt

(Re)Introduction to Graphs and Some Algorithms

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Fundamental Graph Algorithms Part Three

Graphs. Pseudograph: multiple edges and loops allowed

Strongly Connected Components

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

CS521 \ Notes for the Final Exam

TIE Graph algorithms

Basic Graph Definitions

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

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

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

Fundamental Graph Algorithms Part Four

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.

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Lecture 10 Graph algorithms: testing graph properties

Goals! CSE 417: Algorithms and Computational Complexity!

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

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

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Homework No. 4 Answers

Lecture 9 Graph Traversal

Graph: representation and traversal

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

Introduction to Algorithms. Lecture 11

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Applications of BDF and DFS

(Refer Slide Time: 05:25)

Graph implementations :

3.4 Testing Bipartiteness

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

Transcription:

W4231: Analysis of Algorithms 10/21/1999 Definitions for graphs Breadth First Search and Depth First Search Topological Sort. Graphs AgraphG is given by a set of vertices V and a set of edges E. Normally we call n = V and m = E. In a directed graph, an edge is an ordered pairs of vertices (u, v). The edge goes from u to v and is represented using an arrow. In an undirected graph, an edge is a set (unordered pair) of two vertices {u, v}. COMSW4231, Analysis of Algorithms 1 COMSW4231, Analysis of Algorithms 2 Expressive power Representation A graph can be used to represent a communication network, a hierarchy of classes, the topology of a maze, relationships between people, a subway map, a finite-state automaton, the web... Each application motivates a series of computational problems. We will see efficient solutions to the most basic ones: Connectivity and Shortest Paths. Cuts, Flows, Matching. There are two simple ways of representing a directed graph G =(V,E). Assume V = {1,...,n}. Adjacency List. For every node u we maintain a list of all the nodes v such that (u, v) E. Adjacency Matrix. A n n Boolean matrix M[, ] is maintained, where M[u, v] = { 1 if (u, v) E 0 otherwise COMSW4231, Analysis of Algorithms 3 COMSW4231, Analysis of Algorithms 4 Comparison An adjacency list representation uses O(n + m) space: we have an array of n pointers and the sum of the number of elements in all the lists is m. Deciding whether (u, v) E takes O(n) time in the worst case. An adjacency matrix uses O(n 2 ) space. Deciding whether (u, v) E takes O(1) time in the worst case. Assuming names of vertices and pointers use 2 bytes each, adjacency list requires 2n +4m bytes of space (2n +8m for undirected graphs), adjacency matrix n 2 /8. COMSW4231, Analysis of Algorithms 5 COMSW4231, Analysis of Algorithms 6

Terminology Undirected Graph Two vertices s and t are connected if there is a path u and v are adjacent (or neighbors) if{u, v} E. The degree of u is the number of its neighbor (the size of its adjacency list). A path is a sequence of vertices v 1,v 2,...,v k such that any two consecutive vertices are adjacent. The length of the path is k 1. Apathissimple if no vertex is duplicated. A cycle isapathv 1,v 2,...,v k where v 1 = v k. A cycle is simple if v 1,...,v k 1 are all different. s = v 1,v 2,...,v k = t The equivalence relation being connected to among vertices partitions the set of vertices into connected components. A graph is connected if any two vertices are connected. (I.e. the whole graph is a single connected component.) It is possible to test whether a graph is connected in optimal O(n + m) time. COMSW4231, Analysis of Algorithms 7 COMSW4231, Analysis of Algorithms 8 Terminology Directed Graph Search Path, simple path, cycle, simple cycle, as before. Two vertices s and t are strongly connected if there is a directed path from s to t and a directed path from t to s. The relation being strongly connected to partitions the set of vertices into strongly connected components. A graph is strongly connected if all its vertices are in the same strongly connected component. Several graph algorithms use a procedure that searches the graph visiting all edges. The two main methods to search a graph are Breadth-first search Depth-first search It is possible to test whether a graph is strongly connected in optimal O(n + m) time. (No proof) COMSW4231, Analysis of Algorithms 9 COMSW4231, Analysis of Algorithms 10 Breadth First Search Start from a vertex, then visit all vertices at distance one, then visit all vertices at distance two,... Implementation We use a queue Q and a vector of n colors, one for each vertex. BFS (s, G =(V,E)) begin Initialize Q; col(s) :=gray; enqueue (s, Q) while Q is not empty u := dequeue (Q); col(u) :=black for all v such that (u, v) E and col(v) =white do col(v) :=gray enqueue(v, Q) end COMSW4231, Analysis of Algorithms 11 COMSW4231, Analysis of Algorithms 12

Analysis Distance Using adjacency list, running time is O(n + m). We do O(1) operations on every vertex, and O(1) operations on every edge. At the end, the black vertices are precisely those in the connected component of s (for undirected graphs). Say that the distance between s and t is the smallest k such that there is a path of length k connecting s to t. (Distance is undefined, or, iss and t are not connected.) BFS can be modified to find the shortest path between s and every other vertex. COMSW4231, Analysis of Algorithms 13 COMSW4231, Analysis of Algorithms 14 Rationale Whenever a new (white) vertex is found, it is reached through a shortest path from s. Will prove later. Initially, d[s] =0and d[u] = for u s. Inductively, it will always be true that all vertices in the queue have the right entry in the d[ ] vector. When we are looking at the neighbours of u, the white ones will be at distance d[u]+1from s. We maintain a vector of distances d[ ], where d[u] is the distance from s to u. COMSW4231, Analysis of Algorithms 15 COMSW4231, Analysis of Algorithms 16 Modified BFS Depth First Search BFS (s, G =(V,E)) Initialize Q; for all u V do Initialize d[u] := col(s) :=gray; d[s] :=0 enqueue (s, Q) while Q is not empty u := dequeue (Q) col(u) :=black for all v such that (u, v) E and col(v) =white do col(v) :=gray; d[v] :=d[u]+1 enqueue(v, Q) We follow a direction, as far as possible, and then we backtrack. Optimal strategy to get out of a maze (BFS is also optimal, but DFS is more natural). COMSW4231, Analysis of Algorithms 17 COMSW4231, Analysis of Algorithms 18

Recursive Implementation Simple Version Non-recursive Implementation Basic idea (works for undirected connected graphs): DFS (s, G =(V,E)) DFS-R (s, G) end Non-recursive implementation is similar to BFS but uses a stack instead of a queue. DFS-R (s, G =(V,E)) col(s) :=black; for all v such that (u, v) E and col(v) =white do DFS (v, G) COMSW4231, Analysis of Algorithms 19 COMSW4231, Analysis of Algorithms 20 Recursive Implementation General Version Discovery Time and Finish Time time is a global variable. DFS (G =(V,E)) time := 0 for all u V do if col(u) =white then DFS-R (u, G) DFS-R (s, G) time := time +1; d(s) :=time; col(s) =gray for all v such that (s, v) E do if col(v) =white then DFS (v, G) col(s) :=black time := time +1; f(s) =time The algorithm assigns to every vertex u a discovery time d(u) and a finish time f(u). A clock is maintained during the execution of the algorithm in the variable time. Each vertex is time-stamped the first time that it is seen, and the last time that it is dealt with. COMSW4231, Analysis of Algorithms 21 COMSW4231, Analysis of Algorithms 22 Building a DFS Tree Edges in the DFS Tree By a further modification of the procedures DFS and DFS-R, we can also build a tree (or rather a forest). The roots of the forest are the nodes on which we call DFS-R from within DFS. The edges in the forest are the edges of the form (s, v) where s is the parameter in a call of DFS(s, G) and v is white, and DFS(v, G) is the resulting procedure call. The forest represents the way the recursive calls unfold during the computation. An edge (u, v) is a Tree edge if it is part of the forest. Back edge if v is an ancestor of u in the tree. Forward edge if v is a descendant of u in the tree. Cross edge otherwise. In a the DFS forest of an undirected graph, there is no difference betrween forward and back edges, and there are no cross edges. COMSW4231, Analysis of Algorithms 23 COMSW4231, Analysis of Algorithms 24

Acyclic Graphs Topological Sort An acyclic graph is a directed graph without cycles. Acyclic graphs represent hierarchical structures, e.g. precedence constraints (as in the make command, or in course prerequisites). Suppose V is a set of actions that we have to perform, and (u, v) E iff action u has to be done before action v. We want to find a schedule v 1,...,v n of the actions such that if (v i,v j ) E then i<j. If the graph contains a cycle we are not going to be able to do that. If the graph is acyclic we can always find a feasible schedule, and we can do so efficiently. COMSW4231, Analysis of Algorithms 25 COMSW4231, Analysis of Algorithms 26 One Algorithm for Topological Sort The Optimal and Surprising Algorithm 1. Find a node v with out-degree zero; make v be the last element of the schedule. 2. Delete v and its incident edges from the graph. Schedule recursively the remaining vertices. Time: O(n(n + m)) with careless implementation. Algorithm: Do DFS; schedule the vertices by decreasing values of f(). (Latest finish first) Claim: if the graph is acyclic, the nodes in the list are ordered in the right way. Correctness: exercise. COMSW4231, Analysis of Algorithms 27 COMSW4231, Analysis of Algorithms 28 Analysis First Step Running time: O(m + n). We can modify DFS-R so that every time we are finished with a vertex we put it on top of an initially empty linked list. Correctness: by the following two results: G is acyclic there are no back edges in the DFS forest. We only need is proved using the white path theorem. Cross edges and forward edges always go from nodes with higher finish time to nodes with lower finish time. Lemma 1. If G is acyclic then the DFS forest of G has no back edge. PROOF: If there is a back edge then there is a cycle. COMSW4231, Analysis of Algorithms 29 COMSW4231, Analysis of Algorithms 30

The analysis works A Converse to Lemma 1 Theorem 2. If G is acyclic, the order of discovery in DFS is a good topological sort. PROOF: We want to show that if there is an edge (u, v) then f(u) >f(v). When (u, v) is considered: v is not gray, otherwise u would be a descendent of v and (u, v) be a back edge. If v is white, v becomes a child of u, andf(u) >f(v). If v is black, then f(v) <f(u) too. Lemma 3. is acyclic. If the DFS forest of G has no back edge then G PROOF: If there is a cycle, let v be the first discovered vertex of the cycle, and let u be the predecessor of v in the cycle. v is discovered before u, and there is a path (made by all white vertices) from v to u. It follows that u is a descendent of v in the DFS tree (this is quite obvious, but we better prove it later). Then (u, v) is a back edge. COMSW4231, Analysis of Algorithms 31 COMSW4231, Analysis of Algorithms 32 To complete the argument Theorem 4. For any two vertices u and v, exactly one of the following cases hold: 1. The intervals [d(u),f(u)] and [d(v),f(v)] are disjoint. 2. [d(u),f(u)] contains [d(v),f(v)] and v is a descendant of u in the same DFS tree. 3. [d(v),f(v)] contains [d(u),f(u)] and u is a descendant of v in the same DFS tree. Theorem 5. [White Path Theorem] If at time d(u) there is a path of white vertices going from u to v (v included) then v will become a descendant of u in the DFS forest. PROOF: Suppose not. Then assume that all the other vertices in the u v path become a descendant of u, except v. (Otherwise repeat the argument using instead of v the closest element to u in the path that does not become a descendant.) Then let w be the predecessor of v, then d(u) d(v) f(w) f(v) Then the interval [d(v),f(v)] is contained in [d(u),f(u)] and so v is a descendant of u. COMSW4231, Analysis of Algorithms 33 COMSW4231, Analysis of Algorithms 34