in Swi prolog Given the following graph of possible flights between seven US cit
ID: 3597503 • Letter: I
Question
in Swi prolog
Given the following graph of possible flights between seven US cities::
Define a predicate route /33 that takes two cities as arguments and finds the routes to get from city A to a city B.. Your predicate should have the signature route((ccityA,, cityB,, Route)) .
Examples::
? - route((sseattle,, boston,, X))..
X = [sseattle,, omaha,, atlanta,, boston]] ;
false..
? - route(( fresno,, atlanta,, X))..
X = [ffresno,, seattle,, omaha,, atlanta]] ;
X = [ffresno,, albany,, seattle,, omaha,, atlanta]] ;
X = [ffresno,, albany,, dallas,, seattle,, omaha,, atlanta]] ;
false..
? - route((aalbany,, atlanta,, X))..
X = [aalbany,, seattle,, omaha,, atlanta]] ;
X = [aalbany,, dallas,, seattle,, omaha,, atlanta]] ;
false..
? - route((bboston,, atlanta,, X))..
false..
Omaha Seattle Albany Boston Fresno Dallas AtlantaExplanation / Answer
path(fresno,seattle).
path(fresno,albany).
path(fresno,boston).
path(seattle,omaha).
path(seattle,dallas).
path(omaha,albany).
path(omaha,atlanta).
path(albany,seattle).
path(albany,dallas).
path(atlanta,boston).
path(atlanta,dallas).
path(atlanta,albany).
path(dallas,seattle).
path(dallas,albany).
route(A,B,Route) :- direction(A,B,[A],Q), reverse(Q,Route).
direction(A,B,P,[B|P]) :- path(A,B).
direction(A,B,Visited,Route) :- path(A,C), C == B, +member(C,Visited), direction(C,B,[C|Visited],Route).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.