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

Similar documents
CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Representations of Graphs

Elementary Graph Algorithms

Graph Representation

Design and Analysis of Algorithms

Chapter 22. Elementary Graph Algorithms

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

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

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

DFS & STRONGLY CONNECTED COMPONENTS

Graph Algorithms. Definition

Graph: representation and traversal

Graph representation

Minimum Spanning Trees Ch 23 Traversing graphs

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

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

COT 6405 Introduction to Theory of Algorithms

Basic Graph Algorithms

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

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

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

Introduction to Algorithms. Lecture 11

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Unit 2: Algorithmic Graph Theory

Graph implementations :

Announcements. HW3 is graded. Average is 81%

22.3 Depth First Search

Outlines: Graphs Part-2

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Graph Search. Adnan Aziz

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

CISC 320 Introduction to Algorithms Fall Lecture 15 Depth First Search

W4231: Analysis of Algorithms

Graph: representation and traversal

Single Source Shortest Path

Algorithm Design and Analysis

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

Data Structures and Algorithms. Chapter 7. Graphs

Data Structures and Algorithms. Werner Nutt

CS 310 Advanced Data Structures and Algorithms

CSI 604 Elementary Graph Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

Basic Graph Definitions

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

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

CSE 331: Introduction to Algorithm Analysis and Design Graphs

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Announcements Problem Set 4 is out!

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

Design and Analysis of Algorithms

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

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Topic 12: Elementary Graph Algorithms

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Figure 1: A directed graph.

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

Graph. Vertex. edge. Directed Graph. Undirected Graph

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

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

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Elementary Graph Algorithms

Review: Graph Theory and Representation

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

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

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

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Graph Representations and Traversal

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Algorithm Design and Analysis

6.006 Introduction to Algorithms Spring 2008

Minimum Spanning Trees

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

Elementary Graph Algorithms CSE 6331

Lecture 3: Graphs and flows

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

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Copyright 2000, Kevin Wayne 1

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

Lecture 11: Analysis of Algorithms (CS ) 1

Lecture 10: Strongly Connected Components, Biconnected Graphs

Quiz 2 Practice Problems

Solutions to relevant spring 2000 exam problems

Graph Algorithms Using Depth First Search

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

Shortest Path Problem

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

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

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

Transcription:

1 2 Week 5 3 4 5

General remarks We finish, by analysing it. Then we consider the second main graph- algorithm, depth-first (). And we consider one application of, of graphs. Reading from CLRS for week 5 Chapter 22, Sections 22.2, 22.3, 22.4.

Recall: (G,s) 1 for each u V(G) 2 d[u] = 3 π[s] = nil 4 d[s] = 0 5 Q = (s) 6 while Q () 7 u = Dequeue[Q] 8 for each v Adj[u] 9 if d[v] = 10 d[v] = d[u]+1 11 π[v] = u 12 Enqueue(Q, v)

Analysis of Correctness Analysis: At termination of (G, s), for every vertex v reachable from s: Time Analysis: v has been encountered; d[v] holds the length of the shortest path from s to v; π[v] represents an edge on a shortest path from v to s. The initialisation takes time Θ(V). Each vertex is Enqueued once and Dequeued once; these queueing operations each take constant time, so the queue manipulation takes time Θ(V) (altogether). The Adjacency list of each vertex is scanned only when the vertex is Dequeued, so scanning adjacency lists takes time Θ(E) (altogether). The overall time of is thus Θ(V +E).

Why do we get shortest paths? Is it really true that we get always shortest paths (that is, using the minimum number of edges)? Let s assume that to some vertex v there exists a shorter path P in G from s to v than found by. Let this length be d < d[v]. 1 v s, since the distance from s to s is zero (using the path without an edge), and this is correctly computed by. 2 Consider the predecessor u on that shorter path P. 3 If also d[u] would be wrong (that is, too big), than we could use u instead of v. Thus w.l.o.g. d[u] is correct. 4 Now when exploring the neighbours of u, in case v is still unexplored, it would get the correct distance d = d[u]+1. 5 So v must have been explored already earlier (than u). 6 So at the time of determining the distance d[u] d[v] 2, the distance d[v] must have been already set. 7 However the distances d[w] set by are non-decreasing!

