0% found this document useful (0 votes)
180 views23 pages

Minimum Spanning Trees: (Some Material Adapted From Slides by Peter Lee)

The document discusses minimum spanning trees (MST) and two algorithms for finding them - Prim's and Kruskal's algorithms. It begins by introducing the telephone wiring problem that MSTs solve by connecting all customers with minimum total wire length. It then defines MSTs as acyclic subgraphs that connect all vertices of a graph with minimum total edge cost. The document explains applications of MSTs and describes Prim's and Kruskal's algorithms step-by-step with examples, noting they are both greedy approaches.

Uploaded by

mdhuq1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views23 pages

Minimum Spanning Trees: (Some Material Adapted From Slides by Peter Lee)

The document discusses minimum spanning trees (MST) and two algorithms for finding them - Prim's and Kruskal's algorithms. It begins by introducing the telephone wiring problem that MSTs solve by connecting all customers with minimum total wire length. It then defines MSTs as acyclic subgraphs that connect all vertices of a graph with minimum total edge cost. The document explains applications of MSTs and describes Prim's and Kruskal's algorithms step-by-step with examples, noting they are both greedy approaches.

Uploaded by

mdhuq1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Minimum Spanning Trees

(some material adapted from slides by Peter Lee)

Problem: Laying Telephone Wire

Central office

Wiring: Nave Approach

Central office

Expensive!
3

Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers


4

Minimum Spanning Tree (MST)


(see Weiss, Section 24.2.2)

A minimum spanning tree is a subgraph of an undirected weighted graph G, such that

it is a tree (i.e., it is acyclic) it covers all the vertices V


contains |V| - 1 edges

the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique
5

Applications of MST
Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning) Internet content distribution
$$$, also a hot research topic
Idea: publisher produces web pages, content distribution network replicates web pages to many locations so consumers can access at higher speed MST may not be good enough!
content distribution on minimum cost tree may take a long time!
6

How Can We Generate a MST?

b
6 4 5

b
6 4 5

a
5

2 4

d
5

a
5

2 4

d
5

Prims Algorithm
Let V ={1,2,..,n} and U be the set of vertices that makes the MST and T be the MST Initially : U = {1} and T =
while (U V) let (u,v) be the lowest cost edge such that u U and v V-U

T = T {(u,v)}
U = U {v}
8

Prims Algorithm implementation


Initialization a. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = null c. For all vertices v V, v r, set D(v) = d. Insert all vertices into priority queue P, using distances as the keys
9

b
6 4 5

a
5

2 4

d
5

e a b c d 0 e

Vertex Parent e -

Prims Algorithm
While P is not empty:

1. Select the next vertex u to add to the tree u = P.deleteMin()


2. Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w P) If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w

10

Prims algorithm

b
6 4 5

a
5

2 4

d
5

d b c a 4 5 5 e

Vertex Parent e b e c e d e

The MST initially consists of the vertex e, and we update the distances and parent for its adjacent vertices

11

Prims algorithm

b
6 4 5

a
5

2 4

d
5

a c b 2 4 5 e

Vertex Parent e b e c d d e a d

12

Prims algorithm

b
6 4 5

a
5

2 4

d
5

c b 4 5 e

Vertex Parent e b e c d d e a d

13

Prims algorithm

b
6 4 5

a
5

2 4

d
5

b 5 e

Vertex Parent e b e c d d e a d

14

Prims algorithm

b
6 4 5

a
5

2 4

d
5

Vertex Parent e b e c d d e a d

The final minimum spanning tree

15

Prims Algorithm Invariant


At each step, we add the edge (u,v) s.t. the weight of (u,v) is minimum among all edges where u is in the tree and v is not in the tree Each step maintains a minimum spanning tree of the vertices that have been included thus far When all vertices have been included, we have a MST for the graph!

16

Running time of Prims algorithm


Initialization of priority queue (array): O(|V|)

Update loop: |V| calls Choosing vertex with minimum cost edge: O(|V|) Updating distance values of unconnected vertices: each edge is considered only once during entire execution, for a total of O(|E|) updates O(|E| + |V| 2) Overall cost:

17

Another Approach Kruskals


Create a forest of trees from the vertices Repeatedly merge trees by adding safe edges until only one tree remains A safe edge is an edge of minimum weight which does not create a cycle
9

b
6 4 5

a
5

2 4

d
5

forest: {a}, {b}, {c}, {d}, {e}

18

Kruskals algorithm
Initialization a. Create a set for each vertex v V b. Initialize the set of safe edges A comprising the MST to the empty set c. Sort edges by increasing weight
9

b
6 4 5

a
5

2 4

d
5

{a}, {b}, {c}, {d}, {e} A= E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}

19

Kruskals algorithm
For each edge (u,v) E in increasing order while more than one set remains: If u and v, belong to different sets a. A = A {(u,v)} b. merge the sets containing u and v Return A

Use Union-Find algorithm to efficiently determine if u and v belong to different sets


20

Kruskals algorithm
9

b
6

a
5

2 4

d
5

E = {(a,d), (c,d), (d,e), (a,c),


5

(b,e), (c,e), (b,d), (a,b)}

e A {(a,d)} {(a,d), (c,d)} {(a,d), (c,d), (d,e)} {(a,d), (c,d), (d,e), (b,e)}
21

Forest {a}, {b}, {c}, {d}, {e} {a,d}, {b}, {c}, {e} {a,d,c}, {b}, {e} {a,d,c,e}, {b} {a,d,c,e,b}

Kruskals Algorithm Invariant


After each iteration, every tree in the forest is a MST of the vertices it connects Algorithm terminates when all vertices are connected into one tree

22

Greedy Approach
Like Dijkstras algorithm, both Prims and Kruskals algorithms are greedy algorithms The greedy approach works for the MST problem; however, it does not work for many other problems!

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy