Created
March 31, 2018 03:37
-
-
Save RehmanaliMomin/086c3e9204a16d31028a380e6811a4ca to your computer and use it in GitHub Desktop.
Prim's minimum spanning tree algorithm using Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package GraphsNotes; | |
import java.util.Comparator; | |
import java.util.LinkedList; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
public class Prims { | |
public static void main(String[] args) { | |
Graph graph = new Graph(9,9); | |
graph.addEdge(0,1,2); | |
graph.addEdge(0,2,3); | |
graph.addEdge(1,2,4); | |
graph.addEdge(2,3,5); | |
graph.addEdge(1,4,3); | |
graph.addEdge(3,5,7); | |
graph.addEdge(4,5,8); | |
graph.addEdge(2,5,6); | |
graph.addEdge(0,3,3); | |
graph.addEdge(5,6,9); | |
graph.addEdge(4,2,1); | |
graph.Prims(graph,0); | |
} | |
static class Graph { | |
int V; | |
int E; | |
LinkedList<AdjNode> adj[]; | |
Graph(int V, int E) { | |
this.V = V; | |
this.E = E; | |
adj = new LinkedList[V]; | |
for (int i = 0; i < V; i++) { | |
adj[i] = new LinkedList<>(); | |
} | |
} | |
void addEdge(int a, int b, int w) { | |
adj[a].addLast(new AdjNode(b, w)); | |
adj[b].addLast(new AdjNode(a,w)); | |
} | |
// main function | |
void Prims(Graph g, int s){ | |
int count=0; | |
boolean[] visited = new boolean[g.V]; | |
visited[s]=true; | |
// Priority queue because we are going to take the edge which has the least weight first | |
Queue<Node> q = new PriorityQueue(weightComparator); | |
q.add(new Node(s,0)); | |
visited[s]=true; | |
while (!q.isEmpty()){ | |
Node n = q.poll(); | |
int temp=n.data; | |
count+=n.dist; | |
visited[temp]=true; | |
for(AdjNode x : adj[temp]){ | |
int num = x.data; | |
int w = x.weight; | |
// this is to check whether that node is already there in the graph, | |
// if already there then update its value to this new value if more | |
if(visited[num]) { | |
for (Node a:q ) { | |
if(a.data==num && a.dist>w){ | |
a.dist=w; | |
} | |
} | |
} | |
// if not there then obviously we are going to add it in the queue with that weight of the edge. | |
else{ | |
q.add(new Node(num,w)); | |
visited[num] = true; | |
} | |
System.out.println(w); | |
} | |
} | |
System.out.println(count); | |
} | |
public static Comparator<Node> weightComparator = new Comparator<Node>() { | |
@Override | |
public int compare(Node o1, Node o2) { | |
return o1.dist-o2.dist; | |
} | |
}; | |
} | |
// Type of object as saved in the elements of linkedlist in the graph | |
static class AdjNode{ | |
int data; | |
int weight; | |
AdjNode(int data,int weight){ | |
this.data=data; | |
this.weight=weight; | |
} | |
} | |
// Type of object as used in queue, the value and the distance showing the weight of that edge in consideration | |
static class Node{ | |
int data; | |
int dist; | |
Node(int data,int dist){ | |
this.data=data; | |
this.dist=dist; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment