Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting cycles in incremental graph

Given that I have an initially empty graph, to which will, incrementally, be added edges (one by one), what would be the best way to detect and identify appearing cycles?

Would I have to check for cycles in the entire graph every time a new edge is added? This approach doesn't take advantage of computations already made. Is there an algorithm that I still haven't found?

Thanks.

like image 332
zync Avatar asked Nov 27 '25 04:11

zync


1 Answers

You can use the Quick Union Find algorithm here to 1st check whether the two ends of the new edge are already connected.

EDIT:
As noted in the comment, this solution works only for an undirected graph. For a directed graph, following link may help https://cs.stackexchange.com/questions/7360/directed-union-find.

like image 83
Abhishek Bansal Avatar answered Nov 29 '25 20:11

Abhishek Bansal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!