-1

I am currently working on coding the Baruvka's algorithm as below. But I am given a question to check for edge e=uv if it already exists in the list of edges which are already taken using greedy choice. How can I search for an edge e with in O(logn) time? Could you please help with this... The search operation can be done using BFS or DFS and check if the edge exists or not but that becomes a costly operation if I do it for every phase.

Boruvka
 { 
while (T < n-1 edges)
 { 
Find the components of T 
for each tree Ti of the forest T
 { 
Find the smallest weight edge e = uv such that u is in Ti and v is not in Ti 

//Before adding e to T I have to check if it already exists in the list of greedy edges or not. If it exists then I would like to skip adding e to T and go to the next phase. 
Add e to T 
 } 
} 
 return T 
}

But there is a problem of adding one edge more than one time. So in order to eliminate that we need to check whether edge has already been added to T before adding it. Is there any O(n) implementation for this check?

Teja
  • 13,214
  • 36
  • 93
  • 155

1 Answers1

0

You should use disjoint set data structure or some other data structure for each component. When an edge connects two different connected components, how can the edge exist previously in the tree edge list? Please read Boruvka's algorithm.

Whenever you choose an edge, you should merge those two components connected by that edge. When merging, consider this large component as a new vertex, whose edges should be edges of its constituent components going outside.

abdullah
  • 662
  • 5
  • 7