CS202_Assignment3 - CS 202 Fundamental Structures of...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 202 Fundamental Structures of Computer Sciences II Assignment 3 – Heap and AVL Trees Due Date: April 21, 2011 (Thursday) Question 1 (15 Points)– Heaps a) (5 points) Given an array implementation of a heap, what is the index of the left child of a node that is in the kth location in the array? Please do not just write the answer, show its calculation. Otherwise, you will get no points from this question. b) (5 points) Prove by induction that your result for 1.a. is correct. c) (5 points) Prove the following statement by induction: For any two min ­heaps H1 and H2 of size N, if their preorder traversals are the same, then H1 = H2. Question 2 (10 Points)– 2 ­3 Tree Insertion & Deletion a) Starting with an empty 2 ­3 tree, insert the following keys into the tree in the given order. Show the resulting tree after each insertion. 9, 97, 37, 10, 30, 99, 52, 83, 51, 81 b) Delete the following values from your tree in the given order and draw your tree after each deletion. 9, 97, 81 Question 3 (70 Points) – Programming Assignment In this programming assignment, you will try to find a software solution to the problem of the director of the Bilkent Health Center. She is trying to figure out how many doctors should work in the emergency service of the center. For each doctor in the emergency service, the expense of the health center increases; however according to the standards of Bilkent, average waiting time for all patients should not exceed a given amount of time. So, she needs to optimize this number and asks for your help in this task. The center has the data of past patients. Your program should use these data to calculate average waiting times and find out the minimum number of doctors needed to meet the average waiting time requirement. The data are stored in a plain text file1. The first line of the file contains the number of patients. The subsequent lines contain four integers, each separated by one or more whitespace characters (space or tab). These denote, respectively, the patient id, the patient priority (from 1 to 10, higher is more urgent), arrival time (in minutes from a given point [e.g. 12:00 am]) and service time (in minutes). For example, from the file content given below, we understand there are 3 patients. The first patient with id 1 has priority of 9, arrives at the center at minute 1, and his treatment lasts for 5 minutes. The second patient with id 14 has priority of 3, arrives at the center at minute 70, and his treatment lasts 10 minutes. The third patient with id 5 has priority of 3, arrives at the center at minute 82, and his treatment lasts 70 minutes. 1 The file is a UNIX ­style text file with the end ­of ­line denoted by a single \n (ASCII 0x0A) Sample input file: 3 1 9 1 14 3 70 5 3 82 5 10 70 In this assignment, you are asked to write a simulation program that reads patient data from an input file and calculates the minimum number of doctors required for a given maximum average waiting time. In your implementation, you may make the following assumptions:  ­ The data file will always be valid. All data are composed of integers.  ­ In the data file, the patients are sorted according to their arrival times.  ­ There may be at most 200 patients in the data file. Your implementation must obey the following requirements:  ­  ­  ­  ­  ­ The patient with the highest priority should be examined first. In case of having two patients with the same highest priority, the patient who has waited longer should be selected first. If more than one doctor is available at a given time; the patient is assigned to the doctor with a lower id. Once a patient is assigned to a doctor, the doctor immediately starts treating that patient and is not available during the treatment time given for that patient. After the treatment of that patient ends, the doctor becomes available immediately. The waiting time of a patient is the duration (difference) between the arrival time of the patient and the time he is assigned to a doctor. In your implementation, you MUST use a heap ­based priority queue to store patients who are waiting for a doctor (i.e., to store patients who have arrived at the center but have not been treated yet). If you do not use such a heap ­based priority queue to store these patients, then you will get no points from this question. The name of the input file and the maximum average waiting time will be provided as command line arguments to your program. Thus, your program should run using two command line arguments. Thus, the application interface is simple and given as follows: username@dijkstra:~>./simulator <filename> <avgwaitingtime> Assuming that you have an executable called “simulator”, this command calls the executable with two command line arguments. The first one is the name of the file from which your program reads the patient data. The second one is the maximum average waiting time; your program should calculate the minimum number of doctors required for meeting this avgwaitingtime. You may assume that the maximum average waiting time is given as an integer. Hint Use the heap data structure to hold patients that are waiting for a doctor and to find the patient with the highest priority. Update the heap whenever a new patient arrives or a patient treatment starts. In order to find the optimum number of doctors needed, repeat the simulation for increasing number of doctors and return the minimum number of doctors that will achieve the maximum average waiting time constraint. Display the simulation for which you find the optimum number of doctors. SAMPLE OUTPUT: Suppose that you have the following input file consisting of the patient data. Also suppose that the name of the file is patients.txt. 12 1 2 3 4 5 6 7 8 9 10 11 12 20 40 10 10 20 40 20 40 10 10 20 40 1 1 1 1 4 7 9 11 13 14 15 17 10 14 6 5 10 14 10 14 6 5 10 14 The output for this input file is given as follows for different maximum average waiting times. Please check your program with this input file as well as the others that you will create. Please note that we will use other input files when grading your assignments. username@dijkstra:~>./simulator patients.txt 5 Minimum number of doctors requires: 4 Simulation with 4 doctors: Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor 0 1 2 3 3 2 1 0 3 2 0 1 takes takes takes takes takes takes takes takes takes takes takes takes patient patient patient patient patient patient patient patient patient patient patient patient 2 at minute 1 (wait: 0 mins) 1 at minute 1 (wait: 0 mins) 3 at minute 1 (wait: 0 mins) 4 at minute 1 (wait: 0 mins) 5 at minute 6 (wait: 2 mins) 6 at minute 7 (wait: 0 mins) 8 at minute 11 (wait: 0 mins) 7 at minute 15 (wait: 6 mins) 11 at minute 16 (wait: 1 mins) 12 at minute 21 (wait: 4 mins) 9 at minute 25 (wait: 12 mins) 10 at minute 25 (wait: 11 mins) Average waiting time: 3 minutes username@dijkstra:~>./simulator patients.txt 10 Minimum number of doctors requires: 3 Simulation with 3 doctors: Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor Doctor 0 1 2 2 1 0 2 0 1 0 1 2 takes takes takes takes takes takes takes takes takes takes takes takes patient patient patient patient patient patient patient patient patient patient patient patient 2 at minute 1 (wait: 0 mins) 1 at minute 1 (wait: 0 mins) 3 at minute 1 (wait: 0 mins) 6 at minute 7 (wait: 0 mins) 8 at minute 11 (wait: 0 mins) 5 at minute 15 (wait: 11 mins) 12 at minute 21 (wait: 4 mins) 7 at minute 25 (wait: 16 mins) 11 at minute 25 (wait: 10 mins) 4 at minute 35 (wait: 34 mins) 9 at minute 35 (wait: 22 mins) 10 at minute 35 (wait: 21 mins) Average waiting time: 9.83333 minutes Question 4 (5 Points) – Programming Assignment Now suppose that your simulation was to be run for the emergency service of a very large hospital (like Numune) with many potential doctors (N) and many, many more patients. Would it still be a good idea to try the simulation starting from 1 doctor and increasing until you found the right number K ≤ N? What is a better strategy for finding the optimum number of doctors in such a case? Code Format and Notifications To increase the efficiency of the grading process as well as the readability of your code, you have to follow the following instructions about the format and general layout of your program. • Don’t forget to write down your id, name, section, assignment number or any other information relevant to your program in the beginning of your main file. • Don’t forget to write comments at important parts of your code. • Indentation, indentation, indentation... • You are free to write your programs in any environment (you may use either Linux or Windows). On the other hand, we will test your programs on “dijkstra.ug.bcc.bilkent.edu.tr” and we will expect your programs to compile and run on the dijkstra machine. If we could not get your program properly work on the dijkstra machine, you would lose a considerable amount of points. Therefore, we recommend you to make sure that your program compiles and properly works on “dijkstra.ug.bcc.bilkent.edu.tr” before submitting your assignment. • Pay attention to these instructions, otherwise you may lose some points even though your code has no error. ...
View Full Document

This note was uploaded on 10/23/2011 for the course ENGINEERIN 102 taught by Professor Pablo during the Spring '11 term at Bilkent University.

Ask a homework question - tutors are online