1 | #!/usr/bin/env swipl -s
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Rick van der Zwet
|
---|
5 | * 0433373
|
---|
6 | * Prolog programming assigment 1
|
---|
7 | * Licence: BSD
|
---|
8 | * $Id: assignment1.pro 299 2007-11-08 00:10:31Z rick $
|
---|
9 | */
|
---|
10 |
|
---|
11 | /* final call, [X,Y] member of Graph */
|
---|
12 | trans_dag(X, Y, Graph) :-
|
---|
13 | member([X,Y], Graph).
|
---|
14 |
|
---|
15 | /* Find all nodes which are findable from X */
|
---|
16 | trans_dag(X, Y, Graph) :-
|
---|
17 | member([X,Z], Graph),
|
---|
18 | trans_dag(Z, Y, Graph).
|
---|
19 |
|
---|
20 | /* final call, [X,Y] member of Graph, do note initial path is set []
|
---|
21 | * over here */
|
---|
22 | trans(X, Y, Graph, _, []) :-
|
---|
23 | member([X,Y], Graph).
|
---|
24 |
|
---|
25 | /* Find all nodes which are findable from X, but where
|
---|
26 | * passed node is not member of Avoid, to avoid cycles
|
---|
27 | * Path is constructed just the other way around
|
---|
28 | */
|
---|
29 | trans(X, Y, Graph, Avoid, Path) :-
|
---|
30 | not(member(X, Avoid)),
|
---|
31 | member([X,Z], Graph),
|
---|
32 | Path=[Z|PathTail],
|
---|
33 | AvoidAdded=[X|Avoid],
|
---|
34 | trans(Z, Y, Graph, AvoidAdded, PathTail).
|
---|
35 |
|
---|
36 | /* init trans to trans 'system' call, which just ignored the Path and
|
---|
37 | * makes the Avoid list starting []
|
---|
38 | */
|
---|
39 | trans(X, Y, Graph) :-
|
---|
40 | trans(X, Y, Graph, [], _).
|
---|
41 |
|
---|
42 |
|
---|
43 | /* init trans to path 'system' call
|
---|
44 | * makes the Avoid list starting []
|
---|
45 | */
|
---|
46 | path(X, Y, Graph, Path) :-
|
---|
47 | trans(X, Y, Graph, [], Path).
|
---|