Graph path 4

Description

Generates paths between any two nodes of the graph

Source: PrologTutorial (on-line tutorial

Download

Program source code: graph_path_4.pl

Listing

/* prolog tutorial 2.15 Graph structures and paths */
 
 
 
edge(1,2).
 
edge(1,4).
 
edge(1,3).
 
edge(2,3).
 
edge(2,5).
 
edge(3,4).
 
edge(3,5).
 
edge(4,5).
 
 
 
connected(X,Y) :- edge(X,Y) ; edge(Y,X).
 
 
 
path(A,B,Path) :-
 
       travel(A,B,[A],Q), 
 
       reverse(Q,Path).
 
 
 
travel(A,B,P,[B|P]) :- 
 
       connected(A,B).
 
travel(A,B,Visited,Path) :-
 
       connected(A,C),           
 
       C \== B,
 
       \+member(C,Visited),
 
       travel(C,B,[C|Visited],Path).  
 
 
 
% ?- path(1,5,P).

Comments

pl/prolog/pllib/graph_path_4.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0