'How to generate the worst case for disjoint set with only path compression?

A disjoint set with only path compression implemented is like this:

// In cpp.
int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); }
int Union(int a, int b) { f[Find(a)] = Find(b); }

From wiki I've learned about it has a worst complexity of O(n+f*(1+logn)). So how do I reach the worst complexity?



Solution 1:[1]

I assume you mean this

Using path compression alone gives a worst-case running time of O(n+f*(1+log2+f/n)),[10] for a sequence of n MakeSet operations (and hence at most n-1 Union operations) and f Find operations.

Where

[10] [Introduction to Algorithms]1

And I would guess it is when you always add to the longest path.

Solution 2:[2]

When the input is skewed in a way no compression can be applied, the worst case run time would be O(n).

    public static void main(String[] args) throws Exception {
        UnionFind uf = new UnionFind(10);
        uf.union(0, 1);
        uf.union(1, 2);
        uf.union(2, 3);
        uf.union(3, 4);
        uf.union(4, 5);
        uf.union(5, 6);
        uf.union(6, 7);
        uf.union(7, 8);
        uf.union(8, 9);
        System.out.println(Arrays.toString(uf.root)); //[1, 2, 3, 4, 5, 6, 7, 8, 9, 9]
    }
    static class UnionFind {
        public int[] root;

        public UnionFind(int size) {
            root = new int[size];
            for (int i = 0; i < size; i++) {
                root[i] = i;
            }
        }

        public int find(int x) {
            if (x == root[x]) {
                return x;
            }
            return root[x] = find(root[x]);
        }

        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX != rootY) {
                root[rootX] = rootY;
            }
        }
    }

 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Surt
Solution 2 chakwok