-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.java
More file actions
81 lines (59 loc) · 1.42 KB
/
Copy pathprims.java
File metadata and controls
81 lines (59 loc) · 1.42 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
81
import java.util.Scanner;
public class prims {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertex");
n = sc.nextInt();
int w[][] = new int[n+1][n+1];
System.out.println("Enter the weighted matrix");
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
w[i][j] = sc.nextInt();
}
}
//source vertex
int s;
System.out.println("Enter the source vertex");
s = sc.nextInt();
//arr[] = 0;
int arr[] = new int[n+1];
int min,k = 1;
int sum = 0;
int u=0,v=0,flag=0;
//make source vertex visited
arr[s]=1;
while(k<n) {
min = 99;
//source
for(int i=1; i<=n; i++) {
//other vertex
for(int j=1; j<=n; j++) {
//check for the source.v to be 1 and the comparing vertex is unvisited
if(arr[i]==1 && arr[j]==0) {
//compare the weight of the vertex with min
if(w[i][j] < min) {
min = w[i][j];
//get a edge
u=i;
v=j;
}
}
}
}
sum+=w[u][v];
arr[v]=1; //visited
k++;
System.out.println("u ="+u+" v ="+v+" dist="+min);
}
//for left out vertex
for(int i=1; i<=n; i++)
if(arr[i]==0)
flag=1;
if(flag==1)
System.out.println("prim not possible");
else
System.out.println("the minimum cost is="+sum);
}
}