1

I am searching for paths using some generic definitions. For example, when I search for A to B, I actually want to find paths like A to B, A_1 to B_2, A to B_1, or A_5 to B.

The middle nodes traversed may or may not be have a suffix like _N based on the connections in the tree. My prolog statement is like below.

path(A,B,Path) :-
  travel(A,B,[A],Q),
  reverse(Q,Path).

travel(A,B,P,[B|P]) :-
  arc(A,B).
travel(A,B,Visited,Path) :-
  arc(A,C),
  C \== B,
  \+member(C,Visited),
  travel(C,B,[C|Visited],Path).

Using this prolog code, I can find some paths but can not find others. For example, for the generic search criteria A -->15

Paths like (A, 1, 4, 7, 15) are ok,

Paths like (A, 1_1, 4_3, 7, 15) are also ok

Paths like

  • (A_1, X, X, X, 15)
  • (A, X, X, X, 15_2),
  • (A_3, X, X, X, 15_1)

are not ok

Note: X is a placeholder for any node either with the underscore or not.

Can you please help me to find out the problem.

Many thanks, Ferda

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
Ferda-Ozdemir-Sonmez
  • 674
  • 2
  • 13
  • 38

2 Answers2

2

I would extend the arc/2 predicate, or better, would introduce a service arc_ext/2, and would change travel/4 accordingly:

travel(A,B,P,[B|P]) :- arc_ext(A,B).
travel(A,B,Visited,Path) :- arc_ext(A,C),...

% TBD: optimize, when the logic has been tested
arc_ext(A,B) :-
  arc(X,Y),
  sub_atom(X,0,_,_,A), % X starts with A
  sub_atom(Y,0,_,_,B).


CapelliC
  • 59,646
  • 5
  • 47
  • 90
0

adding this line solved the problem.

path_generic(A,B,Path) :- path(X,Y,Path),sub_atom(X,0,_,_,A), sub_atom(Y,0,_,_,B).
Ferda-Ozdemir-Sonmez
  • 674
  • 2
  • 13
  • 38