Question: The program: This program is used to determine if there are airline flights scheduled between a list of cities. Your program is required to do
The program: This program is used to determine if there are airline flights scheduled between a list of cities. Your program is required to do the following:
- Determine if there is a path from the first city to the second, each identified by a three letter code.
- Count the number of stops en-route (there may not be a direct flight.
FACTS:
The flights are listed in the following table:
| Origin City | Destination City |
|---|---|
| DGZ | QYY |
| DGZ | AZI |
| QYY | CSI |
| AZI | TVA |
| CSI | PPG |
| TVA | BRW |
| BRW | CSI |
Use this information to form the facts of your program Remember, your rules are of the form:
myrelationship(city1, city2).
In this case the fact name might be something like: flight, from_to, direct_flight, or some other name that represents the concept represented.
RULES:
Your program will need a recursive rule similar to the rule for ancestor in the "Prolog Basics" link above. Here is a hint for counting the number of stops: If there is a direct flight, the number of stops is zero (basis case). If you have a route already, adding a new flight in the recursive part adds one to the number of stops.
Queries:
To test your program you should run the following queries:
- Find all the routes from DGZ to PPG (there should be two, one will have two stops, the other will have four stops)
- Find a route from TVA to CSI (there should be one stop)
- Find all the routes to BRW (there are 3).
- Find all the routes to PPG (there are 7 of them of with 0, 1, 2, 3, and 4 stops)
Sample output from these queries:
?- route(dgz, ppg, Stops). Stops = 2 ; Stops = 4 ; false. ?- route(tva,csi,Stops). Stops = 1 ; false. ?- route(Orig, brw, Stops). Orig = tva, Stops = 0 ; Orig = dgz, Stops = 2 ; Orig = azi, Stops = 1 ; false. ?- route(Orig,ppg, Stops). Orig = csi, Stops = 0 ; Orig = dgz, Stops = 2 ; Orig = dgz, Stops = 4 ; Orig = qyy, Stops = 1 ; Orig = azi, Stops = 3 ; Orig = tva, Stops = 2 ; Orig = brw, Stops = 1 ; false. ?-
EXTRA Credit: You may receive 5 points extra credit for printing out the cities stopped in when there is no direct flight.
You can use SWI-Prolog to compile and run the code along with any other editing app. I was using sublime text. Thank you so much.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