The role of the queue A crucial property of is that the distances set in step 10 of the algorithm are non-decreasing, that is, if we imagine a watch-point set at step 10, and monitor the stream of values d[v], then we will see 0,1,...,1,2,...,2,3,...,3,... Why is this the case? This must be due to the queue used whose special properties we haven t yet exploited! Since in step 12, directly after setting the distance, we put the vertex into the queue, the above assertion is equivalent to the statement, that the distances of the vertices put into the queue are non-decreasing (in the order they enter the queue). Now why is this the case?

The role of the queue (cont.) First we need to notice that once a distance d[v] is set, it is never changed again. The vertices are taken off the queue ( dequeued ) from the left, and are added ( enqueued ) to the right ( first in, first out ). What is added has a distance one more than what was taken away. We start with 0, and add some 1 s. Then we take those 1 s, and add 2 s. Once the 1 s are finished, we take the 2 s, and add 3 s. And so on that why the sequence of distances is non-decreasing.

Running on directed graphs We can run also on a digraph G, with start-vertex s: 1 For digraphs we have directed spanning trees/forests. 2 Only the vertices reachable from s following the directions of the edges are in that directed tree (directed from s towards the leaves). 3 Still the paths in the directed tree, from s to any other vertex, are shortest possible (given that we obey the given directions of the edges). For a graph (i.e., undirected graph), we need to restart (to obtain a spanning forest, not just a spanning tree) only if the graph is disconnected. However for a digraph there is a much higher chance, that we need to restart in order to cover all vertices. We thus more often need a directed spanning forest, not just a directed spanning tree.

Restart typically needed for digraphs to cover all vertices Consider the simple digraph 1 2 3 In order to cover all vertices, one needs to run at least two times (and if the first time you start it with s = 1, then you need to run it three times). So even for the above apparently very simple graph we need a directed spanning forest. Note that the obtained directed trees (given by π) overlap. So in such a directed forest there is typically a certain overlap between the directed trees in it.

Arcs of different lengths The edges of (di-)graphs have (implicitly) a length of one unit. If arbitrary non-negative lengths are allowed, then we have to generalise to Dijkstra s algorithm. This generalisation must keep the essential properties, that the distances encountered are the final ones and are non-decreasing. But now not all edges have unit-length, and thus instead of a simple queue we need to employ a priority queue. A priority queue returns the vertex in it with the smallest value (distance).

() is another simple but very important technique for ing a graph. Such a constructs a spanning forest for the graph, called the depth-first forest, composed of several depth-first trees, which are rooted spanning trees of the connected components. recursively visits the next unvisited vertex, thus extending the current path as far as possible; when the gets stuck in a corner it backtracks up along the path until a new avenue presents itself. computes the parent π[u] of each vertex u in the depth-first tree (with the parent of initial vertices being nil), as well as its discovery time d[u] (when the vertex is first encountered, initialised to ) and its finishing time f[u] (when the has finished visiting its adjacent vertices).

The algorithm (G) 1 for each u V(G) 2 d[u] = 3 time = 0 4 for each u V(G) 5 if d[u] = 6 π[u] = nil 7 -Visit(u) -Visit(u) 1 time = time +1 2 d[u] = time 3 for each v Adj[u] 4 if d[v] = 5 π[v] = u 6 -Visit(v) 7 time = time +1 8 f[u] = time Analysis: -Visit(u) is invoked exactly once for each vertex, during which we scan its adjacency list once. Hence, like, runs in time Θ(V +E).

