1 / 11

Kruskal’s Algorithm for Computing MSTs

Kruskal’s Algorithm for Computing MSTs. Section 9.2. The Minimum Spanning Tree. Give a graph G = (V, E), the minimum spanning tree (MST) is a weighted graph G’ = (V, E’) such that: E’  E G’ is connected G’ has the minimum cost. Example. 12. E. A. 12. 20. E. A. B. 6. D. 20. C.

Download Presentation

Kruskal’s Algorithm for Computing MSTs

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. Kruskal’s Algorithm for Computing MSTs • Section 9.2

  2. The Minimum Spanning Tree • Give a graph G = (V, E), the minimum spanning tree (MST) is a weighted graph G’ = (V, E’) such that: • E’  E • G’ is connected • G’ has the minimum cost

  3. Example 12 E A 12 20 E A B 6 D 20 C 13 B 6 D C 13 12 Which is the MST? E A 20 Why MST is a tree? 20 B 6 D C 13

  4. Naïve Algorithm for MST(Using Exhaustive Search) MST  Empty-Graph cost   For each subgraph G’ = (V, E’) of G = (V, E) do { 1. Check that G’ is connected 2. Compute cost c of G’ 3. If c < cost then { 3.1 MST  G’ 3.2 cost  c }} What is the complexity “in words” of the algorithm? “the number of subgraphs”  “cost of computing Steps 1-3” How many subgraphs are there? 2|E| This is bad!

  5. The Prim Algorithm // input: a graph G // output: E: a MST for G • Select a starting node, v • T  {v} //the nodes in the MST • E  {} //the edges in the MST • 4. While not all nodes in G are in the T do • 3.1 Choose the edge v’ in G − T such that there is a v in T: • weight(v,v’) is the minimum in • {weight(u,w) : w in G − T and u in T} • 3.2 T  T  {v’} • 3.3 E  E  {(v,v’)} • 5. return E Complexity: - O((|E|+|V|)log2|E|) - In class we show O(|E||V|)

  6. T G-T Why does it works? Property: Suppose that we divide the nodes of a graph G = (V, E) in two groups V, V’: 12 22 7 20 2 5 6 66 10 13 If there is an edge e between the 2 groups with minimum cost, then there is an MST containing e

  7. Visited Non visited Example of a “Greedy” Algorithm Dijkstra-Primis an example of a greedy algorithm since it looks at a subset of the larger problem 12 22 7 20 2 5 6 66 10 13 Unfortunately, not all problems can be solved with greedy algorithms Example of a non greedy algorithm? Our naïve algorithm for MST

  8. Kruskal Algorithm 12 E A 20 12 3 2 B 6 D C 13 6 F • |V| = 6 • |E| = 8 • A tree with |V| nodes has edges |V|-1

  9. The Kruskal Algorithm // input: a graph G with n nodes and m edges // output: E: a MST for G 1. 2. E  {} //the edges in the MST 3. i  1 //counter for EG 4. While do if addingEG[i] to E does not add a cycle then E  E  {EG[i]} i  i + 1 5. return E EG[1..m]  Sort the m edges in G in increasing weight order |E| < n - 1 Is this algorithm Greedy? Yes Complexity: O(|E|log2 |E|)

  10. Why Does the Kruskal Algorithm Works? Property: If a connected graph G has n nodes, then any acyclic subgraph G’ of G with n-1 edges is also connected and includes all n nodes (G’ is a tree) Corollary: Kruskal’s algorithm returns an MST of G

  11. Homework • 010: • 9.1: 3, 6.b, 9 (just explain) • 9.2: 1.a, 2.a, 5 • 011: • 9.1: 2, 6.a, 9.a • 9.2: 1.b, 2.d, 4

More Related