You are on page 1of 2

A

:
: .,.

9:

(, ,
, , , ) .
. :
() , () -
. ,
.
a
1

3
5

e
1

3
d
2

5
"".
.
.
a b 3 (
).
Prolog
.
:
next_to(a,b).
next_to(a,c).
next_to(a,f).
next_to(b,c).
next_to(b,d).
next_to(b,e).
next_to(c,d).
next_to(d,e).
next_to(d,f).
, next_to .
next_to(a,b) next_to(b,a)
a b b a.
- :
() . ..
a b
next_to(a,b).
next_to(b,a).
() , next
X, Y, Y, X,
next(X,Y) :next_to(X,Y).
next(X,Y) :next_to(Y,X).
,
.. link
:

link(a,b,3).
link(a,c,4).
link(a,f,1).
link(b,c,5).
link(b,d,3).
link(b,e,2).
link(c,d,2).
link(d,e,1).
link(d,f,5).



.
X Y
existspath(X,Y) :next_to(X,Y).
existspath(X,Y) :next_to(X,Z),
existspath(Z,Y).
existspath Yes No .
X Y .
X Y Z X
Z Y.



() path(X,Y,Path). X Y
Path. :
?- path(a,e,Path).
Path = [a,b,e];
Path = [a,b,d,e];
Path = [a,c,d,e]; ...
() pathlength(X,Y,Path,Length). path
X Y (
). :
?- pathlength(a,e,Path,Length).
Path = [a,b,e]
Length = 2;
Path = [a,b,d,e]
Length = 3;
Path = [a,c,d,e]
Length = 3; ...
() pathcost(X,Y,Path,Cost). path
X Y ,
. :
?- pathcost(a,e,Path,Cost).
Path = [a,b,e]
Cost = 5;
Path = [a,b,d,e]
Cost = 7;
Path = [a,c,d,e]
Cost = 7; ...
() pathloop(X,Y,Visited,Path). path
X Y
. :
?- pathloop(a,e,[],Path).

You might also like