illustrated 1 /nil / / / 1 /nil 2 /1 / / 1 2 4 6 d[u] 1 2 4 6 f[u] /π[u] u 3 5 7 / / / 3 5 7 (labelling) / / / Stack = () u = 1 Stack = (1) u = 2 1 /nil 2 /1 / / 1 /nil 2 /1 4 /3 / 1 2 4 6 1 2 4 6 3 5 7 3 5 7 3 /2 / / 3 /2 / / Stack = (2,1) u = 3 Stack = (3,2,1) u = 4 1 /nil 2 /1 4 /3 / 1 /nil 2 /1 4 /3 / 1 2 4 6 1 2 4 6 3 5 7 3 /2 5 /4 / 3 5 7 3 /2 5 /4 6 /5 Stack = (4,3,2,1) u = 5 Stack=(5,4,3,2,1) u = 7 1 /nil 2 /1 4 /3 9 /4 1 2 4 6 1 14 /nil 2 /1 4 13 11 /3 9 /4 10 1 2 4 6 3 5 7 3 /2 5/4 6/5 8 7 Stack = (4,3,2,1) u = 6 3 5 7 3 12 /2 5/4 6/5 8 7 Stack = ()

Running on directed graphs Again (as with ), we can run on a digraph G. Again, no longer do we obtain spanning trees of the connected components of the start vertex. But we obtain a directed spanning tree with exactly all vertices reachable from the root (when following the directions of the edges). Different from, the root (or start vertex) normally does not play a prominent role for : 1 Thus we did not provide the form of with a start vertex as input. 2 But we provided the forest-version, which tries all vertices (in the given order) as start vertices. 3 This will always cover all vertices, via a directed spanning forest.

What are the times good for? (Directed) trees do not contain shortest paths to that end their way of exploring a graph is too adventuresomely (while is very cautious ). Nevertheless, the information gained through the computation of discovery and finish times is very valuable for many tasks. We consider the example of scheduling later. In order to understand this example, we have first to gain a better understanding of the meaning of discovery and finishing time.

Existence of a path versus finishing times Lemma 1 Consider a digraph G and nodes u,v V(G) with d[u] < d[v]. Then there is a path from u to v in G if and only if f[u] > f[v] holds. In words: If node u is discovered earlier than node v, then we can reach v from u iff v finishes earlier than u. Proof. If there is a path from u to v, then, since v was discovered later than u, the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (by construction of the discovery-loop), and thus v must finish before u finishes. In the other direction, if v finishes earlier than u, then the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (since the recursion for u must still be running). Thus, when discovering v, we must be on a path from u to v.

Existence of a path versus finishing times Lemma 1 Consider a digraph G and nodes u,v V(G) with d[u] < d[v]. Then there is a path from u to v in G if and only if f[u] > f[v] holds. In words: If node u is discovered earlier than node v, then we can reach v from u iff v finishes earlier than u. Proof. If there is a path from u to v, then, since v was discovered later than u, the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (by construction of the discovery-loop), and thus v must finish before u finishes. In the other direction, if v finishes earlier than u, then the recursive call of -Visit(v) must happen inside the recursive call of -Visit(u) (since the recursion for u must still be running). Thus, when discovering v, we must be on a path from u to v.

Directed acyclic graphs An important applications of digraphs G is with scheduling: The vertices are the jobs (actions) to be scheduled. A directed edge from vertex u to vertex v means a dependency, that is, action u must be performed before action v. Now consider the situation where we have three jobs a,b,c and the following dependency digraph: G = a b Clearly this can not be scheduled! 8 8888888 c In general we require G to by acyclic, that is, G must not contain a directed cycle. A directed acyclic graph is also called a dag.

Topological Given a dag G modelling a scheduling task, a basic task is to find a linear ordering of the vertices ( actions ) such that all dependencies are respected. This is modelled by the notion of. A sort of a dag is an ordering of its vertices such that for every edge (u,v), u appears before v in the ordering. For example consider G = a 8888888 b 8 c The two possible s of G are a,c,b and c,a,b.

