Lab 4. Since this lab has several components in it, I'm providing some guidance to keep everyone

Similar documents
Supplement to A Practical Guide to Data Structures and Algorithms Using Java. Project 4

CS 211 Programming Practicum Spring 2017

Final Exam. 1. (8 pts) Consider the following directed graph. If you iterate through all of the vertices as

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Tips from the experts: How to waste a lot of time on this assignment

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Tips from the experts: How to waste a lot of time on this assignment

CS 211 Programming Practicum Spring 2018

CS380 Algorithm Design & Analysis Assignment 4: Disk Scheduling 1

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

CS211 Programming Practicum Fall 2017

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 211 Programming Practicum Fall 2018

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

(Refer Slide Time: 01.26)

Review sheet for Final Exam (List of objectives for this course)

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Lab 5 Classy Chat. XMPP Client Implementation --- Part 2 Due Oct. 19 at 11PM

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Advanced Programming - CS239

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

CS61BL: Data Structures & Programming Methodology Summer 2014

CHAPTER 13 GRAPH ALGORITHMS

CS 340 Homework Alternative 6. Due 11:59 PM Wednesday December 12

Lecture 5: Implementing Lists, Version 1

Close Your File Template

Lecture 28: Graphs: Shortest Paths (Part 1)

Final Exam. If you plan to solve a problem using a standard graph algorithm then you should clearly

CE 221 Data Structures and Algorithms

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

CISC 3130 Data Structures Spring 2018

Standard ADTs. Lecture 19 CS2110 Summer 2009

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

San José State University Department of Computer Science CS-144, Advanced C++ Programming, Section 1, Spring 2018

Step 1b. After clicking Create account, you will land on the Request an Egencia User Account page where you will enter the following information:

CSE 21 Spring 2016 Homework 5. Instructions

Collections, Maps and Generics

Data & Procedure Priority queues

Introduction to Programming System Design CSCI 455x (4 Units)

Total Score /1 /20 /41 /15 /23 Grader

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

Spring, 2014 CIT 590. Programming Languages and Techniques Homework 7

You must define a class that represents songs. Your class will implement the Song interface in $master/proj2/cs61b/song.java.

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

In this lecture Simple List Algorithms

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CMPS 240 Data Structures and Algorithms Test #2 Fall 2017 November 15

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

(Refer Slide Time: 05:25)

CISC 3130 Data Structures Fall 2018

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Shortest Path Problem

Worksheet 3: Predictive Text Entry

CS Homework 11 p. 1. CS Homework 11

Objects and Iterators

CSCI 200 Lab 3 Using and implementing sets

15-110: Principles of Computing, Spring 2018

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

Graph Algorithms Introduction to Data Structures. Ananda Gunawardena 7/31/2011 1

PRIORITY QUEUES AND HEAPS

CSC 101 Spring 2010 Lab #8 Report Gradesheet

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

1. Suppose you are using a HashMap<String,Integer>. What is the runtime of put and get assuming you have a reasonable hash function?

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Routing & Congestion Control

Part 1: Group Brainstorm (NO computers during this time) Part 2: Submit Individual Brainstorm (You can now use a computer)

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS 209 Spring, 2006 Lab 11: Files & Streams Instructor: J.G. Neal

Programming Exercise 14: Inheritance and Polymorphism

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

Java Review. Fundamentals of Computer Science

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Security and Privacy

Model Solutions. COMP 103: Test April, 2013

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

VE281 Data Structures and Algorithms. Introduction and Asymptotic Algorithm Analysis

Tips from the experts: How to waste a lot of time on this assignment

ASSIGNMENT 5 Objects, Files, and More Garage Management

Topic 7: Algebraic Data Types

University of Waterloo CS240, Winter 2010 Assignment 2

Dijkstra s Algorithm

CS Exam 1 Review Suggestions

(Refer Slide Time: 01:25)

3 ADT Implementation in Java

CSE 100: GRAPH SEARCH

Templates and Operator Overloading in C++

CSCI 355 LAB #2 Spring 2004

