minimum spanning trees
play

Minimum Spanning Trees Carola Wenk Slides courtesy of Charles - PowerPoint PPT Presentation

CMPS 6610/4610 Fall 2016 Minimum Spanning Trees Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 6610/4610 Fall 2016 1 Minimum spanning trees Input: A connected, undirected graph G = ( V


  1. CMPS 6610/4610 – Fall 2016 Minimum Spanning Trees Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 6610/4610 – Fall 2016 1

  2. Minimum spanning trees Input: A connected, undirected graph G = ( V , E ) with weight function w : E  R . • For simplicity, assume that all edge weights are distinct. Output: A spanning tree T — a tree that connects all vertices — of minimum weight:   w ( T ) w ( u , v ) .  ( u , v ) T CMPS 6610/4610 – Fall 2016 2

  3. Example of MST 6 12 9 5 7 14 15 8 10 3 CMPS 6610/4610 – Fall 2016 3

  4. Growing an MST Grow an MST by greedily adding one edge at a time. G ENERIC - M ST (G,w){ T   while T does not form a spanning tree { // Maintain invariant that T is a subset of an MST for G Find a “safe” edge {u,v} such that T  {{u,v}} is a subset of an MST for G T  T  {{u,v}} } return A } CMPS 6610/4610 – Fall 2016 4

  5. Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem [Cut property]. Let G = ( V , E ) and let A  V . Suppose that { u , v}  E is the least-weight edge connecting A to V \ A . Then, { u , v } is contained in an MST T of G . CMPS 6610/4610 – Fall 2016 5

  6. Proof of theorem Proof. Suppose { u , v }  T . Cut and paste. T : v u  A { u , v } = least-weight  V \ A edge connecting A to V \ A CMPS 6610/4610 – Fall 2016 6

  7. Proof of theorem Proof. Suppose { u , v }  T . Cut and paste. T : v u  A { u , v } = least-weight  V \ A edge connecting A to V \ A Consider the unique simple path from u to v in T . CMPS 6610/4610 – Fall 2016 7

  8. Proof of theorem Proof. Suppose { u , v}  T . Cut and paste. T : v u  A { u , v} = least-weight edge  V \ A connecting A to V \ A Consider the unique simple path from u to v in T . Swap { u , v} with the first edge on this path that connects a vertex in A to a vertex in V \ A . CMPS 6610/4610 – Fall 2016 8

  9. Proof of theorem Proof. Suppose { u , v}  T . Cut and paste. T  : v u  A { u , v} = least-weight edge  V \ A connecting A to V \ A Consider the unique simple path from u to v in T . Swap { u , v} with the first edge on this path that connects a vertex in A to a vertex in V \ A . A lighter-weight spanning tree than T results. CMPS 6610/4610 – Fall 2016 9

  10. MST algorithms • Prim’s algorithm: • Maintains one tree • Runs in time O (| E| log | V| ) with binary heaps, in time O (| E| + | V | log | V| ), with Fibonacci heaps • Kruskal’s algorithm: • Maintains a forest and uses the disjoint-set data structure • Runs in time O (| E| log | E| ) CMPS 6610/4610 – Fall 2016 10

  11. Prim’s algorithm I DEA : Maintain V \ A as a priority queue Q . Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A . Q  V Dijkstra: while Q   do key [ v ]   for all v  V u  E XTRACT -M IN ( Q ) key [ s ]  0 for some arbitrary s  V S  S  { u } while Q   for each v  Adj [ u ] do do u  E XTRACT -M IN ( Q ) if d [ v ] > d [ u ] + w ( u , v ) then for each v  Adj [ u ] d [ v ]  d [ u ] + w ( u , v ) do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) D ECREASE -K EY  [ v ]  u At the end, {( v ,  [ v ])} forms the MST edges. CMPS 6610/4610 – Fall 2016 11

  12. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 12

  13. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 13

  14. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 14

  15. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 15

  16. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 16

  17. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 17

  18. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 18

  19. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 19

  20. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 20

  21. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 21

  22. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 22

  23. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 23

  24. Example of Prim’s algorithm   A 6 12  V \ A 9 5    7 14 15 8   0 10 3  u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] do if v  Q and w ( u , v ) < key [ v ] then key [ v ]  w ( u , v ) ⊳ D ECREASE -K EY  [ v ]  u CMPS 6610/4610 – Fall 2016 24

  25. Analysis of Prim Q  V  (| V| ) key [ v ]   for all v  V total key [ s ]  0 for some arbitrary s  V while Q   do u  E XTRACT -M IN ( Q ) for each v  Adj [ u ] | V | do if v  Q and w ( u , v ) < key [ v ] degree ( u ) times times then key [ v ]  w ( u , v )  [ v ]  u Handshaking Lemma   (| E| ) implicit D ECREASE -K EY ’s. Time =  (| V| )· T E XTRACT- M IN +  (| E| )· T D ECREASE- K EY CMPS 6610/4610 – Fall 2016 25

  26. Analysis of Prim (continued) Time =  (| V| )· T E XTRACT- M IN +  (| E| )· T D ECREASE- K EY Q T E XTRACT- M IN T D ECREASE- K EY Total O (| V| 2 ) array O (| V| ) O (1) binary O (log | V| ) O (log | V| ) O (| E| log | V| ) heap Fibonacci O (log | V| ) O (1) O (| E| + | V| log | V| ) heap amortized amortized worst case CMPS 6610/4610 – Fall 2016 26

  27. Kruskal’s algorithm I DEA (again greedy) : Repeatedly pick edge with smallest weight as long as it does not form a cycle. • The algorithm creates a set of trees (a forest ) • During the algorithm the added edges merge the trees together, such that in the end only one tree remains CMPS 6610/4610 – Fall 2016 27

  28. Example of Kruskal’s algorithm S={ {a},{b},{c},{d},{e} MST edges a {f},{g},{h} } 6 12 a set repr. 9 5 b c d 7 14 15 8 e f g 10 3 h Every node is a single tree. CMPS 6610/4610 – Fall 2016 28

  29. Example of Kruskal’s algorithm S={ {a},{b},{c},{d},{f} MST edges a {g}, {e, h} } 6 12 a set repr. 9 5 b c d 7 14 15 8 e f g 10 3 h Edge 3 merged two singleton trees. CMPS 6610/4610 – Fall 2016 29

  30. Example of Kruskal’s algorithm S={ {a},{d},{f}, {g} MST edges a {e, h}, {b, c} } 6 12 a set repr. 9 5 b c d 7 14 15 8 e f g 10 3 h CMPS 6610/4610 – Fall 2016 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend