Breaking
Greedy

# Minimum Spanning Tree (MST) Prime Kruskal algorithm

Prime Algorithm helps to find minimum-spanning-tree. Prims algorithm is useful when a graph with lots of edges. It is significantly faster in the limit when dense graph with many more edges than vertices. Kruskal performs better in sparse graphs and it uses simpler data structures.

Write program to implement prim’s algorithm

## source Code

```package com.dsacode.Algorithm.greedy;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

int dest;
int weight;

this.dest = dest;
this.weight = weight;
this.next = null;
}

public String toString() {
return "" + dest + " - " + weight;
}
}

public class Prime {

int numVertices;
public static final int INF = Integer.MAX_VALUE;
TreeMap < Integer, Integer > heap = new TreeMap < Integer, Integer >();

public Prime(int numVertices) {
this.numVertices = numVertices;
}

private void addEdge(Prime graph, int src, int dest, int weight) {

if (list == null)

list = null;
if (list == null)

}

public void PrimeMST(Prime graph) {
Map < Integer, Integer > MSTholder = new TreeMap < Integer, Integer >();
Set < Integer > set = adjList.keySet();
for (Integer i : set) {
createHeap(i, INF);
}

while (heap.size() != 0) {
int minEdgeVertex = findMin();
heap.remove(minEdgeVertex);

Iterator < AdjListNode > it = list.iterator();

while (it.hasNext()) {
int vertex = node.dest;

if ((heap.containsKey(vertex)) && (node.weight < INF)) {
heap.put(vertex, node.weight);
MSTholder.put(vertex, minEdgeVertex);
}
}
}

printMST(MSTholder);
long cost = MSTCost(MSTholder);
System.out.println("Minimum spanning tree Cost is :" + cost);

}

private long MSTCost(Map < Integer, Integer > MSTholder) {
Set< Map.Entry < Integer, Integer > > set = MSTholder.entrySet();

long sum = 0;
for (Map.Entry < Integer, Integer> entry : set) {

int key = entry.getKey();
int value = entry.getValue();

if (list != null) {
for (int j = 0; j < list.size(); j++) {
if ((node.dest) == key) {
sum += node.weight;
}
}
}
}
return sum;
}

private void printMST(Map < Integer, Integer > MSTholder) {

System.out.println("Minimum Spanning Tree is : ");
Set < Map.Entry < Integer, Integer > > set = MSTholder.entrySet();

for (Map.Entry < Integer, Integer > entry : set) {
System.out.println(entry.getKey() + " -- " + entry.getValue());
}
}

public void createHeap(int vertex, Integer weight) {
if (heap == null)
heap = new TreeMap < Integer, Integer >();

heap.put(vertex, weight);
}

private int findMin() {
Set < Map.Entry < Integer, Integer > > list = heap.entrySet();

int minKey = heap.firstKey();
int minValue = INF;
if (list != null) {
for (Map.Entry < Integer, Integer > entry : list) {
if (minValue > entry.getValue()) {
minValue = entry.getValue();
minKey = entry.getKey();
}
}
}
return minKey;
}

public static void main(String[] args) throws Exception {
int numvertices = 9;

Prime graph = new Prime(numvertices);

graph.PrimeMST(graph);

}

}```

## Output

```Minimum Spanning Tree is:
1 -- 0
2 -- 1
3 -- 4
4 -- 5
5 -- 2
6 -- 5
7 -- 6
8 -- 2
Minimum spanning tree Cost is: 40```