Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple implementation for detecting cycles in a directed graph in C#

I wonder if you could help me with a simple implementation for detecting cycles in a directed graph in C#.

I've read about the algorithms but I'd like to find something already implemented, very simple and short.

I don't care about the performance because the data size is limited.

like image 285
Homam Avatar asked Jun 21 '11 11:06

Homam


People also ask

How can a program detect cycles in a directed graph explain?

To detect a cycle in a directed graph, we can either use the Depth First Search or the Breadth First Search approach. In the DFS technique, we check if there exists a back edge in the DFS tree of the graph because the existence of the back edge indicates the presence of a cycle.

How do you find the number of cycles in a directed graph?

So, one famous method to find cycles is using Depth-First-Search (DFS). By traversing a graph using DFS, we get something called DFS Trees. The DFS Tree is mainly a reordering of graph vertices and edges.

Can directed graphs have cycles?

Acyclic graphs don't have cycles. Directed acyclic graphs (DAGs) are specific names given to acyclic graphs. We can determine if a graph has a cycle by doing DFS and see if we re-explore a vertex that's on our current exploration path.


1 Answers

Check out QuickGraph - it has loads of the algorithms implemented and it's quite a nice library to use.

like image 156
Mike Goatly Avatar answered Nov 02 '22 03:11

Mike Goatly