-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskalGraph.java
More file actions
93 lines (77 loc) · 2.28 KB
/
KruskalGraph.java
File metadata and controls
93 lines (77 loc) · 2.28 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
82
83
84
85
86
87
88
89
90
91
92
93
// kruskal
import java.util.*;
public class KruskalGraph {
static long startTime = System.currentTimeMillis(); // calculate running time of the program
public static void main(String[] args){
// Scanner scan = new Scanner(System.in);
// int[][] matrix = new int[5][5];
int[] ancestor = new int[5];
int minimum;
int u=0;
int v=0;
int Edge_Count = 1;
int total = 0;
int matrix[][] = new int[][] {{0, 1, 0, 0, 0}, //starting from the first node
{1, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
};
int w= 0;
for(int i=1;i<4;i++) //generating the random edge weights
{
Random rand = new Random();
int a11 = rand.nextInt(i)+1;
w = rand.nextInt(5)+1;
matrix[i+1][a11] = w;
matrix[a11][i+1] = w;
}
System.out.println( " ");
for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++)
{
if(matrix[i][j] == 0 && i!=j)
{
Random rand = new Random();
matrix[i][j] = matrix[j][i]=rand.nextInt(100)+20;
}
}}
for (int i = 0; i < 5; i++) { //print the adjacency matrix
for (int j = 0; j < 5; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
while (Edge_Count <5 ){
minimum = 999; //intially allocate infinite values
for (int i =0;i<5;i++){
for (int j=0;j<5;j++){
if (matrix[i][j] < minimum){
minimum = matrix[i][j];
u = i;
v = j;
}
}
}
while(ancestor[u]!=0){
u=ancestor[u];
}
while(ancestor[v]!=0){
v=ancestor[v];
}
if (v!=u){ // find edges while parsing the vertices
Edge_Count++;
System.out.println("Edge between vertices" + u + "-->" + v + " Min " +minimum );
total+=minimum;
ancestor[v]=u;
}
matrix[u][v] = matrix[v][u] = 999;
}
System.out.println("------------------------");
System.out.println("The Weight of the created Minimum Spanning tree is "+total); //Print Weight for minimum spanning tree
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("------------------------");
System.out.println("Run time of the program is " + totalTime + " ms"); //report the running time of program
}
}