# Find a graph for which Kruskal's algorithm achieves worst-case running time

I am working on a problem in which I must find a graph with edge weights on n vertices, for which Kruskal's algorithm achieves worst-case running time. I am using a UNION-FIND data structure, but with no optimisations (path compression or weight/height union rules).

The version of Kruskal's which I am using is as follows:

Kruskal(G)
for each vertex π£ β π do MAKEβSET(π£)
sort all edges in non-decreasing order
for edge π’, π£ β πΈ (in the non-decreasing order) do
if FIND π’ β  FIND(π£) then
colour (π’, π£) blue
UNION(π’, π£)
od
return the tree formed by blue edges


Also, MAKE-SET(x), UNION(x, y) and FIND(x) are defined as follows:

MAKE-SET(π)
Create a new tree rooted at π₯
PARENT(π₯)=x

UNION(π, π)
PARENT FIND(π₯) β πΉπΌππ·(π¦)

FIND(π)
π¦ β π₯
while π¦ β  PARENT(π¦) do
π¦ β PARENT(π¦)
return y


After having looked at this post, my idea was to construct a graph on n vertices, such that there are as many redundant edges as possible and at least one necessary edge which is checked last (this edge must have the greatest weight).

I've looked at the specific cases n = 4 and n = 5 and have tried to construct such graphs in these cases. However, I am struggling to generalise to arbitrary n.

I would appreciate any hints for progressing with this problem. Thank you.

• Given that your pseudo-code doesn't break the loop when the tree is finished, it's really irrelevant how many redundant edges you encounter early, because you'll traverse and query all of them. Concentrate on designing a graph for which most edges will do O(n) work when querying FIND. Commented Mar 20, 2016 at 9:05

Let the answer be a complete graph $G = (V,E)$ with $|V| = n$ and a weight function $W : E \to \mathbb{R}$
Let the vertices be denoted by $1,2,\dots n$ and edges between $i$ and $j$ as $(i,j)$.
Now define the weights of all the edges as follows: $$W(i,j) = j-1, \forall \ 1 \le i < j$$
(Assuming you break ties for the edges on the basis of the value of the nodes $i,j$)
If you add a break statement once all $n$ nodes have been merged, then the algorithm will iterate on all the $\binom{n}{2}$ edges (except the last $n-1$) before finishing execution. This is because it will iterate all the edges in non decreasing order and it will find that except for the edge $(1,j)$ all other edges connect nodes belonging to the same disjoint set.
The running time will thus be $\mathcal{O}(\alpha(n + \binom{n}{2}))$