I am a complete newbie in Prolog. I am trying to figure out a problem where I need to check if path is present between edges. I am done with acyclic graph code for cyclic my code is going to infinite loop.
path(Start, End) :- edge(Start, End).
path(Start, End) :- edge(Start, Z), path(Z, End).
I need to handle this case by defining a new predicate: new_path(Start,End,path) which should eliminate infinite loop. Please let me know how to proceed with it.
You need to keep track of what nodes you've visited as you go, using a prolog list as a LIFO stack. Something along these lines: