Create Presentation
Download Presentation

Download

Download Presentation

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

171 Views
Download Presentation

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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 • Choose certain edges to produce a tree • Note: might also build a forest if graph is not connected**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.**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**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?**Breadth-First Search: Example**r s t u v w x y**Breadth-First Search: Example**r s t u 0 v w x y Q: s**Breadth-First Search: Example**r s t u 1 0 1 v w x y Q: w r**Breadth-First Search: Example**r s t u 1 0 2 1 2 v w x y Q: r t x**Breadth-First Search: Example**r s t u 1 0 2 2 1 2 v w x y Q: t x v**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 v w x y Q: x v u**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø**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)**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)**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**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**22.2 Breadth-first search**Analysis: O(V+E )**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**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.**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.**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.**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**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?**Breadth-First Search: Example**r s t u v w x y**Breadth-First Search: Example**r s t u 0 v w x y Q: s**Breadth-First Search: Example**r s t u 1 0 1 v w x y Q: w r**Breadth-First Search: Example**r s t u 1 0 2 1 2 v w x y Q: r t x**Breadth-First Search: Example**r s t u 1 0 2 2 1 2 v w x y Q: t x v**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 v w x y Q: x v u**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: y**Breadth-First Search: Example**r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø**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)**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)**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**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.**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.**.. 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