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?