Transcription:

CS 241 Algorithms and Data Structures Spring Semester, 2003 Lab 4 April 1, 2003 Due Date: Tuesday April 15 This lab will bring together several of the topics we have studied this semester to nd the route (i.e. sequence of ights) from a given airport at a desired arrival time to a desired destination airport in the shortest amount of time. Here's a some sample output of the last part (Part 3). I hope almost everyone is able to complete Part 3. Options: Compute Shortest Routes(1), Show Route (2), Exit(3): 1 Source Airport Code: BOS Arrival Time at Airport: 1100 AM or PM (A or P):A Options: Compute Shortest Routes(1), Show Route (2), Exit(3): 2 Destination Airport Code: HOU Elapsed time since BOS airport arrival to HOU arrival is 7 hours and 45 minutes. Itinerary: TW 53 (BOS 1203 PM --> STL 223 PM) WN 759 (STL 400 PM --> HOU 545 PM) Options: Compute Shortest Routes(1), Show Route (2), Exit(3): 2 Destination Airport Code: SFO Elapsed time since BOS airport arrival to SFO arrival is 9 hours and 36 minutes. Itinerary: TW 53 (BOS 1203 PM --> STL 223 PM) TW 183 (STL 325 PM --> SFO 536 PM) Options: Compute Shortest Routes(1), Show Route (2), Exit(3): 2 Destination Airport Code: JFK Elapsed time since BOS airport arrival to JFK arrival is 5 hours and 13 minutes. Itinerary: DL 1865 (BOS 245 PM --> JFK 413 PM) Since this lab has several components in it, I'm providing some guidance to keep everyone on track. I strongly recommend that everyone complete the rst two parts (or at least the rst part) by the early submission deadline on April 8. Remember that you receive a 10% bonus for whatever portions you hand in by April 8 at the start of class. Part 1: Binary Heap (40 pts) Design and implement a General BinaryHeap Class that supports the methods described below. As part of your binary heap you must haveahandle class (ideally, as an inner class). My recommendation is that each Handle object will have a single instance variable that is an integer holding the index in the array representation of the binary heap where the given item is held. 1

