0% found this document useful (0 votes)
5 views17 pages

Lec21 Spanning

The document discusses the concept of Minimum-Cost Spanning Trees in weighted connected undirected graphs, detailing methods for edge selection such as Kruskal's and Prim's algorithms. It explains the process of forming a spanning tree by selecting edges based on their costs while avoiding cycles, and provides pseudocode and data structures for implementing Kruskal's method. The document concludes that both methods yield a minimum-cost spanning tree, with Prim's method being the fastest.

Uploaded by

Vivek Gaur
Copyright
© © All Rights Reserved
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)
5 views17 pages

Lec21 Spanning

The document discusses the concept of Minimum-Cost Spanning Trees in weighted connected undirected graphs, detailing methods for edge selection such as Kruskal's and Prim's algorithms. It explains the process of forming a spanning tree by selecting edges based on their costs while avoiding cycles, and provides pseudocode and data structures for implementing Kruskal's method. The document concludes that both methods yield a minimum-cost spanning tree, with Prim's method being the fastest.

Uploaded by

Vivek Gaur
Copyright
© © All Rights Reserved
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/ 17

Minimum-Cost Spanning Tree

• weighted connected undirected graph


• spanning tree
• cost of spanning tree is sum of edge costs
• find spanning tree that has minimum cost
Example

8 10 14
1 3 5 7
7 4 12 6 3
2
2 4 6 8
9

• Network has 10 edges.


• Spanning tree has only n - 1 = 7 edges.
• Need to either select 7 edges or discard 3.
Edge Selection Strategies
• Start with an n-vertex 0-edge forest.
Consider edges in ascending order of cost.
Select edge if it does not form a cycle
together with already selected edges.
 Kruskal’s method.
• Start with a 1-vertex tree and grow it into an
n-vertex tree by repeatedly adding a vertex
and an edge. When there is a choice, add a
least cost edge.
 Prim’s method.
Edge Rejection Strategies
• Start with the connected graph. Repeatedly
find a cycle and eliminate the highest cost
edge on this cycle. Stop when no cycles
remain.
• Consider edges in descending order of cost.
Eliminate an edge provided this leaves
behind a connected graph.
Kruskal’s Method
8 10 14
3 7 3 7
7 4 12 6 3
2
4 8 4 8
9

• Start with a forest that has no edges.

• Consider edges in ascending order of cost.


• Edge (1,2) is considered first and added to
the forest.
Kruskal’s Method
8 10 14
3 7 3 7
7 3 7 4 6 3
2 4 12 6 2
4 8 4 8
9

• Edge (7,8) is considered next and added.


• Edge (3,4) is considered next and added.
• Edge (5,6) is considered next and added.
• Edge (2,3) is considered next and added.
• Edge (1,3) is considered next and rejected
because it creates a cycle.
Kruskal’s Method
8 10 14 10 14
3 7 3 7
7 3 7 4 6 3
2 4 12 6 2
4 8 4 8
9

• Edge (2,4) is considered next and rejected because it creates a cycle.

• Edge (3,5) is considered next and added.


• Edge (3,6) is considered next and rejected.
• Edge (5,7) is considered next and added.
Kruskal’s Method
8 10 14 10 14
3 7 3 7
7 3 7 4 6 3
2 4 12 6 2
4 8 4 8
9

• n - 1 edges have been selected and no cycle formed.


• So we must have a spanning tree.
• Cost is 46.
• Min-cost spanning tree is unique when all edge
costs are different.
Prim’s Method
8 10 14 10 14
3 7 3 7
7 3 7 4 6 3
2 4 12 6 2
4 8 4 8
9

• Start with any single vertex tree.


• Get a 2-vertex tree by adding a cheapest edge.
• Get a 3-vertex tree by adding a cheapest edge.
• Grow the tree one edge at a time until the tree
has n - 1 edges (and hence has all n vertices).
Minimum-Cost Spanning Tree Methods

• Can prove that all stated edge selection/rejection


result in a minimum-cost spanning tree.
• Prim’s method is fastest.
 O(n2) using an implementation similar to that of
Dijkstra’s shortest-path algorithm.
• Kruskal’s uses union-find trees to run in O(n + e
log e) time.
Pseudocode For Kruskal’s Method

Start with an empty set T of edges.


while (E is not empty && |T| != n-1)
{
Let (u,v) be a least-cost edge in E.
E = E - {(u,v)}. // delete edge from E
if ((u,v) does not create a cycle in T)
Add edge (u,v) to T.
}
if (| T | == n-1) T is a min-cost spanning tree.
else Network has no spanning tree.
Data Structures For Kruskal’s Method

Edge set E.
Operations are:
 Is E empty?
 Select and remove a least-cost edge.
Use a min heap of edges.
 Initialize. O(e) time.
 Remove and return least-cost edge. O(log e) time.
Data Structures For Kruskal’s Method

Set of selected edges T.


Operations are:
 Does T have n - 1 edges?
 Does the addition of an edge (u, v) to T result in a
cycle?
 Add an edge to T.
Data Structures For Kruskal’s Method
Use an array for the edges of T.
 Does T have n - 1 edges?
• Check number of edges in array. O(1) time.
 Does the addition of an edge (u, v) to T result in a
cycle?
• Not easy.
 Add an edge to T.
• Add at right end of edges in array. O(1) time.
Data Structures For Kruskal’s Method
Does the addition of an edge (u, v) to T result in
a cycle?
3 7
7 4 6 3
2
4 8

• Each component of T is a tree.


• When u and v are in the same component, the
addition of the edge (u,v) creates a cycle.
• When u and v are in the different
components, the addition of the edge (u,v)
does not create a cycle.
Data Structures For Kruskal’s Method
3 7
7 4 6 3
2
4 8

• Each component of T is defined by the


vertices in the component.
• Represent each component as a set of
vertices.
 {1, 2, 3, 4}, {5, 6}, {7, 8}
• Two vertices are in the same component iff
they are in the same set of vertices.
Data Structures For Kruskal’s Method
3 7
7 4 6 3
2
4 8

• When an edge (u, v) is added to T, the two


components that have vertices u and v
combine to become a single component.
• In our set representation of components, the
set that has vertex u and the set that has
vertex v are united.
 {1, 2, 3, 4} + {5, 6} => {1, 2, 3, 4, 5, 6}

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