1 / 61

Minimum weight spanning trees

Minimum weight spanning trees. 4. 3. 2. 3. 5. 7. 1. 4. 6. 6. 2. Minimum weight spanning trees. 4. 3. 2. 3. 5. 7. 1. 4. 6. 6. 2. Minimum weight spanning trees. 4. 3. 2. 3. 5. 7. 1. 4. 6. 6. 2. Minimum weight spanning trees. 4. 3. 2. 3. 5. 7. 1. 4. 6. 6.

Download Presentation

Minimum weight spanning trees

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. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  2. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  3. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  4. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  5. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  6. Minimum weight spanning trees 4 3 2 3 5 7 1 4 6 6 2

  7. Minimum weight spanning trees Prim’s algorithm 1) S  {1} 2) add the cheapest edge {u,v} such that uS and vSC S S{v} (until S=V)

  8. Minimum weight spanning trees 1) S  {1} 2) add the cheapest edge {u,v} such that uS and vSC S S{v} (until S=V) P = MST output by Prim T = optimal MST Is P = T ? assume all the edgeweights different

  9. Minimum weight spanning trees P = MST output by Prim T = optimal MST P = T assuming all the edgeweights different v1,v2,...,vn order added to S by Prim smallest i such that the edge e E used by Prim to connect vi+1 is not in T.

  10. Minimum weight spanning trees S S={v1,...,vi} look at T vi+1 f smallest i such that the edge e E used by Prim to connect vi+1 is not in T.

  11. Minimum weight spanning trees S={v1,...,vi} look at T+e S e w(f) > w(e) vi+1 f smallest i such that the edge e E used by Prim to connect vi+1 is not in T.

  12. Minimum weight spanning trees S={v1,...,vi} look at T+e-f S e w(f) > w(e) vi+1 f smallest i such that the edge e E used by Prim to connect vi+1 is not in T.

  13. Prim’s algorithm for i  1 to n do C[i]  C[0]  0 S  {} while S V do j  with minimal C[j] over jSC S S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

  14. Prim’s algorithm for i  1 to n do C[i]  C[0]  0 S  {} while S V do j  with minimal C[j] over jSC S S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j Extract-Min O(log n) time O(E+V log V) Decrease-Key O(1) time (amortized)

  15. Shortest path problem 4 3 2 1 2 7 1 A 4 6 8 2 B

  16. Shortest path problem 4 3 2 1 2 7 1 A 4 6 8 2 B

  17. Longest path problem ? 4 3 2 1 2 7 1 A 4 6 8 2 B

  18. Longest path problem ? path from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E no repeated vertices tour from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E no repeated edges walk from u to v = sequence v1,...,vn such that v1 = u, v2 = v, {vi,vi+1} E • walk from u to v  exists path from u to v shortest path = shortest walk longest path  longest walk

  19. Shortest path problem 4 3 2 1 2 7 0 1 A 4 6 8 2 B

  20. Shortest path problem 4 3 2 1 2 7 0 1 1 A 4 6 8 2 B

  21. Shortest path problem 2 4 3 2 1 2 7 0 1 1 A 4 6 8 2 B

  22. Shortest path problem 3 2 4 3 2 1 2 7 0 1 1 A 4 6 8 2 B

  23. Shortest path problem 3 2 4 3 2 1 2 7 0 1 1 A 4 6 4 8 2 B

  24. Shortest path problem 3 2 4 3 2 1 2 7 0 1 1 A 4 6 4 8 2 6 B

  25. Dijkstra’s algorithm for i  1 to n do T[i]  T[0] 0 S {} while S  V do j  with minimal T[j] over jSC S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j running time ?

  26. Dijkstra’s algorithm (for s.p.) T[0]  0; S  {}; for i  1 to n do T[i]  while S  V do j  with minimal T[j] over jSC S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j Prim’s algorithm (for MST) C[0] 0; S  {}; for i  1 to n do C[i]  while S V do j  with minimal C[j] over jSC S S  { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

  27. Dijkstra’s algorithm - correctness T[0]  0; S  {}; for i  1 to n do T[i]  while S  V do j  with minimal T[j] over jSC S  S  { j } for u neighbors of j do if T[j]+w[{j,u}] < T[u] then T[u]T[j]+w[{j,u}]; P[u] j Claim: After t steps we have 1. d(0,u) = T[u] for all uS 2. T[u] = dS(0,u) for all u, where dS(0,u) is the length of shortest path from 0 to u, such that all vertices (except possibly u) of the path are in S Proof: induction on t.

  28. Negative edge-weights? 4 3 -2 1 2 7 2 A 4 6 8 2 B

  29. Negative edge-weights? 4 3 -2 1 2 7 2 2 A 4 6 8 2 B

  30. Shortest path problem -4 -3 -2 -1 -2 -7 -1 A -4 -6 -8 -2 B

  31. Shortest path problem -4 4 -3 3 2 -2 1 -1 -2 2 7 -7 -1 1 A A 4 -4 -6 6 8 -8 2 -2 B B Longest path problem

  32. Shortest path problem -4 4 -3 3 2 -2 1 -1 -2 2 7 -7 1 -1 A A 4 -4 -6 6 8 -8 2 -2 B B Longest path problem shortest path  shortest walk (if negative)

  33. Negative edge weights allowed but no negative cycles shortest path = shortest walk (if negative)

  34. The Bellman-Ford algorithm D[v] = estimate on the distance from 0 Initially: D[0] = 0 D[i] =  for all other vertices Relaxation of an edge: if D[u] + w(u,v) < D[v] then D[v]  D[u] + w(u,v)

  35. The Bellman-Ford algorithm D[v] = estimate on the distance from 0 Initially: D[0] = 0 D[i] =  for all other vertices Repeat |V| times relax every edge e running time = O( V.E )

  36. The Bellman-Ford algorithm Let S[u] be the number of edges in the shortest path from 0 to u Claim: After t steps of the algorithm S[u]  t  D[u] = d(0,u)

  37. All-pairs shortest path problem Dijkstra  O( V2 log V + V.E ) (negative edges not allowed)

  38. The Floyd-Warshall algorithm dynamic programming algorithm negative edges allowed, no negative cycles dijk= length of the shortest path from i to j which only uses vertices 1...k dijk= min {dijk-1,dikk-1 + dkjk-1}

  39. The Floyd-Warshall algorithm dij0 wij for all i,j V for k from 1 to n do for i from 1 to n do for j from 1 to n do dijk= min {dijk-1,dikk-1 + dkjk-1} Time = ? Space = ?

  40. The Floyd-Warshall algorithm dij0 wij for all i,j V for k from 1 to n do for i from 1 to n do for j from 1 to n do dijk= min {dijk-1,dikk-1 + dkjk-1} Time = O(n3) Space = O(n3)

  41. The Floyd-Warshall algorithm dij wij for all i,j V for k from 1 to n do for i from 1 to n do for j from 1 to n do dij= min {dij,dik+ dkj} Time = O(n3) Space = O(n2)

  42. Single source shortest paths Dijkstra = O(E+V log V) Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles) All-pairs shortest paths Dijkstra = O(EV+V2 log V) Floyd-Warshall = O(V3) allows negative edge-weights (but not negative cycles) Johnson = O(EV+V2 log V) allows negative edge-weights (but not negative cycles)

  43. Johnson’s algorithm Dijkstra = O(E+V log V) Bellman-Ford = O(E.V) allows negative edge-weights (but not negative cycles) w’(u,v) = w(u,v) + h(u) – h(v) doesn’t change the shortest paths use Bellman-Ford to compute h such that w’ is non-negative

  44. M[i-1,j]  d-1 M[i,j]  d  M[i,j-1]  d-1 M[i-1,j-1]  d-1 M[i-1,j]  d-1 and M[i,j-1]  d-1 and M[i-1,j-1]  d-1 and A[i,j]=1  M[i,j]  d

  45. if A[i,j] = 1 then M[i,j]  1+min(M[i-1,j],M[i,j-1],M[i-1,j-1]) else M[i,j]  0

  46. If there exists a subset of size k then the k largest numbers have average at least k

  47. T(n)=T(n/2)+O(n) Find(A[l..r], n, s, B) if r  l+1 then try all possibilities else m  median(A[l..r]); p  partition(A[l..r],m) s’  sum in A[p+1..r]; n’  (r-p) if (s+s’)/(n+n’) < B then Find(A[p+1..r], n, s,B) else Find(A[l..p],n+n’,s+s’,B)

More Related