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
andj
are connected:A(i,j) = 1
j
andk
are connected:A(j,k) = 1
k
andi
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 fromj
, i.e., all the 1s in thej
-th row ofA
- 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.