6.006 Lecture 14: Depth-First Search (DFS), Topological Sorting

6.006 Lecture 14: Depth-First Search (DFS), Topological Sorting

Lecture 14 Graphs II: DFS 6.006 Fall 2011 Lecture 14: Graphs II: Depth-First Search Lecture Overview • Depth-First Search • Edge Classification • Cycle Testing • Topological Sort Recall: • graph search: explore a graph e.g., find a path from start vertex s to a desired vertex • adjacency lists: array Adj of jV j linked lists { for each vertex u 2 V , Adj[u] stores u's neighbors, i.e., fv 2 V j (u; v) 2 Eg (just outgoing edges if directed) For example: a a c b c a c b c b Adj Figure 1: Adjacency Lists Breadth-first Search (BFS): Explore level-by-level from s | find shortest paths 1 Lecture 14 Graphs II: DFS 6.006 Fall 2011 Depth-First Search (DFS) This is like exploring a maze. s Figure 2: Depth-First Search Frontier Depth First Search Algorithm • follow path until you get stuck • backtrack along breadcrumbs until reach unexplored neighbor • recursively explore • careful not to repeat a vertex parent = {s: None} search from DFS-visit (V, Adj, s): start vertex s start for v in Adj [s]: (only see v if v not in parent: stuff reachable parent [v] = s from s) nish DFS-visit (V, Adj, v) v } DFS (V, Adj) parent = { } explore for s in V: entire graph if s not in parent: parent [s] = None (could do same DFS-visit (V, Adj, s) } to extend BFS) Figure 3: Depth-First Search Algorithm 2 Lecture 14 Graphs II: DFS 6.006 Fall 2011 Example cross edge S 1 1 S2 a b c forward 8 edge 5 2 6 4 back d e f 7 edge 3 back edge Figure 4: Depth-First Traversal Edge Classification tree edges (formed by parent) nontree edges back edge: to ancestor forward edge: to descendant cross edge (to another subtree) Figure 5: Edge Classification • to compute this classification (back or not), mark nodes for duration they are \on the stack" • only tree and back edges in undirected graph Analysis • DFS-visit gets called with a vertex s only once (because then parent[s] set) X =) time in DFS-visit = jAdj[s]j = O(E) s2V • DFS outer loop adds just O(V ) =) O(V + E) time (linear time) 3 Lecture 14 Graphs II: DFS 6.006 Fall 2011 Cycle Detection Graph G has a cycle , DFS has a back edge Proof (<=) tree edges is a cycle back edge: to tree ancestor (=>) consider rst visit to cycle: v3 v2 vk v 1 v0 FIRST! • before visit to vi finishes, will visit vi+1 (& finish): will consider edge (vi; vi+1) =) visit vi+1 now or already did • =) before visit to v0 finishes, will visit vk (& didn't before) • =) before visit to vk (or v0) finishes, will see (vk; v0) as back edge Job scheduling Given Directed Acylic Graph (DAG), where vertices represent tasks & edges represent dependencies, order tasks without violating dependencies 4 Lecture 14 Graphs II: DFS 6.006 Fall 2011 8 7 9 G H I 4 3 2 1 A B C F D E 6 5 Figure 6: Dependence Graph: DFS Finishing Times Source: Source = vertex with no incoming edges = schedulable at beginning (A,G,I) Attempt: BFS from each source: • from A finds A, BH, C, F • from D finds D, BE, CF slow . and wrong! • from G finds G, H • from I finds I Topological Sort 8 > DFS-Visit(v) > > ... Reverse of DFS finishing times (time at which DFS-Visit(v) finishes) <> order.append(v) > > > order.reverse() :> 5 Lecture 14 Graphs II: DFS 6.006 Fall 2011 Correctness For any edge (u; v) | u ordered before v, i.e., v finished before u u v • if u visited before v: { before visit to u finishes, will visit v (via (u; v) or otherwise) { =) v finishes before u • if v visited before u: { graph is acyclic { =) u cannot be reached from v { =) visit to v finishes before visiting u 6 MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Fall 2011 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us