Here are the methods that you are to implement. The methods size and isempty should run in constant time. All other methods should run in O(log n) time where n is the size of the heap. BinaryHeap.Handle insert(double key,object data) takes as input a key (a double) and a reference to the associated data that could be an object of any type (of type Object in Java or using a Template in C++). It should insert the given item in the binary heap returning a Handle to it. int size() returns the number of items in the heap. boolean isempty() returns true i the heap is holding no items. double minimumkey() returns the value of the minimum key. Object extractmin() removes the minimum item returning its data. boolean decreasekey(binaryheap.handle h, double newkey) takes as input a handle and a new key. Let x be the binary heap item specied by the h. If x's key is smaller than newkey then it does nothing and returns false (indicating the key was not changed). Otherwise, it decreases the value of x's key to newkey (xing the binary heap and handles as appropriate) and returns true. As an alternate, and really better option, you can have the decreasekey method be part of the BinaryHeap.Handle class in which case it will just take the new key as its single parameter. tostring (or in C++ overload the << operator) which takes no parameters and returns a string (or ostream in C++). So that we can tell the structure of the binary heap, please output the key and associated data for the array elements of your binary heap representation in the order they appear in the array. You do not need your program to show the binary heap as a tree but you may nd this helpful to do by hand so you can check your output. A driver called BinaryHeapTester (.java and.cc) is provided. You must turn in your output from this when you submit Part 1. Note that in BinaryHeapTester the associated data is just a string. You may modify BinaryHeapTester if your interface is slightly dierent than above { just describe the modications made in your write-up. I strongly recommend that you write a private swap method that swaps two elements in the binary heap (and only move elements using this method). By doing this all of the maintenance required to keep the instance variable within the Handle correct can be done within this method. In fact, you may want to just implement the binary heap without the Handle and then add it. If you implement your binary heap methods using a swap method then it will be very easy to incorporate the Handle. Part 2: Weighted Directed Graph (30 pts) Design and implement a Weighted Directed Graph Class that allows multiple edges between vertices (which in the application in Part 3 will be airline ights between cities). I 2

strongly recommend that you also implement a Vertex class that will hold the information associated with each vertex and an Edge class that will hold the information associated with each Edge. Here are the basic methods that I recommend you have for each class { add others as needed. I think their names are self explanatory. If you have any questions, please ask. (For those using C++ replace the tostring methods by overloading the << operator.) For the Edge class you really just need to store the head, tail and the edge weight. constructor Edge(Vertex head, Vertex tail, double weight) accessor public methods: Vertex gettail(), Vertex gethead(), double getweight() String tostring() For the Vertex class I recommend that you use an adjacency list of Edges. A little more will need to be added for Part 3, but do that after you get Part 2 working. constructor Vertex(String name) where name is what will be used by the tostring method. String tostring() Finally for the graph class you most likely will want instance variables for the number of vertices, number of edges, and then the adjacency list. I recommend the following methods: constructor Graph() that initializes the graph to have no vertices and no edges (i.e. an empty graph). void addvertex(vertex v) void addedge(edge e) int numberofvertices() int numberofedges() Iterator outedges(vertex v)- return an iterator that goes through all of the outgoing edges from v (in an arbitrary order). If you use a Java ArrayList for your list of adjacent vertices then you can just use the provided iterator() method of an ArrayList to create this iterator. The two methods of the Iterator class you would then use are hasnext() and next(). Iterator vertexiterator() - returns an iterator over all of the vertices in the graph (in an arbitrary order). String tostring() -To be sure that both of your above iterators work. For the tostring method you should use vertexiterator to iterate over all of the vertices. Then for each vertex v use outedges(v) to iterator over v's outgoing edges printing out each one (giving the head, tail, and weight in some easy to read form). 3

A driver called GraphTester (.java and.cc) is provided. You must turn in your output from this when you submit Part 2. You may modify this driver if your interface is slightly dierent than above. Here is the graph that is created by the graph tester. 10.75 C 4 8 2 D 7 E 3.14 4 14 9 K B 11 8 7 I A 1 6 H 13 G 2 10 F J 3.5 Part 3: Fastest Route Application (30 pts) Design and Implement avariant of Dijkstra's Shortest Path Algorithm for the application described on the rst page. Believe it or not, you are really almost done { your graph and binary heap classes do all the work. You just need to make the right calls to them :-). I'd like everyone to spend some time thinking about how you modify Dijkstra's algorithm for this problem and so won't give the details here. I will go over it in class on Tuesday April 8 (and in the help sessions if desired) so if you prefer just save Part 3 for the second week of the assignment. Let me state the problem once again. You are given an list of all ights between a set of airports. The user gives a departure airport and a time when he/she would like to arrive at the airport. The user will also gives a destination airport. The task is to nd a sequence of ights that will get the user to the destination airport at the earliest time with the restriction of a minimum layover of one hour at any intermediate airports and also the ight out of the departure airport must be at least one hour after the stated arrival time. Your output should include all of the information given in the sample output on page 1 but can format it dierently if you desired. There are two data les: airport-data.txt and flight-data.txt both of which contain actual airline schedules (with over 3500 ights) from 1992 collected by Roberto Tamassia from EasySABRE. The le airport-data.txt rst contains the number of cities (an integer). Then for each city there is a line with two items (separated by a tab). The rst is a 3 letter airport code. The second is an oset from GMT. The data from which I constructed these two data les also contained the x and y coordinate of the airport and the name of the city/airport but I removed those. If anyone would like to see the original data, it is full-airplane-data.txt. However, you don't need to use this for your lab. I recommend that you create an Airport class that extends Vertex. This can hold the GMT conversion that is provided in the data le and in general could hold the other data associated with an airport. The le flight-data.txt contains one line per ight that is tab delimited with the following 8 items: airline, ight number, code for source airport, local departure time, A or 4

P (for am or pm) for departure time, code for destination airport, local arrival time, A or P for arrival time. The original data set also had elds related to the fare class, number of stops, and more. I recommend that you create a Flight class that extends Edge that can hold the above information and will have the needed accessors. The only further change is that you might want to add some methods to your Vertex/Airport class or you may prefer to keep this in your driver. The variables you need to store for each vertex/airport are: the handle for calling decreasekey, a boolean indicating whether or not it is still in the binary heap, the edge from its parent (set when it is removed from the binary heap), and the cost to reaching it from the source (also set when it is removed from the binary heap). Also, you will need a way to access an Airport when given its three letter code since that is what the user will input. Just use a hashtable for this (either from Lab 2 or simply use the Java provided HashMap { the key is the three letter code and the associated data is the Airport). For testing this part, you should make a simple driver that provides the following two options (plus whatever others you want for helping debug). First the user should be able to give a source airport and a desired arrival time at the airport and have the best route (i.e. sequence of ights) computed between that airport and all others. The second option should allow the user to provide a destination airport and give the best route computed from the source airport (based on the last call made to the rst option). The driver will also need to read in the data. To help reduce the time you spend here, code fragments to read each of the data les and store the elds in local variables (that you can then decide how to use) are provided on the course home page under labs. You will be given (in the \What to Submit" section) some specic routes to compute. Your output should include all of the information show on page 1. For debugging purposes you might want to make a small version of the data set so that you can trace what is happening. To help reduce the time you need to spend on converting times into the same time zone and computing ight lengths and layover lengths, on the course page under labs, I'm providing a class GMTtime with the following methods: constructor GMTtime(int localtime, int gmtoffset, boolean am) int minutessince(gmttime time) which returns the number of minutes (which could be greater than 60) that have elapsed from the this to time. This will properly deal with times going across AM and PM. String tostring() returns a string with the times in the form they were in the data le except it uses \AM" and \PM" versus just \A" and \P". For your convenience the constructor computes the value of several instance variables that you can use in other methods if needed. These include the local time, the local time in a 24 hour clock (called localmilitarytime), the GMT oset, local hours, minutes (between 0 and 59), the GMT time, and nal the GMT time converted completely to minutes. For example 2:30AM would be 150 minutes into the day. 5

What to Submit For this lab you are expected to submit the following. Please include these items in the given order so that the TAs can nd everything easily. A completed and signed cover sheet with your name written legibly on the top. A write-up that describes your basic design with the focus being on variations/additions from what is described above. If there were any problems with your implementation (e.g. wrong output was obtained, a run-time error occurred) then clearly indicate that in your write-up and give as much information as you can as to what you think is causing the problem. Part 1 when submitted should be clearly labeled and include: your binary heap code and the output from BinaryHeapTester. It is your job to check that the output is correct and if it is not to clearly mark any problems you noticed. You do NOT need to submit BinaryHeapTester {if you made any changes to it just summarize those in your write-up. Part 2 when submitted should be clearly labeled and include: all code that is used for your graph representation (e.g. the Graph, Vertex, and Edge classes) as well as your output form GraphTester. It is your job to check that the output is correct and if it is not to clearly mark any problems you noticed. You do NOT need to submit GraphTester {if you made any changes to it just summarize those in your write-up. Part 3 when submitted should be clearly labeled and include: any code you wrote that was not included in either Part 1 or Part2. This includes your driver. Finally, you should provide the output from the following: { With a source city of STL and desired airport arrival of 7AM show the fastest route to ABQ and PVD. { With a source city of PHX and desired airport arrival of 6PM show the fastest route to PVD, DEN and STL. Although you should not submit the output for the queries shown on page 1 of this assignment, I would recommend you use them to help test that your program is working. If your output does not match that shown on page 1 (and lands in the desired city later than what is shown) then there is a problem. If you cannot x the problem then submit this output to help illustrate what is happening. Finally, for all of your submitted output you should check that the elapsed time given from the arrival at the source airport to the arrival at the destination airport is correct. Don't forgot to account for dierent time zones. You can tell from the GMT oset which time zone each airport is in. 6