Prolog example exercise – flights

We plan a trip to Tokio. We have data about plane flights. For each flight we know duration of flight and distance between airports. Our data is being stored in predicates flight(StartAirport, DestinationAirport, Duration, Distance). On each airport we spent some time waiting (customs etc.) and this waiting time is different for each airport. Data is stored in predicates waiting(Airport, WaitingTime). International abbreviations of airports are: Paris-cdg, Rim-fco, New York-jfk, Los Angeles-lax, Singapore-sin, Ljubljana-lju, Tokyo-nrt.


Data stored in predicates


Connections graph

prolog excercise graph airport

Prolog predicate that checks whether connection exists

Define predicate connExists(Start,Destination) that checks whether there is connection between two airports.


Prolog predicate that finds all paths between two nodes

Define predicate travelTime(Start,Destination,Time,Path). Predicate should find all paths between airports Start and Destination. It should return path and travel time for each path (travel time is time of all necessary flights plus waiting time).

usage example:

Time = 15, Path = [lju,cdg,fco] ?


Prolog predicate to build shortest paths database

Write predicate allShorthestPaths, that builds database of all shortest paths – shortestPath(Start,End,Time).


Prolog predicate to return length of given path

Write predicate pathLength(Path,Length) that returns length of given path.

Usage example:

Length = 2055 ?



Leave a Reply

Your email address will not be published. Required fields are marked *

Please prove me that you are not spamer :) * Time limit is exhausted. Please reload CAPTCHA.