Download presentation
Presentation is loading. Please wait.
1
Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm
CS 341: Algorithms Trevor Brown DC 2338, Office hour M3-4pm
2
This time Depth-first search (DFS) Complexity DFS forest edge types
Application: cycle detection via DFS Application: topological sort via DFS (also without DFS)
3
Depth First Search (DFS)
4
Basic DFS Properties to recall
Also gets a discovery time π[π£] at this point Nodes start white A node π£ turns gray when it is discovered, which is when the first call to π·πΉππππ ππ‘(π£) happens After π£ is turned gray, we recurse on its neighbours After recursing on all neighbours, we turn π£ black Recursive calls on neighbours end first, so neighbours turn black first Also gets a finish time π[π£] at this point
5
Complexity with adjacency lists is π(π+π)
Home exercise: complexity with adjacency matrix?
6
Classifying edges in the DFS forest
Recall the example:
7
What are the edge types in the example graph?
Can we classify edges without inspecting the DFS forest? Perhaps using π[β¦], π β¦ and/or πππππ’π[β¦]?
8
Definitions Definition: we use π° π to denote (π[π’],π[π’]), which we call the interval of π Definition: π is white-reachable from π if there is a path from π’ to π£ containing only white nodes 2 3 12 7 π 4 9 11 13 6 1 8 10 5
9
Exploring d[], f[] and colour[]
Observe: every node π£ that is white-reachable from π’ when we call π·πΉππππ ππ‘(π’) (discover π’) becomes gray after π and black before π (so πΌ π£ is nested inside πΌ π’ ) Note: π£ is discovered during the recursive π·πΉππππ ππ‘ calls, which is equivalent to π being a descendent of π in the DFS forest Start π·πΉππππ ππ‘(π’) and colour π’ grey Perform π·πΉππππ ππ‘ calls recursivelyβ¦ π π π Colour π’ black and return from π·πΉππππ ππ‘(π’)
10
more generally Theorem: Let π,π be any nodes. The following statements are all equivalent. (π£ is discovered during π·πΉππππ ππ‘(π’)) (π£ is white-reachable from π’ when we call π·πΉππππ ππ‘(π’)) (π£ is a descendant of π in the DFS forest) (π£ turns grey after π’ and black before π’) ( πΌ π£ nested inside πΌ π’ )
11
π£ is not a descendent, and not an ancestor
Classification of edge types in DFS DFS inspects every edge in the graph. When DFS inspects an edge {π,π}, the colour of π£ and relationship between the intervals of π’ and π£ determine the edge type. π£ discovered during π«ππΊπ½ππππ(π) but not at the first level of recursion (else {π’,π£} is a tree edge). π π Q1? Q2? π£ is a child of π’ Q4? Q3? π£ is a descendent of π’ Q6? Q5? π£ is an ancestor of π’ Q8? Q7? π£ is not a descendent, and not an ancestor Break up this slide? Recall: (π£ is discovered during π·πΉππππ ππ‘(π’)) β (π£ is white-reachable from π’ when we call π·πΉππππ ππ‘(π’)) β (π£ is a descendant of π in the DFS forest) β (π£ turns grey after π’ and black before π’) β ( πΌ π£ nested inside πΌ π’ ) So the recursive call in π·πΉππππ ππ‘(π’) that discovered π£ must finish (and set π£ black) before π·πΉππππ ππ‘(π’) inspects {π’,π£}
12
useful fact: Parenthesis theorem
Theorem: for each for each pair of nodes π’,π£ the intervals of π’ and π£ are either disjoint or nested Proof: Suppose the intervals are not disjoint. Then either π π£ β πΌ π’ or π[π’]β πΌ π£ WLOG suppose π π£ β πΌ π’ Then π£ is discovered during π·πΉππππ ππ‘(π’), so π£ is white-reachable from π’ when π·πΉππππ ππ‘(π’) starts So, π£ must turn black before π’, so π π£ <π[π’] So the intervals are nested. QED π«ππΊπ½ππππ(π) π[π’] π[π’] π[π£] π[π£] β π[π£] π[π’] π[π’] π[π£] π[π£] β
13
Classification of edge types in DFS
So, πΌ π£ must be earlier. DFS inspects every edge in the graph. When DFS inspects an edge {π,π}, the colour of π£ and relationship between the intervals of π’ and π£ determine the edge type. If πΌ π’ were earlier, then π£ would be discovered before π finishes (because of edge {π’,π£}), so intervals would not be disjoint! π π Intervals π° π and π° π must be disjoint. But which is earlier? Q8? Q7? π£ is not a descendent, and not an ancestor Break up this slide? Recall: (π£ is discovered during π·πΉππππ ππ‘(π’)) β (π£ is white-reachable from π’ when we call π·πΉππππ ππ‘(π’)) β (π£ is a descendant of π in the DFS forest) β ( πΌ π£ nested inside πΌ π’ )
14
DFS Application: Testing whether a graph is a DAG
15
Back edge: points to an ancestor in the DFS forest
16
β¦ Case β : Suppose β directed cycle. Show β back edge.
Let π£ 1 , π£ 2 ,β¦, π£ π , π£ 1 be a directed cycle WLOG let π£ 1 be earliest discovered node in the cycle π π π π = = π π π[ π£ π ] π[ π£ 1 ] π[ π£ 1 ] π[ π£ π ] π[ π£ π ] π[ π£ 1 ] π[ π£ 1 ] π[ π£ π ] π[ π£ 1 ] π[ π£ π ] π[ π£ π ] π[ π£ 1 ] π[ π£ 1 ] π[ π£ 1 ] π[ π£ π ] π[ π£ π ] Discovered before π£ 2 ,β¦, π£ π π£ 2 π£ 2 π£ 3 π£ 3 π£ 1 π£ 1 Recall: every node π£ π that is white-reachable from π£ 1 when we discover π£ 1 (call π·πΉππππ ππ‘( π£ 1 )) turns black before π π (π π£ π <π[ π£ 1 ]) π£ 4 π£ 4 Consider edge { π π , π π } Recall: nodes become gray when discovered Since π π£ 1 <π π£ π , { π π , π π } must be a back or cross edge. Why? π£ π π£ π So π£ π must turn black before π£ 1 , and we have π π£ π <π[ π£ 1 ]. π£ 5 π£ 5 β¦ π£ 6 π£ 6 So when π π is discovered, π£ 2 ,β¦, π£ π are all white Thus, { π π , π π } must be a back edge. QED
17
Turning the lemma into an algorithm
Search for back edges How to identify a back-edge? Back edge When we observe an edge from π’ to π£, check if π is gray π π π
18
DFS: Testing whether a graph is a DAG
20
Home exercise Try running the algorithm on this graph:
21
Topological sort Finding node orderings that satisfy given constraints
22
Example problem: getting dressed in the morning
Dependency graph Edge {π’,π£} means π’ must be completed before π£ Example problem: getting dressed in the morning Watch any time Pants before belt Socks before shoes Could do various things first. Which ones are possible? What do they have in common?
23
Topological sort IFF there is a (directed) cycle!
Might not be possible! How can this happen? Try to order nodes linearly so there are only pointers from left to right! Topological sort
24
more realistic Use cases (1/2)
Compiler design: ordering program compilation steps Not just compilers⦠Linkers, build systems (makefiles, etc.) Can do parse.y, then parse.h, then parse.c, then main.c, then parse.o, then main.o, then prog⦠Cell evaluation-order in spreadsheets⦠Can verify pointers only go left-to-right. Other orders are possible⦠Manufacturing processes, chemical reaction processes, ⦠all sorts of planning and scheduling
25
More realistic use cases (2/2): task scheduling
Goal: output a serial order of tasks that can be run without worrying about dependencies (because all dependencies are already satisfied by the time a task is run). (Nodes are numbered according to one such order.) Can even schedule tasks to run in parallel! Can do 1||2 then 3||13 then 4| 5 |15 etc. Similar to instruction scheduling and pipelining in modern processors!
26
Formal definitions
27
One of these must be repeated. So there is a cycle!
Useful fact One of these must be repeated. So there is a cycle! β¦ π π+π π π π π π π π π
28
Existence of topological sort
Not a very efficient algorithm We can build a more efficient algorithm by using DFS
29
Recall this fact from the DAG-testing application
13 To see why we get a topological ordering, suppose D is a DAG and we order nodes from largest to smallest finish time, so π π π > π π π >β¦> π π πβπ > π π π So edges are left-to-right, hence this is a topological order! β¦ β¦ β¦ π π π π π π π π π π But this contradicts the node ordering! Suppose a right-to-left edge { π£ π , π£ π } exists Since edge { π£ π , π£ π } exists, the lemma implies π π£ π < π π£ π
30
Topological ordering via DFS
31
π(π+π) with adjacency lists
Running time? π(π+π) with adjacency lists We are pushing smallest finishing times first into a stack, so when we pop them out, we will get largest finishing time first Save each node when it finishes Save each node when it finishes
32
Example On the boardβ¦
33
A different algorithm for topological sort
Running time is π(π+π) with adjacency lists
34
Home exercise (Kahnβs algorithm)
Run on this graphβ¦
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.