You are on page 1of 4

package com.interanshala.

javaApp;
import java.util.*;

public class Graph {


static int v = 6;
public static void main(String agrs[]){
Scanner in = new Scanner(System.in);
int[][] graph = new int[][]
{ { 0, 7, 0, 8, 0, 0 },
{ 7, 0, 6, 3, 0, 0 },
{ 0, 6, 0, 4, 2, 5 },
{ 8, 3, 4, 0, 3, 0 },
{ 0, 0, 2, 3, 0, 2 },
{ 0, 0, 5, 0, 2, 0 }};

int k=0;
for(int i=0;i<v;i++){
for(int j=0;j<i;j++){
if(graph[i][j]!=0){
k++;
}
}
}
// prims(graph);

edge[] edges = new edge[k];


k=0;
for(int i=0;i<v;i++){
for(int j=0;j<i;j++){
if(graph[i][j]!=0){
edge newEdge = new edge();
newEdge.source=i;
newEdge.destination=j;
newEdge.weight=graph[i][j];
edges[k]=newEdge;
k++;
}
}
}
kruskal(edges);
}

private static void prims(int[][] graph) {

int[] parent = new int[v];


int[] distance = new int[v];
boolean[] visited = new boolean[v];

Arrays.fill(parent,-1);
Arrays.fill(distance,Integer.MAX_VALUE);
Arrays.fill(visited,false);

distance[0]=0;

for(int i=1;i<v-1;i++){
int minValue = findMinimumValue(distance,visited);

visited[minValue]=true;

for(int j=0;j<v;j++){
if(graph[minValue][j]!=0 && visited[j]==false &&
graph[minValue][j]<distance[j]){
parent[j] = minValue;
distance[j]=graph[minValue][j];
}
}
// System.out.println(parent[minValue] +" "+ minValue +" "+
distance[minValue]);
}

int minCost =0;


for(int i=1;i<v;i++){
System.out.println(parent[i] +" "+ i+ " = "+ distance[i]);
minCost+=distance[i];
}
System.out.println("Cost of Minimum Spanning Tree "+ minCost);

private static int findMinimumValue(int[] distance, boolean[] visited) {


int min = Integer.MAX_VALUE;
int index = -1;
for(int i=0;i<v;i++){
if(visited[i]==false && distance[i]<min){
min = distance[i];
index = i;
}
}
return index;
}

// Kruskal's Algorithm

public static class edge implements Comparable<edge>{


int source, destination, weight;

@Override
public int compareTo(edge newEdge) {
return this.weight-newEdge.weight;
}
}

public static class Subset{


int parent;
int rank;
}

public static int findParent(Subset[] subsets,int i){


if(subsets[i].parent != i){
subsets[i].parent = findParent(subsets,subsets[i].parent);
}
return subsets[i].parent;
}

public static void union(Subset[] subsets,int x,int y){

int xRoot = findParent(subsets,x);


int yRoot = findParent(subsets,y);

if(subsets[xRoot].rank < subsets[yRoot].rank){


subsets[xRoot].parent = yRoot;

}else if(subsets[yRoot].rank < subsets[xRoot].rank){


subsets[yRoot].parent = xRoot;
}else{
subsets[yRoot].parent = xRoot;
subsets[xRoot].rank++;
}
}

private static void kruskal(edge[] edges) {

Arrays.sort(edges);

// for(int i=0;i<edges.length;i++){
// System.out.println(edges[i].source +" "+ edges[i].destination +"
"+ edges[i].weight);
// }

Subset[] subsets = new Subset[v];


for(int i=0;i<v;++i){
subsets[i] = new Subset();
}

for(int i=0;i<v;++i){
subsets[i].parent=i;
subsets[i].rank=0;
// System.out.println(i +""+ subsets[i].parent +""+
subsets[i].rank);
}

int i = 0;
int count = 0;
edge[] result = new edge[v];

while(count<v-1){
edge currentEdge = edges[i];
int xRoot = findParent(subsets,currentEdge.source);
int yRoot = findParent(subsets,currentEdge.destination);

if(xRoot != yRoot){
result[count]= currentEdge;
union(subsets,xRoot,yRoot);
count++;
}
i++;
}

int minCost=0;
for(int n=0;n<count;n++){
System.out.println(result[n].source +" -> "+
result[n].destination +" = "+ result[n].weight);
minCost+=result[n].weight;
}
System.out.println("Cost of minimum spanning tree "+ minCost);

}
}

You might also like