This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: BFS DFS IE170: Algorithms in Systems Engineering: Lecture 17 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University March 2, 2007 Jeff Linderoth IE170:Lecture 17 BFS DFS Taking Stock Last Time DepthFirst Search This Time: Uses of DFS Topological Sort Strongly Connected Components Jeff Linderoth IE170:Lecture 17 BFS DFS DepthFirst Search DFS Input : Graph G = ( V,E ) No source vertex here. Works for undirected and directed graphs. We focus on directed graphs today... Output : Two timestamps for each node d ( v ) , f ( v ) , Output : π ( v ) , predecessor of v not on shortest path necessarily Jeff Linderoth IE170:Lecture 17 BFS DFS DFS (Initialize and Go) dfs ( V,E ) 1 for each u in V 2 do color ( u ) ← green 3 π ( u ) ← nil 4 time ← 5 for each u in V 6 do if color [ u ] = green 7 then dfsvisit ( u ) Jeff Linderoth IE170:Lecture 17 BFS DFS DFS (Visit Node—Recursive) dfsvisit ( u ) 1 color ( u ) ← yellow 2 d [ u ] ← time ++ 3 for each v in Adj [ u ] 4 do if color [ v ] = green 5 then π [ v ] ← u 6 dfsvisit ( v ) 7 8 color ( u ) ← red 9 f [ u ] = time ++ Jeff Linderoth IE170:Lecture 17 BFS DFS Parenthesis Theorem Let’s look at the intervals: [ d [ v ] ,f [ v ]] for each vertex v ∈ V . (Surely, d [ v ] < f [ v ] ) These tell us about the predecessor relationship in G π 1 If I finish exploring u before first exploring v , ( d [ u ] < f [ v ]) then v is not a descendant of u . (Or versa vice) 2 If [ d [ u ] ,f [ u ]] ⊂ [ d [ v ] ,f [ v ]] then u is a descendent of v in the DFS tree 3 If [ d [ v ] ,f [ v ]] ⊂ [ d [ u ] ,f [ u ]] then v is a descendent of u in the DFS tree Jeff Linderoth IE170:Lecture 17 BFS DFS Analysis of DFS Loop on lines 13 O (  V  ) dfsvisit is called exactly once for each vertex v ( Why? ) Because the first thing you do is paint the node yellow The Loop on lines 36 in calls dfsvisit  Adj [ v ]  times for vertex v ....
View
Full
Document
This note was uploaded on 08/06/2008 for the course IE 170 taught by Professor Ralphs during the Spring '07 term at Lehigh University .
 Spring '07
 Ralphs
 Systems Engineering

Click to edit the document details