Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 22, 2018 06:36
Show Gist options
  • Save DonaldKellett/03119b66d0d139fda6f3a46464cbdc73 to your computer and use it in GitHub Desktop.
Save DonaldKellett/03119b66d0d139fda6f3a46464cbdc73 to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 3 - Practical Session - Programming Exercise 1 - Directed graphs
% A directed graph (provided)
connected(1,2).
connected(3,4).
connected(5,6).
connected(7,8).
connected(9,10).
connected(12,13).
connected(13,14).
connected(15,16).
connected(17,18).
connected(19,20).
connected(4,1).
connected(6,3).
connected(4,7).
connected(6,11).
connected(14,9).
connected(11,15).
connected(16,12).
connected(14,17).
connected(16,19).
% path/2 - Given points X and Y, can we go from X to Y?
% Basis: We can go from X to Y if there is a directed edge (X, Y)
path(X, Y) :- connected(X, Y).
% Inductive step: If there is a directed edge (X, Z) for some Z and we can go from Z to Y then there is a path from X to Y
path(X, Y) :- connected(X, Z), path(Z, Y).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment