



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A programming assignment that requires students to implement a priority queue using a heap and dijkstra's shortest path algorithm. The assignment includes instructions for creating a heap test program and an optional shortest path finder. Students are expected to read input from a file, convert it into a heap or adjacency list, and process standard input commands to dequeue or enqueue cities or find the shortest path between two cities.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Goal: The purpose of this assignment is to get practice working with heaps and adjacency lists.
Objectives: be able to write code to implement a priority queue using a heap; be able to write code to implement a edge-labeled undirected graph using adjacency lists; be able to implement Dijkstra’s Shortest Path Algorithm.
This assignment provides the specification of two programs, the first exercise-heap is required the second path-finder is optional for extra credit.
The Heap Test Program (exercise-heap) will read in a sequence of pairs of city names and distances from a file into an array. Then change the array into a minimum-heap priority queue. Then, based on the standard input, be able to dequeue and print out the closest city, and be able to enqueue additional city-distance pairs.
For extra credit, you may write a path-finder program, reads in a list of distances between pairs of cities, puts those into a graph data structure, is then able to find the shortest path between any two cities (specified on the standard input), and shortest path and its distance.
The program should work with input and produce output exactly as described here. (Automated tools that do a character by character comparison may be used for grading.) For inputs that do not comply with this specification, your program may print an error message and exit; there should not be any inputs which would cause your program to have undefined behavior.
exercise-heap command line. The program executable should expect as a command-line argu- ment the name of a file containing pairs of cities and their distance. Example:
./ exercise - heap à cities. txt
data file. The file specified on the command-line (e.g., cities.txt) will contain lines of up to 80 non-null characters. (If there are more than 80 -characters—not counting '\n'—on a line, the additional characters should be silently discarded, producing the same result as if all of the lines were truncated to 80 characters.)
Each pair of lines contains the name of a city and a number which represent the distance of the city from some particular point). Lower numbers represent higher-priority. For example:
College à Station 201 Houston 210 Dallas 278 El à Paso 539
Standard Input. On the standard input the program can expect to receive lines denoting enqueue or dequeue commands. Dequeue commands consist solely of the characters dequeue, and en- queue commands consist of the word enqueue, a tab, the name of a city, a tab, and a distance. For example:
dequeue enqueue−→Austin −→ 83 dequeue dequeue
Standard Output For each dequeue, command, the exercise-heap program should remove and printout the name and distance (separated by a tab) of the city in the priority queue with the lowest associated distance. So for the inputs above, the program should output:
College à Station−→ 201 Austin −→ 83 Houston−→ 210
The program should work with input and produce output exactly as described here. (Automated tools that do a character by character comparison may be used for grading.) For inputs that do not comply with this specification, your program may print an error message and exit; there should not be any inputs which would cause your program to have undefined behavior.
path-finder command line. The program executable should expect as command-line arguments ( 1 ) the name of a file containing pairs of cities and their distance and ( 2 ) the name of a city you are interested in finding distances from. Example:
./ path - finder à cities - pairs. txt à " El à Paso "
data file. The file specified on the command-line (e.g., city-pairs.txt) will contain lines of up to 80 non-null characters. (If there are more than 80 -characters—not counting '\n'—on a line, the additional characters should be silently discarded, producing the same result as if all of the lines were truncated to 80 characters.)
Each group of three lines contains the name of one city, the name of a second city, and a number which represents the distance between those city. Lower numbers represent higher-priority. For example:
The exercise-heap program should store the information about cities/distances in a minimum heap stored in an array and keyed on the distance number. (Note: since this is a minimum heap, the smallest rather than the biggest number should be at the root.) For efficiency, the data from the cities.txt should be read in in order and then transformed into a heap. To ‘heapify’ the input from cities.txt, traverse the array backwards sifting down each element (see page 182 of the text book); this turns an n-element array into a heap in O(n) time. Once the information is converted into a heap, the standard input commands should be processed using the heap as a priority queue (where smallest distance means highest priority).^1
The path-finder should read the data stored in the city-pairs.txt file and place into into an adjacency list, and then use Dijkstra’s algorithm (see Section 7. 3 in the text book), to calculate the shortest path to each of the cities.
You may find it useful to number the cities as you encounter them in the input and maintain an AA Tree mapping names to index numbers. You can then use those index numbers as indexes into dynamic array lists, vertex numbers, as a value to store in the heap, etc. It should be possible to use the same heap implementation for both programs.
Provide a Makefile that produces an executable exercise-heap and path-finder with the proper dependencies on your source files.
Test your program thoroughly using valgrind and a variety of valid and invalid inputs.
Please create a README file divided into three sections: NOTES TO GRADER, EXTRA CREDIT (optional), PROGRAM VALIDATION, and CERTIFICATION.
Under NOTES TO GRADER, add any notes, you wish the TA/instructor to consider while grading. Please give a general overview of the state of your exercise-heap program. Describe the major data structures, and explain how the program is decomposed into files and functions. Describe any known problems with it program. If gcc produces any warning or error messages (with the switches: -std=c99 -pedantic -Wall -Wextra -Wstrict-prototypes -Wno-unused-parameter), or if valgrind reports any problem, please list them, describe what you think they mean, and say whether you think it indicates a problem in your code or if gcc/valgrind is just being ‘too picky.’ Please, also note approximately how many hours you spent to complete the assignment. This information will not effect your grade, but will be used for future course planning.
If you implemented the path-finder program (or attempted to), please add a section en- titled EXTRA CREDIT. In this section describe the current state of your path-finder pro- gram and how it is implemented. Include a description of any known defects.
Under PROGRAM VALIDATION, please describe what you did to test your program or otherwise check that it is behaving correctly. In particular: What inputs did you try? How did you determine if the outputs were correct? Did you try any invalid inputs and make sure that your program doesn’t crash? Did you use valgrind when running these tests? (See also the hints of Testing and Debugging given in the instructions to Assignment 5 .)
You might find it easiest, to initially skip the input file and heapify algorithm, and just do the enqueue/dequeue commands first, and then come back to heapify later.
Below this statement, please write your name and date.
Use svn to commit your final submission by 5 pm on the due date.
Permissible Collaboration and Resources. This assignment is to be completed individually. You are allowed to discuss general strategies for solving assignments with fellow students or other in- dividuals, but it is no longer a ‘general strategy’ if the discussion gets to the level of detail of what would be done in actual lines of code found in your programs. In addition, discussions of ‘general strategy’ should not be taking place while either party is editing their source code. (It is, however, perfectly acceptable to discuss C language constructs and concrete examples of them which are not taken directly from anyone’s solution to an assignment.) In addition, you may seek more specific help from mentors and lab tutors in trying to understand why their code doesn’t work. Furthermore, you may also seek help and guidance of any kind from the TA and instructor.
For this assignment, you may not consult any heap or Dijkstra’s shortest path code other than that provided by the instructor, TA, or in the text book.