CS202_Assignment5 - CS 202 Fundamental Structures of Computer Sciences II Assignment 5 – Hashing and Graphs Due Date: 23:59 

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 5 – Hashing and Graphs Due Date: 23:59  ­ ­ May 22nd, 2011 (Sunday) Question 1 (20 Points)– Hash Tables Consider inserting the keys 36, 18, 71, 3, 146, 38, 49 in the given order into a hash table of size 11. a) For resolving collisions, use double hashing with the following two hash functions h1(x) = x mod 11, and h2(x) = 5 – (x mod 5) where x is the key value. b) For resolving collisions, use quadratic probing and analyze the average cost of an unsuccessful search for the resulting hash table after insertions are over. Question 2 (80 Points) – Programming Assignment Way back when, in the time before electricity, all long distance communication was done with messengers carrying messages on horses. You are the postmaster for the country of “Farawayia” in these times. Your Queen gives you a big chest of gold and asks you to build a messenger system that will allow her people to send messages from any city in the country to any other city. If you can get it done cheaply, you can keep the remainder of the gold. But if it doesn’t work, you lose your head! So, you set out to hire messengers and buy horses. Messengers are easy to find and they are mostly willing to work for tips. So, you quickly find out that horses will be the most important cost factor. There are many horse breeders in the country; each sells their own breed. Some breeds are cheap but can only go a short distance before needing rest and food. Others breeds are very expensive but can run long distances before resting or feeding. You can only rest/feed horses at cities and you have to buy all your horses from one breeder (other breeders will not talk to you once you buy from one). So you realize that you need to find the longest distance a horse will have to run before reaching a city where it can be fed and rested, and buy the cheapest breed that can manage that distance. The messages do not need to go directly. Messages from city A to city B can be relayed through other cities if it makes things easier. So you find the Queen’s mapmaker and he gives you the list of roads in the country. You now need to find the maximum distance a horse will need to run at a time, using this list. Write a C++ program to solve this problem (Farawayia has hamster ­powered computers ). The list of roads is given as a text file with the following format: 4 1 1 1 1 2 3 6 2 2 4 3 4 4 10 13 4 5 8 15 The first line1 of the file has two numbers separated by whitespace2, giving the number of cities and roads, respectively. After that, each subsequent line has the IDs of two cities as integers followed by the length of the road between them as an integer, again separated by whitespace3. Note that: • There may be more than one road between two cities. • The roads are two ­way, and are the same distance in both directions. • The road network given by the file is guaranteed to be connected; i.e. any city will be reachable from any other city, even if not directly. In this instance, the example file tells us that there are 4 cities with 6 roads between them. The first road is between city #1 and city #2 and is 10 km long. The second road is between the same two cities and is 13 km long. The third road is between city #1 and city #4 and is 4 km long, &c. Your program should load the file and output the longest distance a horse would need to run without stopping. In the case of the file above, the answer is 8 km. Since we can send messages between cities #1 and #2 through city #4 and messages between cities #3 and #4 through city #1, we don’t need to use any roads longer than 8 km. So the horses will not need to run more than 8km without rest. The program should get the name of the input file as a command line argument and simply return the correct number. So, a sample run for the example file above would be something like: [email protected]:~>./hw5 example.txt 8 [email protected]:~> There is no hard limit to the number of cities/roads the input file may have. Your program should be able to work with rather large test sets (up to tens of thousands of roads/cities) and solve the problem efficiently (within seconds or at most a couple of minutes on dijkstra). The algorithm and structures you use are up to you. 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 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 can not get your program to properly work on the dijkstra machine, you will 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. • 1 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. Pay attention to these instructions, otherwise you may lose some points even though your code has no error. Lines are terminated by a single line feed (\n  ­ ASCII 0Ah) character. Whitespace is defined as one or more space (  ­ ASCII 09h) or tab (\t  ­ ASCII 20h) characters. 3 You can assume that the file will be correct and only include integers. 2 ...
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