I was searching for implementation of Karger's Algorithms and found this:
//Find, and Union are the use of Disjoint set union on the nodes.
MinCut(edges, V, E):
vertices=V
While(vertices > 2):
i=Random integer in the range [0,E-1]
set_1=find(edges[i].u)
set_2=find(edges[i].v)
if(set_1 != set_2):
vertices = vertices-1
Union(u, v)
ans=0
For(i in the range 0 to E-1):
set_1=find(edges[i].u)
set_2=find(edges[i].v)
if(set_1 != set_2):
ans = ans + 1
Return ans
Unlike other implementations I've seen online, this one doesn't involve adjacency matrices, self-loop removal etc.
While I understand how this works, I don't understand whether it'll be significantly slower than other implementations. We aren't removing any edges, so it seems there will be a lot of operations wasted on edges where set_1 == set_2.
The note at the end says that the time complexity for MinCut is ~O(E), but why does the while loop run for O(E) times? Can someone please elaborate on this?
Thanks