-4

this is a paragraph from data structure book, by Narasimha Karumanchi (disjoint set ADT)

fast find implementation(quick find)

this method uses an array, where the array contains the set name for each element.

In this representation to perform union(a,b)[assuming that a, is in set i and b is in set j] we need to scan the complete array and change all i to j. this take O(n).

my question 1

if find operation takes O(1) then why scan complete array and change all i to j ,only two of them need to change , so how it is O(n)

============

(book 8.8 section)

using an array which stores the parent of each element instead of using root as its set name, solve union O(n)

my 2. question

how does it solve issue using parent? And how both root and parent approaches gives us skew tree?

Raven
  • 11
  • 4

1 Answers1

1

Consider that you have 8 elements in 2 sets, where the set identifiers are 1 and 2 (so i = 1 and j = 2 in your description). Assume that 4 elements are in set 1 (elements 0, 1, 3, 7) and 4 elements are in set 2 (elements 2, 4, 5, 6).

Quick find implementation would represent this array as:

[ 1, 1, 2, 1, 2, 2, 2, 1 ]

And as the following representation (both elements 1 and 2 have self-loops):

     1           2
   / | \       / | \
  0  3  7     4  5  6

If you perform union(1, 2) and only update the set identifier (let’s assume that we are updating the elements in set 2 to point to set 1), then you would have an array such as:

[ 1, 1, 1, 1, 2, 2, 2, 1 ]

If you perform find(2) you get as a result the identifier 1, which is correct. But if you perform find(4) you get as a result the identifier 2, which is incorrect because element 4 now belongs to set 1.

So you have to scan the complete array (and therefore the operation has time complexity O(n)) to update the identifiers, to have the following array as a result of the union operation:

[ 1, 1, 1, 1, 1, 1, 1, 1 ]

And the following representation (with 1 having a self-loop)

         1
   / / | | \ \ \
  0 2  3 4  5 6  7

As I'm sure you will see later in the book, there are more efficient implementations of the union-find data structure, which are based on union by rank and path compression.

ruakh
  • 175,680
  • 26
  • 273
  • 307
Rene Argento
  • 53
  • 1
  • 8