Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a directed graph is cyclic?

How can we detect if a directed graph is cyclic? I thought using breadth first search, but I'm not sure. Any ideas?

like image 325
iva123 Avatar asked Mar 26 '10 17:03

iva123


People also ask

How do you tell if a directed graph has a cycle?

The existence of a cycle in directed and undirected graphs can be determined by whether depth-first search (DFS) finds an edge that points to an ancestor of the current vertex (it contains a back edge). All the back edges which DFS skips over are part of cycles.

Is directed graph cyclic?

Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false. This diagram clearly shows a cycle 0 -> 2 -> 0.

Can a directed graph have a cycle?

A back edge is an edge that connects a vertex to its ancestor in the DFS tree, and if a DFS tree of a graph has a back edge, it contains a cycle, as the back edge will allow it to have a trail with repeated first and last nodes.

Can DFS detect cycle in directed graph?

Using a Depth First Search (DFS) traversal algorithm we can detect cycles in a directed graph. If there is any self-loop in any node, it will be considered as a cycle, otherwise, when the child node has another edge to connect its parent, it will also a cycle.


3 Answers

What you really need, I believe, is a topological sorting algorithm like the one described here:

http://en.wikipedia.org/wiki/Topological_sorting

If the directed graph has a cycle then the algorithm will fail.

The comments/replies that I've seen so far seem to be missing the fact that in a directed graph there may be more than one way to get from node X to node Y without there being any (directed) cycles in the graph.

like image 175
Peter Avatar answered Oct 06 '22 11:10

Peter


Usually depth-first search is used instead. I don't know if BFS is applicable easily.

In DFS, a spanning tree is built in order of visiting. If a the ancestor of a node in the tree is visited (i.e. a back-edge is created), then we detect a cycle.

See http://www.cs.nyu.edu/courses/summer04/G22.1170-001/6a-Graphs-More.pdf for a more detailed explanation.

like image 31
kennytm Avatar answered Oct 06 '22 11:10

kennytm


Use DFS to search if any path is cyclic

class Node<T> { T value; List<Node<T>> adjacent;  }

class Graph<T>{

    List<Node<T>> nodes;

   public boolean isCyclicRec()
   {

      for (Node<T> node : nodes)
      {
            Set<Node<T>> initPath = new HashSet<>();
            if (isCyclicRec(node, initPath))
            {
              return true;
            }
      }
      return false;
   }

   private boolean isCyclicRec(Node<T> currNode, Set<Node<T>> path)
   {
      if (path.contains(currNode))
      {
        return true;
      }
      else
      {
        path.add(currNode);
        for (Node<T> node : currNode.adjacent)
        {
            if (isCyclicRec(node, path))
            {
                return true;
            }
            else
            {
                path.remove(node);
            }
        }
      }
      return false;
  }
like image 28
Abhijeet Kushe Avatar answered Oct 06 '22 11:10

Abhijeet Kushe