Final Review Depth-First Search Vs

Final Review Depth-First Search Vs

Final review Depth-first search vs. Breadth-first search Depth-first search. Put unvisited vertices on a stack. Breadth-first search. Put unvisited vertices on a queue. source set of marked vertices s Connected component: maximal set of connected vertices. DFS marks all vertices connected to s in time v proportional to the sum of their degrees. no such edge set of can exist unmarked vertices x w BFS finds path from s to t that uses fewest number of edges. Breadth-frst Intuition.maze exploration BFS examines vertices in increasing distance from s. 2 ‣ directed graphs 3 Depth-first search in digraphs Same method as for undirected graphs. • Every undirected graph is a digraph (with edges in both directions). • DFS is a digraph algorithm. DFS (to visit a vertex v) Mark v as visited. Recursively visit all unmarked vertices w adjacent from v. 4 Breadth-first search in digraphs Same method as for undirected graphs. • Every undirected graph is a digraph (with edges in both directions). • BFS is a digraph algorithm. s BFS (from source vertex s) w Put s onto a FIFO queue, and mark s as visited. Repeat until the queue is empty: - remove the least recently added vertex v - for each unmarked vertex adjacent from v: add to queue and mark as visited.. v Is w reachable from v in this digraph? Proposition. BFS computes shortest paths (fewest number of edges). 5 Topological sort DAG. Directed acyclic graph. Topological sort. Redraw DAG so all edges point up. 0→5 0→2 0 0→1 3→6 3→5 3→4 2 5 1 5→4 6→4 6→0 3→2 1→4 3 4 6 directed edges DAG Solution. Reverse DFS Postorder! topological order 6 Reverse DFS postorder in a DAG marked[] reversePost 0 dfs(0) 1 0 0 0 0 0 0 - dfs(1) 1 1 0 0 0 0 0 - dfs(4) 1 1 0 0 1 0 0 - 2 5 1 4 done 1 1 0 0 1 0 0 4 1 done 1 1 0 0 1 0 0 4 1 3 4 dfs(2) 1 1 1 0 1 0 0 4 1 2 done 1 1 1 0 1 0 0 4 1 2 6 dfs(5) 1 1 1 0 1 1 0 4 1 2 check 2 1 1 1 0 1 1 0 4 1 2 5 done 1 1 1 0 1 1 0 4 1 2 5 0→5 0 done 1 1 1 0 1 1 0 4 1 2 5 0 0→2 check 1 1 1 1 0 1 1 0 4 1 2 5 0 check 2 1 1 1 0 1 1 0 4 1 2 5 0 0→1 dfs(3) 1 1 1 1 1 1 0 4 1 2 5 0 3→6 check 2 1 1 1 1 1 1 0 4 1 2 5 0 3→5 check 4 1 1 1 1 1 1 0 4 1 2 5 0 3→4 check 5 1 1 1 1 1 1 0 4 1 2 5 0 dfs(6) 1 1 1 1 1 1 1 4 1 2 5 0 5→4 6 done 1 1 1 1 1 1 1 4 1 2 5 0 6 6→4 3 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3 6→0 check 4 1 1 1 1 1 1 0 4 1 2 5 0 6 3 3→2 check 5 1 1 1 1 1 1 0 4 1 2 5 0 6 3 check 6 1 1 1 1 1 1 0 4 1 2 5 0 6 3 reverse DFS 1→4 done 1 1 1 1 1 1 1 4 1 2 5 0 6 3 postorder is a topological order! 7 Strongly-connected components Def. Vertices v and w are strongly connected if there is a directed path from v to w and a directed path from w to v. Key property. Strong connectivity is an equivalence relation: • v is strongly connected to v. • If v is strongly connected to w, then w is strongly connected to v. • If v is strongly connected to w and w to x, then v is strongly connected to x. Def. A strong component is a maximal subset of strongly-connected vertices. A graph and its connected components A digraph and its strong components 8 Kosaraju's algorithm Simple (but mysterious) algorithm for computing strong components. • Run DFS on GR to compute reverse postorder. • Run DFS on G, considering vertices in order given by first DFS. DFS in reverse digraph (ReversePost) DFS in original digraph G dfs(1) dfs(0) dfs(11) dfs(6) dfs(7) 1 done dfs(5) check 4 check 9 check 6 dfs(12) check 4 dfs(8) check unmarked vertices in the order check unmarked vertices in the order dfs(4) dfs(3) dfs(9) check 0 check 7 0 1 2 3 4 5 6 7 8 9 10 11 12 1 0 2 4 5 3 11 9 12 10 6 7 8 check 5 check 11 6 done check 9 dfs(2) dfs(10) 8 done dfs(0) dfs(1) check 0 check 12 7 done check 8 dfs(6) 1 done check 3 10 done 9 done dfs(7) dfs(0) 2 done 3 done 12 done dfs(8) dfs(5) check 2 11 done check 7 dfs(4) 4 done check 9 8 done dfs(3) 5 done check 12 7 done check 5 check 1 check 10 6 done dfs(2) 0 done check 2 dfs(2) check 0 check 4 dfs(4) check 3 check 5 dfs(11) 2 done check 3 dfs(9) 3 done dfs(12) Second check 2 DFS gives strong components. (!!) check 11 4 done dfs(10) 5 done strong check 9 check 1 components 10 done 0 done 9 12 done check 2 check 8 check 4 check 6 check 5 9 done check 3 11 done dfs(11) check 6 check 4 dfs(5) dfs(12) dfs(3) dfs(9) check 4 check 11 check 2 dfs(10) 3 done check 12 check 0 10 done 5 done 9 done 4 done 12 done check 3 11 done 2 done check 9 0 done check 12 dfs(1) check 10 check 0 dfs(6) 1 done check 9 check 2 check 4 check 3 check 0 check 4 reverse 6 done check 5 postorder dfs(7) check 6 for use check 6 check 7 in second dfs(8) check 8 dfs() check 7 check 9 (read up) check 9 check 10 8 done check 11 7 done check 12 check 8 Kosaraju’s algorithm for fnding strong components in digraphs ‣ minimum spanning trees 10 Minimum spanning tree Given. Undirected graph G with positive edge weights (connected). Def. A spanning tree of G is a subgraph T that is connected and acyclic. Goal. Find a min weight spanning tree. 24 4 23 6 9 18 5 11 16 8 7 10 14 21 spanning tree T: cost = 50 = 4 + 6 + 8 + 5 + 11 + 9 + 7 Brute force. Try all spanning trees? 11 Kruskal's algorithm Kruskal's algorithm. [Kruskal 1956] Consider edges in ascending order of weight. Add the next edge to the tree T unless doing so would create a cycle. next MST edge is red graph edges sorted by weight MST edge (black) 0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 obsolete edge (gray) grey vertices are a cut defined by the vertices connected to one of the red edge’s vertices 12 Trace of Kruskal’s algorithm Kruskal's algorithm: implementation challenge Challenge. Would adding edge v–w to tree T create a cycle? If not, add it. Efficient solution. Use the union-find data structure. • Maintain a set for each connected component in T. • If v and w are in same set, then adding v–w would create a cycle. • To add v–w to T, merge sets containing v and w. w v w v Case 1: adding v–w creates a cycle Case 2: add v–w to T and merge sets containing v and w 13 Prim's algorithm Prim's algorithm. [Jarník 1930, Dijkstra 1957, Prim 1959] Start with vertex 0 and greedily grow tree T. At each step, add to T the min weight edge with exactly one endpoint in T. edges with exactly one endpoint in T (sorted by weight) 0-7 0.16 1-7 0.19 0-2 0.26 0-2 0.26 0-2 0.26 5-7 0.28 0-4 0.38 5-7 0.28 1-3 0.29 6-0 0.58 2-7 0.34 1-5 0.32 4-7 0.37 2-7 0.34 0-4 0.38 1-2 0.36 6-0 0.58 4-7 0.37 0-4 0.38 0-6 0.58 2-3 0.17 5-7 0.28 4-5 0.35 5-7 0.28 1-5 0.32 4-7 0.37 1-3 0.29 4-7 0.37 0-4 0.38 1-5 0.32 0-4 0.38 6-2 0.40 4-7 0.37 6-2 0.40 3-6 0.52 0-4 0.38 3-6 0.52 6-0 0.58 6-2 0.40 6-0 0.58 6-0 0.58 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 Trace of Prim’s algorithm 14 Prim's algorithm: lazy implementation Challenge.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    56 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