Skip to content

Instantly share code, notes, and snippets.

@EdoardoVignati
Created January 23, 2021 13:37
Show Gist options
  • Save EdoardoVignati/158a123af3ef3759281193673e213202 to your computer and use it in GitHub Desktop.
Save EdoardoVignati/158a123af3ef3759281193673e213202 to your computer and use it in GitHub Desktop.
Minimal Ubuntu installation and working example of graph exploration with Prolog with DAG traversal and full console output
% sudo apt-get install software-properties-common
% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog
edge(1,2)
edge(2,3)
edge(3,4)
edge(2,6)
edge(6,7)
connected(X,Y) :- edge(X,Y).
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).
allPaths(A,B,LIST) :- findall(X, path(A,B,X), LIST).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment