**Unformatted text preview: **Lecture 13, Nov 9 2010 Partial Order Example: subsets ordered by inclusion
» Note implicit relationships, e.g. x ∈ {x,y,z} but there is no edge between them. Acyclic directed graph, vertices ordered by reachability Topological sort: total order compatible with partial order.
» 7,5,3,11,8,2,10,9 » 3,7,8,5,11,10,9,2 Numerous applications, mostly in scheduling: instructions, compilations in makefile, etc.
159 Topological Sort Directed acyclic graph G. Algorithm:
» Call DFS to compute finishing times f[v] for each vertex v. » As each v is finished, insert it onto the front of linked list » Return the linked list. Claim: the output list is a legal topological sort.
» Sufficient to prove that, for every u and v s.t. (uv) is an edge, we have f[v] < f[u]. (Why ??) » Consider edge (uv) explored by DFS. Observe that when (uv) is explored, v cannot be gray ! » (back edge implies cycle) » If v white, it becomes descendant of u, and thus f[v] < f[u]. » If v black, it finished before u started, so again f[v] < f[u].
160 1 Min-Cost Spanning Tree Applications:
» Cable TV, » Circuit layout, » Basic task for many optimization algs (eg. flow). Formally:
» Undirected graph G=(V,E). » Weights w: E R » Goal: find spanning tree of minimum weight. (spanning = connects all nodes in G) (tree weight = sum of weights of tree edges) For simplicity, we will assume single connected component. Straightforward extension to multiple components.
161 Example graph 162 2 Example MST 163 What if we modify edge weight ? X7 X 164 3 Greedy step 165 Greedy step 166 4 Optimum Substructure Assume T is MST of G, A T subtree of T let uv be min-weight edge connecting A to (T-A) ⇒ ∃ MST T 0 s.t. (A ∪ uv ) ⊆ T 0 (loose notation) Graph induced by edges in T but not in A Proof: “Cut-and-Paste” approach”
» u’v’ connects A to (T-A)2 in T » Replace u’v’ by uv, » Claim resulting T’ is optimum MST Questions:
» Why no more edges parallel to u’v’ in T ??
– T cannot have cycles » Why u’v’ exists at all ??
– walk in T until you hit [T-A]2 – No edges in T between [T-A]1 and [T-A]2 167 Prim’s Algorithm Main idea:
» Pick a node v, set A={v}. » Repeat:
– find min-weight edge e, outgoing from A, – add e to A (implicitly add the new node to A as well). Need support for finding an edge that is:
» Outgoing (connecting node in A to node not in A), » Min-weight among all outgoing. 168 5 Implementing Prim’s Alg First try:
» Keep all edges (outgoing and internal) from A in a heap, » new node: add all its edges to the heap. » To get “next edge”:
– – – – extract min-weight from heap check if internal. (how ??) if yes, discard and repeat. If not internal, add to A Time:
» O(E) insertions and O(E) deletions from heap: Total: O(E log V)
169 6 ...

View
Full
Document