0% found this document useful (0 votes)
26 views

Algorithms Lab Ex 6

Uploaded by

pixaho2996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Algorithms Lab Ex 6

Uploaded by

pixaho2996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of

Computer Science and Engineering

Title: Implement Prim’s Algorithm

Algorithms Lab
CSE 206

Green University of Bangladesh


1 Objective(s)
• To learn Prim’s algorithm to find MST of a graph.

2 Problem Analysis
2.1 Prim’s Algorithm
Prim’s algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the
edges of that graph which
• form a tree that includes every vertex.
• has the minimum sum of weights among all the trees that can be formed from the graph.

2.2 How Prim’s algorithm works


It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding
a global optimum. We start from one vertex and keep adding edges with the lowest weight until we reach our
goal. The steps for implementing Prim’s algorithm are as follows:
• Initialize the minimum spanning tree with a vertex chosen at random.
• Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree.

• Keep repeating step 2 until we get a minimum spanning tree.

2.3 Example of Prim’s algorithm

Figure 1: Start with a weighted graph

(a) Choose the edge with the least weight, if there are more
than 1, choose anyone (b) Choose the next shortest edge and add it

Figure 2: Step 2 and 3


(a) Choose the next shortest edge that doesn’t create a cycle (b) Choose the next shortest edge that doesn’t create a cycle
and add it and add it

Figure 3: Step 4 and 5

Figure 4: Repeat until you have a spanning tree

3 Algorithm
Algorithm 1: Prim’s Algorithm

1 T = ∅;
2 U= 1;
3 while (U 6= V) do
4 let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;
5 T = T ∪ (u, v)
6 U=U∪v
7 end

4 Implementation in Java
1 // A Java program for Prim’s Minimum Spanning Tree (MST) algorithm.
2 // The program is for adjacency matrix representation of the graph
3
4 import java.util.*;
5 import java.lang.*;
6 import java.io.*;
7
8 class MST {
9 // Number of vertices in the graph
10 private static final int V = 5;
11
12 // A utility function to find the vertex with minimum key
13 // value, from the set of vertices not yet included in MST
14 int minKey(int key[], Boolean mstSet[])
15 {
16 // Initialize min value
17 int min = Integer.MAX_VALUE, min_index = −1;
18
19 for (int v = 0; v < V; v++)
20 if (mstSet[v] == false && key[v] < min) {
21 min = key[v];
22 min_index = v;
23 }
24
25 return min_index;
26 }
27
28 // A utility function to print the constructed MST stored in
29 // parent[]
30 void printMST(int parent[], int graph[][])
31 {
32 System.out.println("Edge \tWeight");
33 for (int i = 1; i < V; i++)
34 System.out.println(parent[i] + " − " + i + "\t" + graph[i][parent[i
]]);
35 }
36
37 // Function to construct and print MST for a graph represented
38 // using adjacency matrix representation
39 void primMST(int graph[][])
40 {
41 // Array to store constructed MST
42 int parent[] = new int[V];
43
44 // Key values used to pick minimum weight edge in cut
45 int key[] = new int[V];
46
47 // To represent set of vertices included in MST
48 Boolean mstSet[] = new Boolean[V];
49
50 // Initialize all keys as INFINITE
51 for (int i = 0; i < V; i++) {
52 key[i] = Integer.MAX_VALUE;
53 mstSet[i] = false;
54 }
55
56 // Always include first 1st vertex in MST.
57 key[0] = 0; // Make key 0 so that this vertex is
58 // picked as first vertex
59 parent[0] = −1; // First node is always root of MST
60
61 // The MST will have V vertices
62 for (int count = 0; count < V − 1; count++) {
63 // Pick thd minimum key vertex from the set of vertices
64 // not yet included in MST
65 int u = minKey(key, mstSet);
66
67 // Add the picked vertex to the MST Set
68 mstSet[u] = true;
69
70 // Update key value and parent index of the adjacent
71 // vertices of the picked vertex. Consider only those
72 // vertices which are not yet included in MST
73 for (int v = 0; v < V; v++)
74
75 // graph[u][v] is non zero only for adjacent vertices of m
76 // mstSet[v] is false for vertices not yet included in MST
77 // Update the key only if graph[u][v] is smaller than key[v]
78 if (graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < key[
v]) {
79 parent[v] = u;
80 key[v] = graph[u][v];
81 }
82 }
83
84 // print the constructed MST
85 printMST(parent, graph);
86 }
87
88 public static void main(String[] args)
89 {
90 /* Let us create the following graph
91 2 3
92 (0)−−(1)−−(2)
93 | / \ |
94 6| 8/ \5 |7
95 | / \ |
96 (3)−−−−−−−(4)
97 9 */
98 MST t = new MST();
99 int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },
100 { 2, 0, 3, 8, 5 },
101 { 0, 3, 0, 0, 7 },
102 { 6, 8, 0, 0, 9 },
103 { 0, 5, 7, 9, 0 } };
104
105 // Print the solution
106 t.primMST(graph);
107 }
108 }

5 Sample Input/Output (Compilation, Debugging & Testing)


Edge Weight

0 - 1 => 2
1 - 2 => 3
0 - 3 => 6
1 - 4 => 5

6 Discussion & Conclusion


Based on the focused objective(s) to understand about the MST algorithms, the additional lab exercise made
me more confident towards the fulfilment of the objectives(s).
7 Lab Task (Please implement yourself and show the output to the
instructor)
1. Write a Program in java to find the Second Best Minimum Spanning Tree using Prim’s Algorithm.

7.1 Problem analysis


A Minimum Spanning Tree T is a tree for the given graph G which spans over all vertices of the given graph
and has the minimum weight sum of all the edges, from all the possible spanning trees. A second best MST T0
is a spanning tree, that has the second minimum weight sum of all the edges, from all the possible spanning
trees of the graph G.

8 Lab Exercise (Submit as a report)


• Find the number of distinct minimum spanning trees for a given weighted graph.

9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.

You might also like