1 / 130

CS4413 Graph Algorithms II (These materials are used in the classroom only)

CS4413 Graph Algorithms II (These materials are used in the classroom only). Graph Searching. Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph Pick a vertex as the root

idania
Download Presentation

CS4413 Graph Algorithms II (These materials are used in the classroom only)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS4413 Graph Algorithms II (These materials are used in the classroom only)

  2. Graph Searching • Given: a graph G = (V, E), directed or undirected • Goal: methodically explore every vertex and every edge • Ultimately: build a tree on the graph • Pick a vertex as the root • Choose certain edges to produce a tree • Note: might also build a forest if graph is not connected

  3. Breadth-First Search • “Explore” a graph, turning it into a tree • One vertex at a time • Expand frontier of explored vertices across the breadth of the frontier • Builds a tree over the graph • Pick a source vertex to be the root • Find (“discover”) its children, then their children, etc.

  4. Breadth-First Search • Again will associate vertex “colors” to guide the algorithm • White vertices have not been discovered • All vertices start out white • Grey vertices are discovered but not fully explored • They may be adjacent to white vertices • Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices • Explore vertices by scanning adjacency list of grey vertices

  5. Breadth-First Search BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What does v->d represent? What does v->p represent?

  6. Breadth-First Search: Example r s t u         v w x y

  7. Breadth-First Search: Example r s t u  0       v w x y Q: s

  8. Breadth-First Search: Example r s t u 1 0    1   v w x y Q: w r

  9. Breadth-First Search: Example r s t u 1 0 2   1 2  v w x y Q: r t x

  10. Breadth-First Search: Example r s t u 1 0 2  2 1 2  v w x y Q: t x v

  11. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2  v w x y Q: x v u

  12. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y

  13. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y

  14. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y

  15. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø

  16. Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the running time? Total running time: O(V+E)

  17. BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the storage cost in addition to storing the tree? Total space used: O(max(degree(v))) = O(V)

  18. Breadth-First Search: Properties • BFS calculates the shortest-path distance to the source node • Shortest-path distance (s,v) = minimum number of edges from s to v, or  if v not reachable from s • Proof given in the book (p. 535-538) • BFS builds breadth-first tree, in which paths to root represent shortest paths in G • Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

  19. Review: Graph Searching • Given: a graph G = (V, E), directed or undirected • Goal: methodically explore every vertex and every edge • Ultimately: build a tree on the graph • Pick a vertex as the root • Choose certain edges to produce a tree • Note: might also build a forest if graph is not connected

  20. 22.2 Breadth-first search Analysis: O(V+E )

  21. The operation of BFS

  22. The operation of BFS

  23. The operation of BFS

  24. Shortest paths

  25. PRINT_PATH(G, s, v ) 1 if v = s 2 then print s 3 else ifπ[v]=NIL 4 then print “no path from” s “to” v “exist” 5 else PRINT-PATH(G, s, π[v]) 6 print v

  26. Breadth-First Search • Input: Graph G = (V, E), either directed or undirected, and source vertex s∈V. • Output: d[v] = distance (smallest # of edges) from sto v, for all v ∈ V. Also π[v] = usuch that (u, v)is last edge on shortest path • uis v’s predecessor. • set of edges {(π[v], v) : v = s} forms a tree. • Later, a breadth-first search will be generalized with edge weights. Now, let’s keep it simple. • Compute only d[v], not π[v]. • Omitting colors of vertices. • Idea: Send a wave out from s. • First hits all vertices 1 edge from s. • From there, hits all vertices 2 edges from s. • Etc. • Use FIFO queue Q to maintain wavefront. • v ∈ Qif and only if wave has hit v but has not come out of vyet.

  27. Example: undirected graph

  28. Example: directed graph • Can show that Qconsists of vertices with dvalues. i i i . . . i i+1 i+1 . . . i+1 • Only 1 or 2 values. • If 2, differ by 1 and all smallest are first. • Since each vertex gets a finite dvalue at most once, values assigned to vertices are monotonically increasing over time. • Actual proof of correctness is a bit trickier. See book. • BFS may not reach all vertices. • Time = O(V + E). • O(V)because every vertex enqueued at most once. • O(E)because every vertex dequeued at most once and we examine (u, v)only when uis dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

  29. Review: Breadth-First Search • “Explore” a graph, turning it into a tree • One vertex at a time • Expand frontier of explored vertices across the breadth of the frontier • Builds a tree over the graph • Pick a source vertex to be the root • Find (“discover”) its children, then their children, etc.

  30. Review: Breadth-First Search • Again will associate vertex “colors” to guide the algorithm • White vertices have not been discovered • All vertices start out white • Grey vertices are discovered but not fully explored • They may be adjacent to white vertices • Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices • Explore vertices by scanning adjacency list of grey vertices

  31. Review: Breadth-First Search BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What does v->d represent? What does v->p represent?

  32. Breadth-First Search: Example r s t u         v w x y

  33. Breadth-First Search: Example r s t u  0       v w x y Q: s

  34. Breadth-First Search: Example r s t u 1 0    1   v w x y Q: w r

  35. Breadth-First Search: Example r s t u 1 0 2   1 2  v w x y Q: r t x

  36. Breadth-First Search: Example r s t u 1 0 2  2 1 2  v w x y Q: t x v

  37. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2  v w x y Q: x v u

  38. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y

  39. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y

  40. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y

  41. Breadth-First Search: Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø

  42. Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the running time? Total running time: O(V+E)

  43. BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } } What will be the storage cost in addition to storing the graph? Total space used: O(max(degree(v))) = O(V)

  44. Breadth-First Search: Properties • BFS calculates the shortest-path distance to the source node • Shortest-path distance (s,v) = minimum number of edges from s to v, or  if v not reachable from s • Proof given in the book (p. 472-5) • BFS builds breadth-first tree, in which paths to root represent shortest paths in G • Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

  45. Definition: Minimum Spanning Tree • A spanning tree whose weight is minimum over all spanning trees is called a minimum spanning tree, or MST. • Example: • In this example, there is more than one MST. Replace edge (b,c) by (a,h). Get a different spanning tree with the same weight.

  46. Growing a Minimum Spanning Tree • Some properties of an MST: • It has |V| -1 edges. • It has no cycles. • It might not be unique. • Building up the solution • We will build a set A of edges. • Initially, A has no edges. • As we add edges to A, maintain a loop invariant: • Loop invariant: A is a subset of some MST. • Add only edges that maintain the invariant. If A is a subset of some MST, an edge (u, v) is safe for A if and only if A ∪ {(u, v)} is also a subset of some MST. So we will add only safe edges.

  47. .. continued • Generic MST algorithm Use the loop invariant to show that this generic algorithm works. Initialization: The empty set trivially satisfies the loop invariant. Maintenance: Since we add only safe edges, A remains a subset of some MST. Termination: All edges added to A are in an MST, so when we stop, A is a spanning tree that is also an MST

More Related