Can we detect cycles in directed graph using Union-Find data structure?

后端 未结 2 1315
轮回少年
轮回少年 2021-01-03 00:30

I know that one can detect cycles in direct graphs using DFS and BFS. I want to know whether we can detect cycles in directed graphs using Union-Find or not

2条回答
  •  孤街浪徒
    2021-01-03 01:17

    No, we cannot use union-find to detect cycles in a directed graph. This is because a directed graph cannot be represented using the disjoint-set(the data structure on which union-find is performed).

    When we say 'a union b' we cannot make out the direction of edge

    1. is a going to b? (or)
    2. is b going to a?

    But, incase of unordered graphs, each connected component is equivalent to a set. So union-find can be used to detect a cycle. Whenever you try to perform union on two vertices belonging to the same connected component, we can say that cycle exists.

提交回复
热议问题