Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Prims.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.*;
public class Prims {
public static void main(String[] args) {
int cost[][] = new int[10][10];
int i, j, mincost = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices: ");
int n = sc.nextInt();
System.out.println("Enter the cost matrix: ");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cost[i][j] = sc.nextInt();
}
}
System.out.println("Min Tree edges are");
mincost = pr(cost, n, mincost);
System.out.println("Minimum cost: " + mincost);
}
static int pr(int[][] cost, int n, int mincost) {
int nearV[] = new int[10], t[][] = new int[10][10], u =0, i, j, k;
for (i = 2; i <= n; i++)
nearV[i] = 1;
nearV[1] = 0;
for (i = 1; i < n; i++) {
int min = 999;
for (j = 1; j <= n; j++) {
if (nearV[j] != 0 && cost[j][nearV[j]] < min) {
min = cost[j][nearV[j]];
u = j;
}
}
t[i][1] = u;
t[i][2] = nearV[u];
mincost += min;
nearV[u] = 0;
for (k = 1; k <= n; k++) {
if (nearV[k] != 0 && cost[u][k] < cost[k]
[nearV[k]])
nearV[k] = u;
}
System.out.println(i + ": Minimum edge is <" + t[i]
[2] + ", " + t[i][1] + ">\tCost: " + min);
}
return mincost;
}
}