Document Sample
cheapest Powered By Docstoc
					                         CS2007 Practical 3: Finding Cheapest Route

The goal of this practical is to implement a simple search program which uses maps and finds
flights between cities. When you finish the practical session, please show the demonstrator
what you have managed to accomplish. The practicals web page contains an example
solution in a jar file, as well as source for (see below).

Part 1: Finding direct flights

Write a program which lets users find out if there are direct flights between two cities, and
what such a flight costs. The file contains a Java method which returns a
Map<String,Integer> with flight cost data; you can copy this into your code (and edit it if you

You will need to represent the flight information in Java. I suggest you use a
Map<String,Integer>, where the key consists of the origin and destination cities (eg,
"AberdeenLondon") and the value is the cost of the flight (eg, 50).

Optional: Use a Map<String,Map<String,Integer>> (a map whose key is the origin city and
whose value is a smaller map that represents flights from this city, as a map whose key is the
destination city and whose value is the cost of the flight) instead of a Map<String,Integer>;
this is in principle a better solution but it is harder to program.

Part 2: Finding single connections

Now modify your program so that it finds the cheapest route that involves one connection (eg,
Aberdeen to Boston via London). You can do this by finding all cities that you can reach
directly from origin city (ie, that are the destination of flights from the origin); these are
potential connection cities. Then compute the cost of reaching the ultimate destination city
via each possible connection, and find the cheapest route.

For example, if the user asks to fly from Aberdeen to Boston, the supplied file
shows four cities that can be reached from Aberdeen: Amsterdam, London, Paris, and
Stornoway. We can compute that the cost of Aberdeen-Amsterdam-Boston is 275; the cost of
Aberdeen-London-Boston is 300; the cost of Aberdeen-Paris-Boston is 270; and that
Aberdeen-Stornoway-Boston is impossible (because there are no flights from Stornoway to
Boston). Hence the cheapest route is Aberdeen-Paris-Boston.

Part 3 (optional challenge for good students): Finding multiple connections

Now modify your program so that it tries itineraries involving 2 or 3 connections, as well as
single connections and direct flights, and reports the cheapest possible route between the
user's two cities. You can do this by trying direct flights, single connections, double
connections, and triple connections, and seeing which one overall is cheapest.

You might find it useful to define a Path class which represents a path (including connecting
cities). This is just a suggestion, you do not have to do this.

Note that if you have a method which finds the cheapest route involving N connections, you
can use this method when finding the cheapest N+1 connection route. Find all cities that you
can reach directly from the origin city (as in part 2), and then use your existing method to find
the cheapest N-connection route from each possible "connection city" to the destination city.

Shared By: