1 / 28

Algorithms ( and Datastructures )

Algorithms ( and Datastructures ). Lecture 9 MAS 714 part 2 Hartmut Klauck. Connectivity by Matrix Multiplication. Problem: we can decide connectivity for all pairs , but have not solved APD or APSP!. APD for undirected unweighted graphs.

ringo
Download Presentation

Algorithms ( and Datastructures )

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. Algorithms (andDatastructures) Lecture 9 MAS 714 part 2 Hartmut Klauck

  2. Connectivity by Matrix Multiplication • Problem: wecandecideconnectivityfor all pairs, but have not solvedAPD orAPSP!

  3. APD for undirected unweighted graphs • G is undirected and unweighted, adjacency matrix A • We have an algorithm for integer matrix multiplication • A2[u,v] • The number of paths of length 2 from u to v • New matrix A‘: • A‘[u,v]=1 iff uv and A[u,v]=1 or A2[u,v]> 0 • otherwise A‘[u,v]=0 • A‘[u,v]=1, if path from u to v has length ·2 • one matrix multiplication • G‘ has matrix A‘

  4. An observation • Lemma 1: • (u,v) distance in G, ‘(u,v) in G‘ • If(u,v) even, then(u,v)=2‘(u,v) • If (u,v) odd, then(u,v)=2‘(u,v)-1 • Idea: once we know distances for G‘ AND the parity of the shortest path length in G, we can compute the distances in G

  5. The parities • Lemma 2: • Vertices u,v • For every neighbor w of u:(u,v)-1· (w,v) · (u,v)+1 • There is a neighbor w of u:(w,v)=(u,v)-1

  6. The parities • Lemma 3: • (u,v) even, then ‘(w,v)¸‘(u,v) for all neighbors w of u • (u,v) odd, then ‘(w,v)·‘(u,v) for every neighbor w of u, and there is w with ‘(w,v)<‘(u,v) • Proof: • (u,v)=2k. (w,v)¸2k-1 and ‘(u,v)=k. Then‘(w,v)¸(w,v)/2¸k-1/2, hence±‘(w,v)¸k=‘(u,v) (distances are integers) • Analogous for odd (u,v)

  7. The parities • Lemma 4: • (u) neighbors of u, and d(u) degree • (u,v) is even iffw2(u)‘(w,v)¸‘(u,v)d(u) • (u,v) is odd iffw2(u)‘(w,v)<‘(u,v)d(u)

  8. APD algorithm • Graph G, matrix A • Output: distance matrix D • Let A‘[u,v]=1 iff uv and A[u,v]+A2[u,v]>0, otherwise 0 • If A‘[u,v]=1 for all uv then D=2A‘-A (basecase) • Compute D‘ for A‘ recursively • S=AD‘ • Compute D: • D[u,v]= • 2D‘[u,v], ifS[u,v] ¸D‘[u,v] A2[u,u] • 2D‘[u,v]-1, if S[u,v] < D‘[u,v] A2[u,u]

  9. Running Time • Running time is O(T(n) log n), for the matrix multiplication running time T(n) on n£n matrices • Proof: • diameter(D)=d. Then diameter(G‘) = dd/2e. • Running time L(n,d). • L(n,1) and L(n,2) are O(T(n))+O(n2) • Recursion tree depth is O(log n) • L(n,d)· 2T(n)+L(n,d d/2e)+O(n2) • Remark: matrix entries of size O(n2)

  10. Correctness • Induction over the diameter • Case 1: diameter(G)·2 • D=2A‘-A is correct • Case 2: diameter(G)¸3 , i.e., diameter( A‘)< diameter(A) • (u,v) even: (u,v) =2‘(u,v) =2D‘[u,v] • (u,v) odd:(u,v) =2‘(u,v)-1=2D‘[u,v]-1 • We have to show • (u,v) even iff S[u,v] ¸ D‘[u,v] A2[u,u] • (u,v) oddiff S[u,v] < D‘[u,v] A2[u,u] • S[u,v]=w A[u,w]D‘[w,v]= w2(u)‘(w,v) • By induction all D‘ correct, A2[u,u]=d(u) and Lemma 4 implies correctness

  11. Conclusion • We get an O(n log n) algorithm for APD • Using an O(n ) algorithm for integer matrix multiplication

  12. Minimum spanning trees • Definition • A spanning tree of an undirected connected graph is a set of edges: • edges form a tree • every vertex is in at least one edge • When the edges of G haveweights, thena minimumspanning tree is a spanning tree withthesmallestsum of edgeweights

  13. MST • Motivation: measure costs to establishconnectionsbetweenvertices • Basic procedure in manygraphalgorithms • Problem first studied by Boruvka in 1926 • Other algorithms: Kruskal, Prim • Inputs: adjacency list with weights

  14. Applicationexample • Metric TSP (TravelingSalesman Problem) • weights form a metric • Still NP-complete • 2-approximation: • Find an MST • Traverse the MST in an Euler tour • Makeshortcutstogenerate a Hamiltoniancycle • Euler tour costistwicethe MST cost, henceatmosttwicethe TSP cost, shortcutscannotincreasecost

  15. MST • Generic algorithm: • Start with an empty set of edges • Add edges, such that current edge set is always subset of a minimumspanning tree • Edges that can be added are called safe

  16. Generic Algorithm • Set A=; • As long as A is not (yet) a spanning tree add a safe edge e • Output A

  17. Safe Edges • How can we find safe edges? • Definition: • A cut S, V-S is a partition of V • A set of edges repects the cut, if no edge crosses • An edge is light for a cut, if it is the edge with smallest weight that crosses the cut • An edge is light, if it is light for a set of cuts (e.g. all cuts respected by a edgesetA)

  18. Safe Edges • Theorem:Let G be an undirected connected weighted graph. A asubset of a minimum spanning tree. C=(S, V-S) a cut that A respects.Then the lightest edge of C is safe. • Proof: • T is an MST containing A • Suppose e is not in T (otherwise we are done) • Construct another MST that contains e

  19. Safe Edges • Insertinge={u,v}into T creates a cycle p in T[{e} • u and v areon different sides of the cut • Another edge e‘ in T crosses the cut • e‘ is not in A (A respects the cut) • Remove e‘ from T (T isnowdisconnectedinto 2 trees) • Add e to T (thetwotreesreconnectintoone) • W(e)=W(e‘), so T‘ is also minimal • Hence A[{e} subset of T ‘ • e is safe for A.

  20. Which edges are not in a min. ST? • Theorem: • G a graph with weights W.All edge weights distinct.C a cycle in G and e={u,v} the largest edge in C. • Then e is in no minimum spanning tree. • Proof: • Assume e is in a min. ST T • Remove e from T • Result is two trees (containing all vertices) • The vertices of the two trees form a cut • Follow C-{e} from u to v • Some edge e‘ crosses the cut • T-{e}[{e‘} is a spanning tree with smaller weight T

  21. Algorithms • We complete the algorithm „skeleton“ in two ways • Prim: A is always a tree • Kruskal: A starts as a forest that joins into a single tree • initially every vertex its own tree • join trees until all are joined up

  22. Data structures: Union-Find • We need to store a set ofdisjointsetswith the following operations: • Make-Set(v):generate a set {v}. Name of the set is v • Find-Set(v):Find the name of the set that contains v • Union(u,v):Join the sets named u and v. Name of the new set is either u or v • As with Dijkstra/priority queues the running time will depend on the implementation of the data structure

  23. Kruskal • Input: graph G, weights W:ER • A=; • For each vertex v: • Make-Set(v) • Sort edges in E (increasing) by weight • For all edges{u,v}(order increasing by weight): • a=FindSet(u), b=FindSet(v) • Ifabthen A:=A[{{u,v}} Union(a,b) • Output A

  24. Implementation Union Find • Universeof n elements • UsearrayM with n entries • Sets are represented as trees, by pointers towards the roots • MakeSet(v) forall v: M(v)=v for all v • Union(u,v): set M(v)=u, if the set of u is larger than the set ofv (important!) • Find(v): follow the pointers fromM(v) until M(u)=u, output u • We need to store for each v that is a root also the size of its set • Beginning with 1, update duringUnion

  25. Running times Union Find • MakeSet: O(n) tomake n singleton sets • Union: O(1) • Find: corresponds to maximum depth of trees • Depth is O(log n): • Claim:Trees with size g have depth · log g:Proof: • This is true when trees are generated • Union: Sets u,v join with sizes a,b and depthsq·loga andr·log b, wloga¸b • New tree has size a+b • r < q, then new depthisq · log a · log (a+b) • r = q, thennewdepthisq+1, but a¸ 2q, b¸ 2q, a+b¸2q+2q=2q+1, hencenewdepth·log (a+b) • r > q, thenthenewdepthis r+1. r·log b, b·a, hencer+1· log(2b)· log(a+b)

  26. Kruskal • Input: graph G, weights W:ER • A=; • For each vertex v: • Make-Set(v) • Sort edges in E (increasing) by weight • For all edges{u,v}(order increasing by weight): • a=FindSet(u), b=FindSet(v) • Ifabthen A:=A[{{u,v}} Union(a,b) • Output A

  27. Running time Kruskal • We have: • until 3: O(n) • 4: O(m log n) • 5: O(m log n) • total O(n+m log n)

  28. Correctness • We only have to show that all edges inserted are safe • Choose a cut respected by A, which is crossed by the new edge e • e has minimum weight under all edges forming nocycle, hence e hasminimumweightamong all edgescrossingthecut • Hence e must besafefor A

More Related