forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPrim.java
60 lines (58 loc) · 1.1 KB
/
Prim.java
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
package see_Practice;
import java.util.Scanner;
import java.util.Arrays;
public class Prim {
static int n;
static int a[][];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
System.out.println("enter the number of vertices:");
n=in.nextInt();
a=new int[n][n];
System.out.println("enter the cost matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
prims();
in.close();
}
public static void prims()
{
int no_edge=0,sum=0;
boolean[] selected=new boolean[n];
Arrays.fill(selected, false);
selected[0]=true;
while(no_edge<=n-1)
{
int x=0,y=0,min=999;
for(int i=0;i<n;i++)
{
if(selected[i]==true)
{
for(int j=0;j<n;j++)
{
if(!selected[j]&& a[i][j]!=0)
{
if(a[i][j]<min)
{
min=a[i][j];
x=i;
y=j;
}
}
}
}
}
System.out.println(x+":"+y+"->"+a[x][y]);
sum=sum+a[x][y];
selected[y]=true;
no_edge++;
}
System.out.println("minimum spanning cost is"+sum);
}
}