Skip to content

Conversation

@IsaacCheng9
Copy link
Owner

@IsaacCheng9 IsaacCheng9 commented Jan 14, 2026

Summary

More Efficient Implementation of Boruvka's Algorithm

  • Improved efficiency using union-find for optimal merge operations
  • Previously, we tracked components using a vertex_to_component dict, which resulted in O(V^2) for the total cost of all merges across the algorithm
    • merge_components() scanned all V vertices to update their component IDs
    • O(V) scan per merge * O(V) total merges = O(V^2) total for merging
  • With the new union-find, our total merge cost drops to O(V) by using a:
    • parent array to track component membership via a tree structure
    • find() to follow parent pointers to the root, with path compression
    • union() to link two trees, merging the smaller into the larger
    • Each find() / union() is O(a(V)), where a(V) is the inverse Ackermann function
      • This amortises to O(1) due to path compression and union by size, so the total merge cost drops to O(V)
  • Before changes, the total time complexity was O(E log V + V^2)
    • Edge scanning: O(E) per iteration * O(log V) iterations = O(E log V)
    • Merging: O(V) per merge * O(V) merges = O(V^2)
  • After changes, the total time complexity is now O(E log V)
    • Edge scanning: O(E) per iteration * O(log V) iterations = O(E log V)
    • Merging: O(a(V)) per merge * O(V) merges = O(V), as the inverse Ackermann function amortises to O(1)

Other Changes

  • Split out Boruvka's algorithm to be separate to the Graph class
    • Boruvka's algorithm is a standalone function with encapsulated union-find state

Origin

image

I looked into this after frobeniusfg left a comment on the YouTube video – thanks!

@IsaacCheng9 IsaacCheng9 changed the title Improve implementation efficiency using union-find Improve efficiency of merge operations using union-find to achieve O(E log V) implementation of Boruvka's algorithm Jan 14, 2026
@IsaacCheng9 IsaacCheng9 merged commit 2e8b651 into main Jan 14, 2026
2 checks passed
@IsaacCheng9 IsaacCheng9 deleted the improve-efficiency branch January 14, 2026 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants