Given the following graph of possible flights between seven US cities: (graph ta
ID: 3731734 • Letter: G
Question
Given the following graph of possible flights between seven US cities: (graph taken from the web site of the American Mathematical Society, http://www.ams.org) Define a predicate route/3 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(cityA, cityB, Route). Examples:
?- route(seattle, boston, X).
X = [seattle, omaha, atlanta, boston] ; false.
?- route(fresno, atlanta, X).
X = [fresno, seattle, omaha, atlanta] ;
X = [fresno, albany, seattle, omaha, atlanta] ;
X = [fresno, albany, dallas, seattle, omaha, atlanta] ;
false.
?- route(albany, atlanta, X).
X = [albany, seattle, omaha, atlanta] ;
X = [albany, dallas, seattle, omaha, atlanta] ;
false.
?- route(boston, atlanta, X).
Omaha Seattle Albany Boston Fresno Dallas AtlantaExplanation / Answer
Please find my 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.