prolog - How to use list to record path from A to B? -
If I have a map with nodes 1,2, ..., n, then I know that I < From Code> n1 to n2
, I can use the following code:
path (1,2). Path (3,5). ..... get_to (A, B): - Path (A, B). Get_to (A, B): - Path (A, C), get_to (C, B).
But how can you record the path of b
and show it? First of all, we get the right of vocabulary: Direct connection between nodes (often called) is called edge. And the whole path is called path. Here is the first attempt:
path (A, B, [A, B]): - edge (A, B) path (A, C, [A, B | VS]): - edge (A, B), path (B, C, [B | VS]).
Note that the list can now be used to determine all the paths of a certain length.
? - Length (P, 10), Path (A, B, P)
Or even all paths, sorted by length:
? - Length (P, N), Path (A, B, P).
The price of this publicity is that this final query does not end. There are ways to fix this, but they are not straight forward.
Comments
Post a Comment