-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecond Best MST.java
151 lines (130 loc) · 3.84 KB
/
Second Best MST.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Author - Shamura Ahmad
*/
package com.mycompany.secondbestmst;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.File;
import java.io.IOException;
class SecondBestMST {
class Edge implements Comparable<Edge> {
int src, dest, weight;
public int compareTo(Edge compareEdge) {
return this.weight - compareEdge.weight;
}
}
int V, E;
Edge edge[];
Edge result[];
Edge result1[];
Edge result2[];
int leader[];
SecondBestMST(int v, int e) {
V = v;
E = e;
leader = new int[V + 1];
edge = new Edge[E];
for (int i = 0; i < e; ++i) {
edge[i] = new Edge();
}
}
void MakeSet() {
for (int i = 0; i <= V; i++) {
leader[i] = i;
}
}
int FindSet(int n) {
return leader[n];
}
void Union(int u, int v) {
int newLeader = Math.min(u, v);
int preLeader = Math.max(u, v);
for (int i = 0; i <= V; i++) {
if (leader[i] == preLeader) {
leader[i] = newLeader;
}
}
}
Edge[] Print(Edge MST[]) {
for (int i = 0; i < MST.length; i++) {
System.out.println(MST[i].src + " --> " + MST[i].dest + " == " + MST[i].weight);
}
return MST;
}
int Cost(Edge MST[]) {
int cost = 0;
for (int i = 0; i < MST.length; i++) {
cost += MST[i].weight;
}
return cost;
}
Edge[] CreateEdge() {
Edge MST[] = new Edge[V - 1];
for (int i = 0; i < MST.length; i++) {
MST[i] = new Edge();
}
return MST;
}
void KruskalMST() {
result = CreateEdge();
Arrays.sort(edge);
MakeSet();
int i = 0;
for (Edge next_edge : edge) {
int x = FindSet(next_edge.src);
int y = FindSet(next_edge.dest);
if (x != y) {
result[i++] = next_edge;
Union(x, y);
}
}
System.out.println("Minimum Spanning Tree Edges :");
Print(result);
System.out.println("Minimum Spanning Tree Cost : " + Cost(result));
}
void SecondBestMST() {
result1 = CreateEdge();
result2 = CreateEdge();
int sCost = Integer.MAX_VALUE;
int cost;
for (int k = 0; k < V - 1; k++) {
MakeSet();
int i = 0;
for (Edge next_edge : edge) {
if (next_edge != result[k]) {
int x = FindSet(next_edge.src);
int y = FindSet(next_edge.dest);
if (x != y) {
result1[i++] = next_edge;
Union(x, y);
}
}
}
cost = Cost(result1);
if (cost < sCost) {
sCost = cost;
result2 = result1;
}
}
System.out.println("\nSecond Best Minimum Spanning Tree Edges :");
Print(result2);
System.out.println("Second Best Minimum Spanning Tree Cost : " + sCost);
}
public static void main(String[] args) throws FileNotFoundException {
int V;
int E;
File file = new File("C:\\Users\\great computer\\Desktop\\SecondBestMST.txt");
Scanner sc = new Scanner(file);
V = sc.nextInt();
E = sc.nextInt();
SecondBestMST graph = new SecondBestMST(V, E);
for (int i = 0; i < E; i++) {
graph.edge[i].src = sc.nextInt();
graph.edge[i].dest = sc.nextInt();
graph.edge[i].weight = sc.nextInt();
}
graph.KruskalMST();
graph.SecondBestMST();
}
}