week 5
play

Week 5 Kullmann Analysing BFS Depth-first search Depth-first - PowerPoint PPT Presentation

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


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

  2. CS 270 General remarks Algorithms Oliver Kullmann Analysing BFS Depth-first We finish BFS, by analysing it. search Analysing Then we consider the second main graph-search algorithm, DFS depth-first search (DFS). Dags and topological And we consider one application of DFS, topological sorting Detecting sorting of graphs. cycles Reading from CLRS for week 5 Chapter 22, Sections 22.2, 22.3, 22.4.

  3. CS 270 Recall: BFS Algorithms Oliver Kullmann Analysing BFS ( G , s ) BFS 1 for each u ∈ V ( G ) Depth-first search 2 d [ u ] = ∞ Analysing 3 π [ s ] = nil DFS Dags and 4 d [ s ] = 0 topological 5 Q = ( s ) sorting Detecting 6 while Q � = () cycles 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 )

  4. CS 270 Analysis of BFS Algorithms Oliver Kullmann Correctness Analysis: At termination of BFS ( G , s ), for every Analysing vertex v reachable from s : BFS Depth-first v has been encountered; search d [ v ] holds the length of the shortest path from s to v ; Analysing DFS π [ v ] represents an edge on a shortest path from v to s . Dags and topological Time Analysis: sorting Detecting The initialisation takes time Θ( V ). cycles Each vertex is Enqueue d once and Dequeue d 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 Dequeue d, so scanning adjacency lists takes time Θ( E ) (altogether). The overall time of BFS is thus Θ( V + E ).

  5. CS 270 Why do we get shortest paths? Algorithms Oliver Is it really true that we get always shortest paths (that is, using Kullmann the minimum number of edges)? Let’s assume that to some Analysing vertex v there exists a shorter path P in G from s to v than BFS found by BFS. Let this length be d ′ < d [ v ]. Depth-first search 1 v � = s , since the distance from s to s is zero (using the path Analysing DFS without an edge), and this is correctly computed by BFS. Dags and topological 2 Consider the predecessor u on that shorter path P . sorting Detecting 3 If also d [ u ] would be wrong (that is, too big), than we cycles 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 BFS are non-decreasing!

  6. CS 270 The role of the queue Algorithms Oliver A crucial property of BFS is that the distances set in step 10 of Kullmann the algorithm are non-decreasing, that is, Analysing BFS Depth-first if we imagine a watch-point set at step 10, search and monitor the stream of values d [ v ], then we will see Analysing DFS 0 , 1 , . . . , 1 , 2 , . . . , 2 , 3 , . . . , 3 , . . . . Dags and topological sorting Why is this the case? This must be due to the queue used — Detecting cycles 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?

  7. CS 270 The role of the queue (cont.) Algorithms Oliver Kullmann First we need to notice that once a distance d [ v ] is set, it is Analysing never changed again. BFS The vertices are taken off the queue (“dequeued”) from the Depth-first search left, and are added (“enqueued”) to the right (“first in, first Analysing out”). DFS Dags and What is added has a distance one more than what was topological sorting taken away. Detecting cycles 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.

  8. CS 270 Running BFS on directed graphs Algorithms We can run BFS also on a digraph G , with start-vertex s : Oliver Kullmann 1 For digraphs we have directed spanning trees/forests . Analysing BFS 2 Only the vertices reachable from s following the directions Depth-first of the edges are in that directed tree (directed from s search towards the leaves). Analysing DFS 3 Still the paths in the directed tree, from s to any other Dags and vertex, are shortest possible (given that we obey the given topological sorting directions of the edges). Detecting cycles For a graph (i.e., undirected graph), we need to restart BFS (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.

  9. � � CS 270 Restart typically needed for digraphs to cover all Algorithms vertices Oliver Kullmann Consider the simple digraph Analysing BFS Depth-first 1 2 search Analysing DFS Dags and 3 topological sorting In order to cover all vertices, one needs to run BFS at least two Detecting cycles 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.

  10. CS 270 Arcs of different lengths Algorithms Oliver Kullmann Analysing BFS The edges of (di-)graphs have (implicitly) a length of one unit. Depth-first search If arbitrary non-negative lengths are allowed, then we have Analysing to generalise BFS to Dijkstra’s algorithm . DFS Dags and This generalisation must keep the essential properties, that topological sorting the distances encountered are the final ones and are Detecting non-decreasing . cycles 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).

  11. CS 270 Depth-first search Algorithms Oliver Kullmann Depth-first search (DFS) is another simple but very important Analysing technique for searching a graph. BFS Depth-first Such a search constructs a spanning forest for the graph, called search the depth-first forest , composed of several depth-first trees , Analysing DFS which are rooted spanning trees of the connected components. Dags and topological DFS recursively visits the next unvisited vertex, thus extending sorting the current path as far as possible; when the search gets stuck in Detecting cycles a “corner” it backtracks up along the path until a new avenue presents itself. DFS 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 search has finished visiting its adjacent vertices).

  12. CS 270 The algorithm Algorithms Oliver Kullmann Analysing BFS DFS ( G ) DFS-Visit ( u ) Depth-first search 1 for each u ∈ V ( G ) 1 time = time + 1 Analysing 2 d [ u ] = ∞ 2 d [ u ] = time DFS Dags and 3 time = 0 3 for each v ∈ Adj [ u ] topological sorting 4 for each u ∈ V ( G ) 4 if d [ v ] = ∞ Detecting 5 if d [ u ] = ∞ 5 π [ v ] = u cycles 6 π [ u ] = nil 6 DFS-Visit ( v ) 7 DFS-Visit ( u ) 7 time = time + 1 8 f [ u ] = time Analysis: DFS-Visit ( u ) is invoked exactly once for each vertex, during which we scan its adjacency list once. Hence DFS , like BFS , runs in time Θ( V + E ).

  13. CS 270 DFS illustrated Algorithms Oliver 1 1 2 − / nil ∞ − / − ∞ − / − ∞ − / − − / nil − / 1 − / − ∞ ∞ − / − Kullmann 1 2 4 6 1 2 4 6 d [ u ] f [ u ] / π [ u ] Analysing u BFS 3 5 7 3 5 7 (labelling) ∞ − / − ∞ − / − ∞ − / − − / − ∞ ∞ − / − ∞ − / − Depth-first Stack = () u = 1 Stack = (1) u = 2 search 1 2 1 2 4 − / nil − / 1 ∞ − / − ∞ − / − − / nil − / 1 − / 3 ∞ − / − Analysing DFS 1 2 4 6 1 2 4 6 Dags and topological 3 5 7 3 5 7 3 3 sorting ∞ − / − ∞ − / − ∞ − / − ∞ − / − − / 2 − / 2 Stack = (2 , 1) u = 3 Stack = (3 , 2 , 1) u = 4 Detecting cycles 1 2 4 1 2 4 − / nil − / 1 − / 3 ∞ − / − − / nil − / 1 − / 3 ∞ − / − 1 2 4 6 1 2 4 6 3 5 7 3 5 7 3 5 3 5 6 ∞ − / − − / 2 − / 4 − / 2 − / 4 − / 5 Stack = (4 , 3 , 2 , 1) u = 5 Stack =(5 , 4 , 3 , 2 , 1) u = 7 1 2 4 9 1 2 4 9 − / nil − / 1 − / 3 − / 4 14 / nil 13 / 1 11 / 3 10 / 4 1 2 4 6 1 2 4 6 3 5 7 3 5 7 3 5 6 3 5 6 − / 2 8 / 4 7 / 5 12 / 2 8 / 4 7 / 5 Stack = (4 , 3 , 2 , 1) u = 6 Stack = ()

Recommend


More recommend