Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting cycles in an adjacency matrix

Let A be the adjacency matrix for the graph G = (V,E). A(i,j) = 1 if the nodes i and j are connected with an edge, A(i,j) = 0 otherwise.

My objective is the one of understanding whether G is acyclic or not. A cycle is defined in the following way:

  • i and j are connected: A(i,j) = 1
  • j and k are connected: A(j,k) = 1
  • k and i are connected: A(k,i) = 1

I have implemented a solution which navigates the matrix as follows:

  • Start from an edge (i,j)
  • Select the set O of edges which are outgoing from j, i.e., all the 1s in the j-th row of A
  • Navigate O in a DFS fashion
  • If one of the paths generated from this navigation leads to the node i, then a cycle is detected

Obviously this solution is very slow, since I have to evaluate all the paths in the matrix. If A is very big, the required overhead is very huge. I was wondering whether there is a way of navigating the adjacency matrix so as to find cycles without using an expensive algorithm such as DFS.

I would like to implement my solution in MATLAB.

Thanks in advance,

Eleanore.

like image 574
Eleanore Avatar asked May 08 '13 08:05

Eleanore


People also ask

How will you detect cycle in undirected graph using adjacency matrix?

To detect if there is any cycle in the undirected graph or not, we will use the DFS traversal for the given graph. For every visited vertex v, when we have found any adjacent vertex u, such that u is already visited, and u is not the parent of vertex v. Then one cycle is detected.

How do you determine if a 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.

Can DFS detect cycles?

Depth First Traversal can be used to detect a cycle in a Graph. DFS for a connected graph produces a tree. There is a cycle in a graph only if there is a back edge present in the graph. A back edge is an edge that is joining a node to itself (self-loop) or one of its ancestor in the tree produced by DFS.


1 Answers

I came across this question when answering this math.stackexchange question. For future readers, I feel like I need to point out (as others have already) that Danil Asotsky's answer is incorrect, and provide an alternative approach. The theorem Danil is referring to is that the (i,j) entry of A^k counts the number of walks of length k from i to j in G. The key thing here is that a walk is allowed to repeat vertices. So even if a diagonal entries of A^k is positive, each walk the entry is counting may contain repeated vertices, and so wouldn't count as a cycle.

Counterexample: A path of length 4 would contain a 4-cycle according to Danil's answer (not to mention that the answer would imply P=NP because it would solve the Hamilton cycle problem).

Anyways, here is another approach. A graph is acyclic if and only if it is a forest, i.e., it has c components and exactly n-c edges, where n is the number of vertices. Fortunately, there is a way to calculate the number of components using the Laplacian matrix L, which is obtained by replacing the (i,i) entry of -A with the sum of entries in row i of A (i.e., the degree of vertex labeled i). Then it is known that the number of components of G is n-rank(L) (i.e., the multiplicity of 0 as an eigenvalue of L).

So G has a cycle if and only if the number of edges is at least n-(n-rank(L))+1. On the other hand, by the handshaking lemma, the number of edges is exactly half of trace(L). So:

G is acyclic if and only if 0.5*trace(L)=rank(L). Equivalently, G has a cycle if and only if 0.5*trace(L) >= rank(L)+1.

like image 104
Casteels Avatar answered Sep 18 '22 00:09

Casteels