Embed
Email

cheapest

Document Sample

Shared by: gegeshandong
Categories
Tags
Stats
views:
0
posted:
10/27/2011
language:
English
pages:
1
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 flights.java (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 flights.java contains a Java method which returns a

Map with flight cost data; you can copy this into your code (and edit it if you

wish).



You will need to represent the flight information in Java. I suggest you use a

Map, 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> (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;

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 flights.java 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.



Related docs
Other docs by gegeshandong
Mar - Mr Hanson
Views: 0  |  Downloads: 0
WhatDoYouMeanHighest.Price
Views: 0  |  Downloads: 0
core data
Views: 0  |  Downloads: 0
jan-18-2009b
Views: 0  |  Downloads: 0
Status - California State University
Views: 0  |  Downloads: 0
PHASE ONE
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!