lecture 9
play

Lecture 9 Graphs, BFS and DFS Announcements! HW4 due MONDAY - PowerPoint PPT Presentation

Lecture 9 Graphs, BFS and DFS Announcements! HW4 due MONDAY (Notice the change) But you might want to get it in on Friday anyway, so that you can study for the. MIDTERM Wednesday May 10. In this room, during class (3-4:20)


  1. Depth First Search • DFS (w, currentTime): currentTime = 1 • w.entryTime = currentTime D • currentTime ++ • Mark w as . • for v in w.neighbors: A w • if v is : Start:0 • currentTime = DFS (v, currentTime) C • currentTime ++ • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress all done

  2. Depth First Search • DFS (w, currentTime): currentTime = 1 • w.entryTime = currentTime D • currentTime ++ • Mark w as . • for v in w.neighbors: A • if v is : Start:0 • currentTime = DFS (v, currentTime) w C • currentTime ++ • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress all done

  3. Depth First Search • DFS (w, currentTime): currentTime = 2 • w.entryTime = currentTime D • currentTime ++ • Mark w as . • for v in w.neighbors: A • if v is : Start:0 • currentTime = DFS (v, currentTime) w C • currentTime ++ Start: 1 • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress all done

  4. Depth First Search • DFS (w, currentTime): currentTime = 20 • w.entryTime = currentTime D • currentTime ++ • Mark w as . • for v in w.neighbors: A • if v is : Start:0 • currentTime = DFS (v, currentTime) C • currentTime ++ Start: 1 • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress Takes until currentTIme = 20 all done

  5. Depth First Search • DFS (w, currentTime): currentTime = 21 • w.startTime = currentTime D • currentTime ++ • Mark w as . • for v in w.neighbors: A • if v is : Start:0 • currentTime = DFS (v, currentTime) w C • currentTime ++ Start: 1 End: 21 • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress Takes until currentTIme = 20 all done

  6. Depth First Search • DFS (w, currentTime): currentTime = 21 • w.startTime = currentTime etc D • currentTime ++ • Mark w as . • for v in w.neighbors: w A • if v is : Start:0 • currentTime = DFS (v, currentTime) C • currentTime ++ Start: 1 End: 21 • w.finishTime = currentTime unvisited • Mark w as • return currentTime in progress Takes until currentTIme = 20 all done

  7. DFS finds all the nodes reachable from the starting point In an undirected graph, this is called a connected component. start One application: finding connected components .

  8. Why is it called depth-first? • We are implicitly building a tree: YOINK! D E A F B A E C B Call this the G “DFS tree” G C • And first we go as deep as we can. D F

  9. Running time To explore just the connected component we started in • We look at each edge only once. • And basically don’t do anything else. • So… O(m) • (Assuming we are using the linked-list representation) Verify this formally! Ollie the over-achieving ostrich

  10. Running time To explore the whole thing • Explore the connected components one-by-one. • This takes time O(n + m) or

  11. You check: DFS works fine on directed graphs too! C A B Only walk to C, not to B. Ollie the over-achieving ostrich

  12. Application: topological sorting • Example: package dependency graph • Question: in what order should I install packages? coreutils multiarch -support tar dpkg libselinux1 libbz2 Suppose the dependency graph has no cycles: it is a Directed Acyclic Graph (DAG)

  13. Can’t always eyeball it.

  14. Application: topological sorting • Example: package dependency graph • Question: in what order should I install packages? coreutils multiarch -support tar dpkg libselinux1 libbz2

  15. Observations : Let’s do DFS • The start times don’t seem that useful. • But the packages we should include earlier have larger finish times. start:9 finish:10 coreutils start:7 finish:8 multiarch -support tar start:3 finish:4 dpkg start:0 finish:11 libselinux1 start:2 libbz2 start:1 finish:5 finish:6

  16. Suppose the underlying graph has no cycles This is not an accident Claim: In general, we’ll always have: A B finish: [larger] finish: [smaller] To understand why, let’s go back to that DFS tree.

  17. A more general statement (check this statement (t (this is h hold lds e even if t if there ar are c cycle les) carefully!) This is called the “parentheses theorem” in CLRS If v is a descendent of w in this tree: • w.start v.start v.finish w.finish timeline w w If w is a descendent of v in this tree: • w.finish v.finish w.start v.start If neither are descendents of each other: • w.start v.start v.finish w.finish v v (or the other way around)

  18. A B If So to prove this -> Then B.finishTime < A.finishTime Suppose the underlying graph has no cycles • Since the graph has no cycles, B must be a descendent of A in that tree. • All edges go down the tree. • Then A.finishTime B.startTime A.startTime B.finishTime • aka, B.finishTime < A.finishTime.

  19. Back to this problem • Example: package dependency graph • Question: in what order should I install packages? coreutils multiarch -support tar dpkg libselinux1 libbz2

  20. In reverse order of finishing time • Do DFS • dpkg • Maintain a list of packages, in the • coreutils • order you want to install them. tar • libbz2 • When you mark a vertex as all done , • libselinux1 put it at the beginning of the list. • multiarch_support start:9 finish:10 coreutils start:3 start:7 multiarch finish:8 -support tar finish:4 dpkg start:0 finish:11 libselinux1 start:2 libbz2 start:1 finish:5 finish:6

  21. What did we just learn? • DFS can help you solve the topological sorting problem • That’s the fancy name for the problem of finding an ordering that respects all the dependencies • Thinking about the DFS tree is helpful.

  22. Example: B Unvisited In progress All done A Start:0 C D

  23. Example B Unvisited In progress All done A Start:0 C Start:1 D

  24. Example B Unvisited In progress All done A Start:0 C Start:1 D Start:2

  25. Example Start:3 B Unvisited In progress All done A Start:0 C Start:1 D Start:2

  26. Example Start:3 B Leave:4 Unvisited In progress All done A Start:0 C Start:1 B D Start:2

  27. Example Start:3 B Leave:4 Unvisited In progress All done A Start:0 C Start:1 B D D Start:2 Leave:5

  28. Example Start:3 B Leave:4 Unvisited In progress All done A Start:0 C Start:1 Leave: 6 B D C D Start:2 Leave:5

  29. Example Start:3 B Leave:4 Unvisited In progress All done A Start:0 Leave: 7 C Start:1 Do them in this order: Leave: 6 A B D C D Start:2 Leave:5

  30. Another use of DFS • In-order enumeration of binary search trees Given a binary search 5 tree, output all the nodes in order. 3 7 8 2 4 Instead of outputting a node when you are done with it, output it when you are done with the left child and before 1 you begin the right child.

  31. Part 2: breadth-first search

  32. How do we explore a graph? If we can fly 5 4 1 3 7 8 6 2 9

  33. Breadth-First Search Exploring the world with a bird’s-eye view Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

  34. Breadth-First Search Exploring the world with a bird’s-eye view Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

  35. Breadth-First Search Exploring the world with a bird’s-eye view Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

  36. Breadth-First Search Exploring the world with a bird’s-eye view Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

  37. Breadth-First Search Exploring the world with a bird’s-eye view Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

  38. Same disclaimer as for DFS: you may have seen other ways to implement this, this will be convenient for us. Breadth-First Search Exploring the world with pseudocode L i is the set of nodes • Set L i = {} for i=1,…,n we can reach in i • L 0 = {w}, where w is the start node steps from w • For i = 0, …, n-1: • For u in L i : • For each v which is a neighbor of u: - • If u isn’t yet visited: L 0 • mark u as visited, and put it in L i+1 L 1 L 2 Go through all the nodes in L i and add their L 3 unvisited neighbors to L i+1

  39. BFS also finds all the nodes reachable from the starting point start It is also a good way to find all the connected components.

  40. Running time To explore the whole thing • Explore the connected components one-by-one. • Same argument as DFS: running time is O(n + m) Verify these! • Like DFS, BFS also works fine on directed graphs. Ollie the over-achieving ostrich

  41. Why is it called breadth-first? • We are implicitly building a tree: YOINK! L 0 D A E L 1 F B E A L 2 F B C D G L 3 Call this the G C “BFS tree” • And first we go as broadly as we can.

  42. Application: shortest path • How long is the shortest path between w and v? w v

  43. Application: shortest path • How long is the shortest path between w and v? Not been there yet Can reach there in zero steps Can reach there in w one step Can reach there in two steps v Can reach there in three steps It’s three!

  44. To find the distance between w and all other vertices v The distance between • Do a DFS starting at w two vertices is the length of the shortest path between them. • For all v in L i (the i’th level of the BFS tree) • The shortest path between w and v has length i • A shortest path between w and v is given by the path in the BFS tree. • If we never found v, the distance is infinite.

  45. Just the idea…see Proof idea CLRS for details! • Suppose by induction it’s true for vertices in L 0, L 1, L 2 • For all i < 3, the vertices in L i have distance i from v. • Want to show : it’s true for vertices of distance 3 also. • aka, the shortest path between w and v has length 3. • Well, it has distance Not been there at most 3 Can reach there • Since we just found a in zero steps path of length 3 Can reach there • And it has distance at w in one step least 3 Can reach there • Since if it had distance in two steps i < 3, it would have Can reach there v been in L i. in three steps

  46. What did we just learn? • The BFS tree is useful for computing distances between pairs of vertices. • We can find the shortest path between u and v in time O(m). The BSF tree is also helpful for: • Testing if a graph is bipartite or not.

  47. Application: testing if a graph is bipartite • Bipartite means it looks like this: Can color the vertices red and orange so that there are no edges between any same-colored vertices Example: are students are classes if the student is enrolled in the class

  48. Is this graph bipartite?

  49. How about this one?

  50. How about this one?

  51. Solution using BFS • Color the levels of the BFS tree in alternating colors. • If you ever color a node so that you A never color two connected nodes the same, then it is bipartite. • Otherwise, it’s not. B E F C D G

  52. Breadth-First Search For testing bipartite-ness Not been there yet Can reach there in zero steps Can reach there in one step start Can reach there in two steps Can reach there in three steps

Recommend


More recommend