-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskalDemo.java
More file actions
80 lines (79 loc) · 1.99 KB
/
KruskalDemo.java
File metadata and controls
80 lines (79 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DAA;
/**
*
* @author dell
*/
import java.util.*;
public class KruskalDemo {
static int parent[];
static int cost[][];
public static void main(String[]args)
{
int i,j,n,min,ne=1;
int u=0,v=0,a=0,b=0,mincost=0;
Scanner in=new Scanner(System.in);
System.out.println("enter the number of nodes in the graph");
n=in.nextInt();
cost=new int[n+1][n+1];
parent=new int[n+1];
System.out.println("enter the cost matrix");
for(i=1;i<=n;i++)
{
parent[i]=0;
for(j=1;j<=n;j++)
{
cost[i][j]=in.nextInt();
if(cost[i][j]==0)
cost[i][j]=999;
}
}
System.out.println("the edges of minimum spanning tree are:");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=findparent(u);
v=findparent(v);
if(union(u,v)==1)
{
System.out.println(ne+++"edge selected ("+a+"---"+b+") cost "+min);
mincost+=min;
}
cost[a][b]=cost[b][a]=999;
}
System.out.println("minimum cost="+mincost);
}
public static int findparent(int i)
{
while(parent[i]!=0)
{
i=parent[i];
}
return i;
}
public static int union(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
}