Please read the new rule regarding the restriction on the use of AI tools. ×

Time Complexity of Union-Find

Revision en1, by SAT2020, 2022-07-23 23:46:19

Hey everyone,

First off, I'm finally an expert! Thank you to everyone who has responded to my previous blog posts, your input helped me grow as a competitive programmer greatly!

Now, onto the actual question. What is the time complexity of using the "find" operation on an array-based union-find algorithm? First, let me put some pseudo-code of the union-find algorithm so that my ramblings will make more sense.

// We initialize the array such that each node is its own parent array = [0, 1, 2, 3, 4, 5... n]

// Find the "root" of a node function find(index): while (array[index] != index) index = array[index]

return index

// Join the "roots" of two nodes function union(a, b): array[find(a)] = array[find(b)]

Please someone correct me if my implementation is wrong or if there's any way I can improve my pseudo-code. Now, onto the actual question.

Let's imagine you have a straight, directed graph with randomized node order. For example: 3 --> 5 --> 1 --> 2 --> 4. I speculate that the time complexity of generating the union-find array is at worst n^2, and that once the array is generated, the time complexity of finding the root from any given node is at worst n. Let's go through the example I provided, assuming that we join nodes starting from the lowest node (1), then the second lowest (2), etc.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English SAT2020 2022-07-24 03:17:47 3 Tiny change: ' is going wrong here?' -> ' is going on here?' (published)
en6 English SAT2020 2022-07-24 03:16:12 17
en5 English SAT2020 2022-07-24 03:14:42 963
en4 English SAT2020 2022-07-23 23:48:38 19
en3 English SAT2020 2022-07-23 23:48:01 18 Reverted to en1
en2 English SAT2020 2022-07-23 23:47:38 18
en1 English SAT2020 2022-07-23 23:46:19 1395 Initial revision (saved to drafts)