We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 126305a commit bcf1bacCopy full SHA for bcf1bac
1 file changed
src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java
@@ -71,6 +71,7 @@ private void solve() {
71
UnionFind uf = new UnionFind(n);
72
73
while (uf.components > 1) {
74
+ boolean stop = true;
75
Edge[] cheapest = new Edge[n];
76
77
// Find the cheapest edge for each component
@@ -81,12 +82,16 @@ private void solve() {
81
82
83
if (cheapest[root1] == null || e.cost < cheapest[root1].cost) {
84
cheapest[root1] = e;
85
+ stop = false;
86
}
87
if (cheapest[root2] == null || e.cost < cheapest[root2].cost) {
88
cheapest[root2] = e;
89
90
91
92
93
+ if (stop) break;
94
+
95
// Add the cheapest edges to the MST
96
for (int i = 0; i < n; i++) {
97
Edge e = cheapest[i];
0 commit comments