Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
ECE250: Algorithms and Data Structures
Elementary Graph Algorithms– Part B
Materials from CLRS: Chapter 22.3
Ladan Tahvildari, PEng, SMIEEE Professor
Software Technologies Applied Research (STAR) Group
Dept. of Elect. & Comp. Eng.
University of Waterloo
Acknowledgements
v The following resources have been used to prepare materials for this course: Ø MIT OpenCourseWare Ø Introduction To Algorithms (CLRS Book) Ø Data Structures and Algorithm Analysis in C++ (M. Wiess) Ø Data Structures and Algorithms in C++ (M. Goodrich)
v Thanks to many people for pointing out mistakes, providing suggestions, or helping to improve the quality of this course over the last ten years: Ø http://www.stargroup.uwaterloo.ca/~ece250/
acknowledgment/ Lecture 25 ECE250 2
Lecture 25 ECE250 3
Depth-First Search v A depth-first search (DFS) in a graph G is like
wandering in a labyrinth with a string and a can of paint Ø We start at vertex s, tying the end of our string to the point
and painting s “visited (discovered)”. Next we label s as our current vertex called u
Ø Now, we travel along an arbitrary edge (u,v)
Ø If edge (u,v) leads us to an already visited vertex v we return to u
Ø If vertex v is unvisited, we unroll our string, move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps
ECE250 4
Depth-First Search (cont’)
v Eventually, we will get to a point where all incident edges on u lead to visited vertices
v We then backtrack by unrolling our string to a previously visited vertex v. Then v becomes our current vertex and we repeat the previous steps
Lecture 25
ECE250 5
DFS Algorithm
Visit all children recursively
DFS(G) 01 for each vertex u ∈ G.V() 02 u.setcolor(white)03 u.setparent(NIL) 04 time ← 0 05 for each vertex u ∈ G.V() 06 If u.color() = white then DFS-Visit(u) DFS-Visit(u) 01 u.setcolor(gray) 02 time ← time + 1 03 u.setd(time) 04 for each v ∈ u.adjacent() 05 if v.color() = white then 06 v.setparent(u) 07 DFS-Visit(v) 08 u.setcolor(black) 09 time ← time + 1 10 u.setf(time)
Init all vertices Make sure all vertices are visited
Lecture 25
ECE250 6
DFS Algorithm (cont’)
v Initialize – color all vertices white
v Visit each and every white vertex using DFS-Visit
v Each call to DFS-Visit(u) roots a new tree of the depth-first forest at vertex u
v When DFS returns, every vertex u is assigned Ø a discovery time d[u], and a finishing time f[u]
Lecture 25
ECE250 7
DFS Timestamping
v Vertex u is Ø white before time d[u] Ø gray between time d[u] and time f[u], and Ø black thereafter
v Notice the structure througout the algorithm Ø gray vertices form a linear chain Ø correponds to a stack of vertices that have not
been exhaustively explored (DFS-Visit started but not yet finished)
Lecture 25
ECE250 9
DFS Algorithm Running Time
v Running time Ø the loops in DFS take time Θ(V) each, excluding the time to
execute DFS-Visit Ø DFS-Visit is called once for every vertex
§ its only invoked on white vertices, and § paints the vertex gray immediately
Ø for each DFS-visit a loop interates over all v.adjacent()
Ø the total cost for DFS-Visit is Θ(E) Ø the running time of DFS is Θ(V+E)
. () ( )v Vv adjacent E
∈
=Θ∑
Lecture 25
ECE250 10
Generic Graph Search
v BFS, when GrayVertices is a Queue v DFS, when GrayVertices is a Stack
GenericGraphSearch(G,s) 01 for each vertex u ∈ G.V() 02 u.setcolor(white) 03 u.setparent(NIL) 04 s.setcolor(gray) 05 GrayVertices.init() 06 GrayVertices.add(s) 07 while not GrayVertices.isEmpty()08 u ← GrayVertices.remove() 09 for each v ∈ u.adjacent() do 10 if v.color() = white then 11 v.setcolor(gray) 12 v.setparent(u) 13 GrayVertices.add(v) 14 u.setcolor(black)
Lecture 25
ECE250 11
DFS Parenthesis Theorem
v Discovery and finish times have parenthesis structure Ø represent discovery of u with left parenthesis "(u" Ø represent finishin of u with right parenthesis "u)" Ø history of discoveries and finishings makes a well-formed
expression (parenthesis are properly nested)
v Intuition for proof: any two intervals are either disjoint or enclosed Ø Overlaping intervals would mean finishing ancestor, before
finishing descendant or starting descendant without starting ancestor
Lecture 25
ECE250 13
DFS Edge Classification
v Tree edge (gray to white) Ø Edges in depth-first forest
v Back edge (gray to gray) Ø from descendant to ancestor in depth-first tree
Edge (u,v) is tree edge if v was first discovered by exploring edge (u,v)
Edge (u,v) connecting u to an ancestor v in depth first tree.
Lecture 25
ECE250 14
DFS Edge Classification (cont’)
v Forward edge (gray to black) Ø Nontree edge from ancestor to descendant in
depth-first tree v Cross edge (gray to black)
Ø remainder – between trees or subtrees
Between vertices in same depth first tree (one vertex is not ancestor of the other) or ...
Lecture 25