Finding a cycles in a graph using Prolog?

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.

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.

I used Depth First Search with visited node list if we encounter any visited node during traversal it returns true. I tested with small inputs it looks like working correctly. Cycle(X):- cycleh(X,X).

Cycleh(X,Visited) :- edge(X,Y), (member(Y,Visited) ->! ,true; cycleh(Y,Y|Visited)).

It's been a while since I used Prolog, but perhaps this approach will work: A path is a sequence of edges where each edge starts on the node the previous edge ended on (e.g. A -> b, be -> c, c -> d). The trivial path is a single edge, and more complex paths can be formed by taking an existing path and adding an edge to it. A cycle is a path that starts and ends on the same node.

Can you use these definitions to build your Prolog rules?

I haven't been using Prolog for some time, but here is my approach to this problem. You could make rule path(X,Y) that checks if there exists path from node X to Y. A path is a single edge or an edge leading to a path.

Having this, it's easy to find cycle starting from node X -- it will be simply path(X,X). Here is my implementation (taken from the top of my head and not necessarily correct, but gives the idea): path(X,Y) :- edge(X,Y). Path(X,Y) :- edge(X,Z), path(Z,Y).

Cycle(X) :- path(X,X).

Take edge(a,b). Edge(b,c). Edge(c,b).

And the query? - cycle(X). Depth first Prolog will run into an infinite loop, and not find the cycle.

– Countably Infinite Jul 19 at 11:31.

This should do the trick: cycle( X ) :- cycle( X , ). Cycle( Curr , Visited ) :- member( Curr, Visited ) ,!. Cycle( Curr , Visited ) :- edge( Curr , Next ) , cycle( Next , Curr|Visited ) .

Although it appears to be a similar solution to @Gökhan Uras -- great minds think alike! Or something B^) The basic logic is that you have a cycle if the current node has already been visited (the first clause in the cycle/2 helper predicate. At that point, we cut(!) and declare success The reason for the cut (!) is that without it, backtracking would result in revisiting a node already visited and thus an infinite set of cycles.

If the current node has not been visited, we grab an edge anchored at the current node and visit that. Backtracking into the 2nd clause of cycle/2 visits the next edge, so once a particular path is exhausted, cycle/2 backtracks and tries another path.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions