Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 Lecture 24: Minimum Spanning Trees. Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205.

Similar presentations


Presentation on theme: "CSC 213 Lecture 24: Minimum Spanning Trees. Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205."— Presentation transcript:

1 CSC 213 Lecture 24: Minimum Spanning Trees

2 Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205

3 Minimum Spanning Tree (MST) Spanning subgraph Subgraph including every vertex Spanning tree Spanning subgraph that is also a tree Minimum spanning tree Spanning tree with minimum total edge weight ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3 2 5 7 4

4 Cycle Property Cycle Property: MSTs do not contain cycles So weight of each edge in MST is lower than weight of edge which completes cycle Proof: e is edge in G & not in MST C is cycle formed when adding e to MST For every edge f in C, weight(f)  weight(e) If weight(f)  weight(e), then exists a smaller spanning tree with e instead of f 8 4 2 3 6 7 7 9 8 e C f 8 4 2 3 6 7 7 9 8 C e f Replacing f with e yields a better spanning tree

5 UV Partition Property Partition Property: Given partitions of vertices, minimum weight edge is in a MST connecting partitions Proof: e is edge with smallest weight connecting partitions, but not in MST f connects partitions in MST If MST does not contain e, consider cycle formed by including e with MST By cycle property, weight(f) ≤ weight(e) But since e has smallest weight, weight(f) = weight(e) Create another MST by replacing f with e 7 4 2 8 5 7 3 9 8 e f 7 4 2 8 5 7 3 9 8 e f Replacing f with e yields another MST UV

6 Kruskal’s Algorithm Add edges to MST greedily Priority queue contains edges outside the cloud  Element: edge  Key: edge’s weight Only want edges that will not make a cycle  Unfortunately, detecting cycles can be hard…

7 Data Structs for Kruskal Algorithm Algorithm maintains forest of trees Edge accepted when connecting trees Use partition (i.e., collection of disjoint sets): find( u ): return set containing u union( u, v ): replace sets with u & v with their union

8 Partition Representation Each set is Sequence Each element refers to its set makeSet( u ) takes O (1) time find( u ) takes O (1) time union( u, v ) moves elements of smaller set to Sequence of larger set and updates references By having union move smaller set into large set, elements are processed only when set size doubles n calls to union() takes O (log n ) time

9 Kruskal’s Algorithm Algorithm KruskalMST(Graph G) Q  new PriorityQueue() T  new Graph() P  new Partition() for each vertex v in G P.makeSet(v) T.insertVertex(v) for each edge e in G Q.insert(e.getWeight(), e); while T.edges().getSize() < n-1 Edge e = Q.removeMin() Assign u, v to G.endpoints(e) if P.find(u)  P.find(v) then T.insertEdge(e) P.union(P.find(u),P.find(v)) return T

10 Kruskal Example JFK BOS MIA ORD LAX DFW SFO BWI PVD 867 2704 187 1258 849 144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337

11 Example

12

13

14

15

16

17

18

19

20

21

22

23 144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337

24 Prim-Jarnik’s Algorithm Similar to Dijkstra’s algorithm Pick arbitrary vertex s and grow MST as cloud of vertices Label each vertex v with d ( v ) --- smallest weight of edge connecting v to vertex in cloud At each step: Add vertex u with smallest distance to cloud Update labels of vertices adjacent to u

25 Prim-Jarnik’s Algorithm (cont.) Priority queue stores vertices outside of cloud Key: distance Element: vertex Locator-based methods insert( k, e ) – returns locator replaceKey( l, k ) – changes item’s key Store three labels with each vertex Distance from cloud Edge connecting vertex to cloud (MST edge) Locator in priority queue

26 Prim-Jarnik’s Algorithm (cont.) Algorithm PrimJarnikMST(Graph G) Q  new PriorityQueue() s  G.vertices().elemAtRank(0) for each vertex v in G if v = s v.setDistance(0) else v.setDistance(  ) v.setParent(null) l  Q.insert(getDistance(v), v) setLocator(v,l) while !Q.isEmpty() u  Q.removeMin() for each edge e in G.incidentEdges(u) z  G.opposite(u,e) r  e.getWeight() if r < z.getDistance() z.setDistance(r) z.setParent(e) Q.replaceKey(z.getLocator(),r)

27 Example B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 8   B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5  7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5  7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5 4 7

28 Example (contd.) B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7 B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7

29 Prim-Jarnik’s Analysis Operations incidentEdges called once per vertex Set/Get each vertex’s labels O(deg(v)) times Priority queue operations Each vertex inserted once and removed once  Each insertion/removal takes O(log n) Vertex w ’s key modified O ( deg(w)) times  Each key change takes O(log n) time Algorithm takes O((n  m) log n) time using adjacency list structure


Download ppt "CSC 213 Lecture 24: Minimum Spanning Trees. Announcements Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205."

Similar presentations


Ads by Google