With the complete set of path/2 facts, we can start finding routes
between nodes. We will revise what we learnt in the previous two
modules.
We can find if there is a route between two immediately adjacent
nodes:
| ?- path(a, 1).
yes
We can find which path or paths start or end with a particular node:
| ?- path(2, Node).
Node = 5 ?
yes
| ?- path(Node, 5).
Node = 2 ?
yes
We could even try a query such as path(Start, End).
We also saw how we could have more than one goal in a query. For
instance we could find if there was a route between two nodes via
an intermediate node.
| ?- path(a, 1), path(1, 2).
yes
A more interesting query would be one that used a variable to specify
the intermediate node:
| ?- path(a, Via), path(Via, 2).
Via = 1 ?
yes
Prolog has instantiated the first goal (path(a, Via)) against
the database of facts and thus has to find a second goal of path(1,
2).
If we wanted to traverse n intermediate nodes, we would
have to specify n+1 goals in our query.
|