0

[enter image description here]

How can I write (using print_path) a rule to print the path from one node to another if exists

Print_path(a, c, Res) --->  Res=[a, f, c]

What I did was :

path(a,b). %there is a path from a to b 
path(a,f).
path(b,c).
path(c,d).
path(c,e).
path(e,d).
path(f,g).
path(f,c).
path(f,e).`

I do not know what to do next.

false
  • 10,264
  • 13
  • 101
  • 209
fayez
  • 1
  • 3
    "I don't know what to do next" doesn't give us much to go on, in terms of how much you understand. Are you comfortable with writing basic relations in prolog? If so, have a read about recursion and the accumulator pattern (both in general, and specifically with respect to prolog). In general you will want to define a helper predicate that recursively fills in an accumulator with the required nodes, and uses the base case to copy the final contents of the accumulator onto an output variable. Look at a basic prolog textbook, the "train stations" problem is literally the first problem in any book – Tasos Papastylianou Dec 13 '16 at 02:35
  • "Won't somebody please do my homework for me?" – Daniel Lyons Dec 14 '16 at 06:30
  • @TasosPapastylianou Thank you i am studying (recursion) right now , and I almost understand how the solution – fayez Dec 14 '16 at 18:37

0 Answers0