Finishing times in DAGs Lemma 2 After calling on a dag, for every edge (u,v) we have f[u] > f[v]. Proof. There are two cases regarding the discovery times of u and v: 1 If d[u] < d[v], then by Lemma 1 we have f[u] > f[v] (since there is a path from u to v (of length 1)). 2 Now assume d[u] > d[v]. If f[u] > f[v], then we are done, and so assume that f[u] < f[v]. But then by Lemma 1 there is a path from v to u in G, and this together with the edge from u to v establishes a cycle in G, contradicting that G is a dag.

Finishing times in DAGs Lemma 2 After calling on a dag, for every edge (u,v) we have f[u] > f[v]. Proof. There are two cases regarding the discovery times of u and v: 1 If d[u] < d[v], then by Lemma 1 we have f[u] > f[v] (since there is a path from u to v (of length 1)). 2 Now assume d[u] > d[v]. If f[u] > f[v], then we are done, and so assume that f[u] < f[v]. But then by Lemma 1 there is a path from v to u in G, and this together with the edge from u to v establishes a cycle in G, contradicting that G is a dag.

Topological via Corollary 3 To ly sort a dag G, we run on G and print the vertices in reverse order of finishing times. (We can put each vertex on the front of a list as they are finished.)

Topological illustrated Consider the result of running the algorithm on the following dag. 1/20 21/26 22/25 27/28 3/4 m n o p q r s 2/5 6/19 t u v w 7/8 10/17 x y z 11/12 9/18 14/15 (labelling: d[u]/f[u]) 23/2413/16 Listing the vertices in reverse order of finishing time gives the following of the vertices: u : p n o s m r y v w z x u q t f[u]: 28 26 25 24 20 19 18 17 16 15 12 8 5 4

Deciding acyclicity For a graph G (i.e., undirected) detecting the existence of a cycle is simple, via or : G has a cycle (i.e., is not a forest) if and only if resp. will discover a vertex twice. One has to be a bit more careful here, since the parent vertex will always be discovered twice, and thus has to be excluded, but that s it. However for digraphs it is not that simple do you see why?

Revisiting the lemma for In Lemma 2 we said: If G has no, then along every edge the finishing times strictly decrease. So if we discover in digraph G an edge (u,v) with f[u] < f[v], then we know there is a cycle in G. Is this criterion also sufficient? YES: just consider a cycle, and what must happen with the finishing times on it. Lemma 4 Consider a digraph G. Then G has a cycle if and only if every run of on G yields some edge (u,v) E(G) with f[u] < f[v].

Revisiting the lemma for In Lemma 2 we said: If G has no, then along every edge the finishing times strictly decrease. So if we discover in digraph G an edge (u,v) with f[u] < f[v], then we know there is a cycle in G. Is this criterion also sufficient? YES: just consider a cycle, and what must happen with the finishing times on it. Lemma 4 Consider a digraph G. Then G has a cycle if and only if every run of on G yields some edge (u,v) E(G) with f[u] < f[v].

Revisiting the lemma for In Lemma 2 we said: If G has no, then along every edge the finishing times strictly decrease. So if we discover in digraph G an edge (u,v) with f[u] < f[v], then we know there is a cycle in G. Is this criterion also sufficient? YES: just consider a cycle, and what must happen with the finishing times on it. Lemma 4 Consider a digraph G. Then G has a cycle if and only if every run of on G yields some edge (u,v) E(G) with f[u] < f[v].

Revisiting the lemma for In Lemma 2 we said: If G has no, then along every edge the finishing times strictly decrease. So if we discover in digraph G an edge (u,v) with f[u] < f[v], then we know there is a cycle in G. Is this criterion also sufficient? YES: just consider a cycle, and what must happen with the finishing times on it. Lemma 4 Consider a digraph G. Then G has a cycle if and only if every run of on G yields some edge (u,v) E(G) with f[u] < f[v].