Skip to content

Commit bcf1bac

Browse files
committed
Add stop condition to Boruvkas algorithm
1 parent 126305a commit bcf1bac

File tree

1 file changed

+5
-0
lines changed
  • src/main/java/com/williamfiset/algorithms/graphtheory

1 file changed

+5
-0
lines changed

src/main/java/com/williamfiset/algorithms/graphtheory/Boruvkas.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void solve() {
7171
UnionFind uf = new UnionFind(n);
7272

7373
while (uf.components > 1) {
74+
boolean stop = true;
7475
Edge[] cheapest = new Edge[n];
7576

7677
// Find the cheapest edge for each component
@@ -81,12 +82,16 @@ private void solve() {
8182

8283
if (cheapest[root1] == null || e.cost < cheapest[root1].cost) {
8384
cheapest[root1] = e;
85+
stop = false;
8486
}
8587
if (cheapest[root2] == null || e.cost < cheapest[root2].cost) {
8688
cheapest[root2] = e;
89+
stop = false;
8790
}
8891
}
8992

93+
if (stop) break;
94+
9095
// Add the cheapest edges to the MST
9196
for (int i = 0; i < n; i++) {
9297
Edge e = cheapest[i];

0 commit comments

Comments
 (0)