Algorithm Ch 4 Lec 2
Algorithm Ch 4 Lec 2
10/1/2024
4.6: Minimum-Cost Spanning Trees
Definition: A minimum spanning tree (MST) of an edge-
weighted graph is a spanning tree whose weight (the sum of the
weights of its edges) is no larger than the weight of any other
spanning tree.
Example of MCST (Figure 4.6)
Finding a spanning tree of G with minimum cost
10/1/2024
Prim’s Algorithm
▪ A greedy method to obtain a minimum-cost spanning tree.
▪ How to determine the next edge to be added? The next edge to
include is chosen according to some optimization criterion.
▪ The simplest such criterion is to choose an edge that results in a
minimum increase in the sum of the costs of the edges so far
included.
▪ There are two possible ways to interpret this criterion.
• In the first, the set of edges so far selected form a tree. Thus,
if A is the set of edges selected so far, then A forms a tree.
• The next edge(u,v) to be included in A is a minimum-cost
edge not in A with the property that A U {(u,v)}is also a
tree.
10/1/2024
Prim’s Algorithm Implementation
Implementation of Prim’s algorithm
❑ Start with a tree that includes only a minimum-cost edge of G.
❑ The next edge (i,j) to be added is such that i is a vertex already
in the tree and j is a vertex not yet included.
❑ The cost[i,j] is minimum among all edges (k,l) such that vertex
k is in tree and vertex l is not in the tree.
❑ • Associate a value near[j] with each vertex j not yet included in the tree
• near[j]: a vertex in the tree such that cost[j,near[j]] is minimum among
all choices for near[j]
• near[j] = 0 for all j already in the tree.
• The next edge is defined by the vertex j such that near[j] ≠ 0 (j not
already in the tree) and cost[j, near[j]] is minimum
10/1/2024
Example 4.9(Prim’s Algorithm)
The tree starts with the edge(1,6) with minimum cost among all the
edges
Next edge to be included in the tree will be selected in the following
ways:
near(1)=0 // already in the tree
near(2)=1, cost(2, near(2)) = 28
near(3)=1 (or 6), cost(3, near(3))= ∞ // no edge to the tree
near(4)=1, (or 6) cost(4, near(4)) = ∞ // no edge to the tree
near(5)=6, cost(5, near(5)) = 25
near(6)=0 // already in the tree
near(7)=1, (or 6), cost(7, near(7))= ∞ // no edge to the tree
So, the next vertex is 5 and the edge to be includes is edge(6,5).
10/1/2024
Example: 4.9 (Prim’s Algorithm)
10/1/2024
Krushkal’s Algorithm
10/1/2024
Early form of the Algorithm
Algorithm: 4.10
10/1/2024
Krushkal’s Algorithm
10/1/2024