We are given a graph with the following facts:
edge(a,b)
edge(a,c)
edge(b,a)
edge(c,d)
edge(d,d)
edge(d,e)
edge(e,f)
edge(f,g)
edge(g,e)
And we are asked to define a rule, cycle(X)
, that determines if there is a cycle starting from the node X
.
I am really lost on how to do this, I tried attempting to traverse the nodes and checking if the next one would be the starting one again but I cannot seem to get it to work
We do a BFS traversal of the given graph. For every visited vertex 'v', if there is an adjacent 'u' such that u is already visited and u is not a parent of v, then there is a cycle in the graph. If we don't find such an adjacent for any vertex, we say that there is no cycle.
A Cycle Graph or Circular Graph is a graph that consists of a single cycle. In a Cycle Graph number of vertices is equal to number of edges. A Cycle Graph is 2-edge colorable or 2-vertex colorable, if and only if it has an even number of vertices.
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.
Archie's idea is a good starting point, but it will create an infinite loop if it finds another cycle while searching for the path.
I also haven't used prolog for years, but you will need something like path(X,Y,Visited)
, where you keep track of the visited nodes, preventing the endless loops.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With