简体   繁体   中英

Can we find the path between 2 node if they are connected using union-find algo?

    I studied the union find algo and found it is very useful in following ways.

    to find out if 2 node are connected or not
    to compress the path. (save space)

    if the problem is to find if 2 node connected or not , then we can use union
 find.But if we have to find out the path between these 2 node, 
then how can we use the data structure which is use to find 
union find ( data structure  use is - it store root element in 
arrays of node. form kind of tree)?

    I tried a lot and found that we have to use graph to find out the path\
between node and can not use union find data structure .

    any other views on it.

There are numerous algorithms to implement union/find but from your question it seems you are referring to the disjoint set forest implementation. Thus I will focus on this particular implementation.

To improve the performance of union and find operations, disjoint set forest apply different heuristics eg path compression. Theses heuristics improve the asymptotic complexity union and find, but as a result you can no longer reconstruct the original graph structure. For this reason you will not be able to find a path between the two vertices. After all the algorithm is meant to solve this particular problem - be able to perform union and find.

However you can use some other union/find implementation. For instance you can use the same idea as with Disjoint set forest, but without path compression(you can still use union by rank). This of course will increase the asymptotic complexity of union and find, but will also preserve the graph structure and thus you will be able to find a path between the vertices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM