HW5 Handout

Transcription

HW5 Handout
CS305 Programming Assignment #5: “Seg Fault Hunting on Graphs”
Out:
April. 7, 2015.
Due:
April 21, 2015 by 11:00 pm on the CS305 Moodle site.
Total points:
100. approximately 20% of the total homework grade.
Lectures:
Graphs and Adjacency Lists were discussed in Lectures 20 and 21.
Dijkstra’s algorithm was discussed in Lecture 23.
This homework assignment has starter code. Go to the course website and download it now.
This final homework assignment in CS305 is designed to give students practice with a C implementation
of graphs as adjacency lists and Dijkstra’s algorithm.
!
Context
The state of Oregon comprises approximately 250 cities, from Portland to Greenhorn. The undirected
graph below summarizes thirteen of Oregon’s cities and the distances (in miles) between them along
existing highways and interstates in the region. For example, Astoria is 95 miles from Portland via
highway 30. Similarly, Ontario is 75 miles from Baker City along I-84.
The graph depicted above has been implemented in the collection of files main.c, main.h, graph.c, graph.h
and makefile provided in the starter code. The graph is implemented as a undirected graph. That said,
given the nature of the above data, one can also think of it as a directed graph, with an edge in each
direction.
!
Date: 4/5/2015
1
This homework assignment requires that students implement Dijkstra’s algorithm. The algorithm,
presented on 4/7 in class, is listed here:
!
DIJKSTRA(G, weight, s)
1
INITIALIZE SINGLE SOURCE(G, s)
2
Set := empty
3
for each vertex v in V[G]
4
do put(v, Q)
5
while !isEmpty(Q)
6
do u := get-min(Q)
7
put(S, u)
8
for each vertex v in Adj[u]
9
do RELAX(u, v, weight)
!
To implement Dijkstra’s algorithm, students are encouraged to alter the basic adjacency list representation
of the graph, as implemented in graph.c and graph.h.
!
Draw your picture of the adjacency list representation of the graph here. Students who come to office
hours for help will be asked for this picture.
!!
!!
!!
!!
!!
!!
!!
!!
!!
!!
!!
!!
!!
!
!
Date: 4/5/2015
2
!
No function prototype is provided for the implementation of dijkstra(); students may choose to define the
function prototype however they wish. An incomplete comment header is provided in main.c. Students
should implement dijkstra() there:
!
/*
* function: dijkstra()
*
* CS305 Students.
* here.
Place your implementation of dijkstra's algorithm
Thank you.
*/
!
In addition, students should call their dijkstra() function in the place indicated in main.c:
!
// ************************************************************************
// PHASE 4: Find the shortest paths and print the result.
printf(" * Student add your call to dijkstra() here.\n");
printf(" * Be sure to print the shortest path and cost \n");
printf(" * between %s and %s. See HW5 handout. \n", argv[SRC], argv[DST]);
!
!
Test Cases
Up to 80 points will be awarded for the correct execution of the following test cases.
!1. The program gracefully informs the user of the program’s usage if the wrong number of
command line arguments are provided (10 points).
!
$ ./hw5
usage: ./hw5 <source city name> <dest. city name>
This program finds the shortest path from the given source city to
destination.
!and
!
$ ./hw5 Portland to Burns
usage: ./hw5 <source city name> <dest. city name>1
This program finds the shortest path from the given source city to
destination.
!!
2. The program gracefully handles when a city not in the HW5 map is requested (10 points).
!
!!
$ ./hw5 Boise Portland
Boise is not in the map.
Date: 4/5/2015
Please see your HW5 handout for map details.
3
!3. The program has default behavior when a city in the map is given at the command line (20
! points).
!
!
!
!
!
!
!
!
!
!
!
!
!
!
$ ./hw5 Portland Astoria
Creating a graph for 13 cities.
Adjacency list for city #0: Astoria
-> Seaside -> Portland
Adjacency list for city #1: BakerCity
-> Pendleton -> Ontario
Adjacency list for city #2: Bend
-> Burns -> TheDalles -> BlueRiver
Adjacency list for city #3: BlueRiver
-> Bend -> Eugene
Adjacency list for city #4: Burns
-> Pendleton -> Ontario -> Bend
Adjacency list for city #5: Eugene
-> BlueRiver -> Salem -> Florence
Adjacency list for city #6: Florence
-> Eugene -> Seaside
Adjacency list for city #7: Ontario
-> BakerCity -> Burns
Adjacency list for city #8: Pendleton
-> TheDalles -> Burns -> BakerCity
Adjacency list for city #9: Portland
-> Astoria -> TheDalles -> Salem
Adjacency list for city #10: Salem
-> Eugene -> Portland
Adjacency list for city #11: Seaside
-> Florence -> Astoria
Adjacency list for city #12: TheDalles
-> Pendleton -> Bend -> Portland
-----------------------------* Student add your call to dijkstra() here.
* Be sure to print the shortest path and cost
* between Portland and Astoria.
-----------------------------Freed all memory. Thank you!
Date: 4/5/2015
4
4. The program implements Dijkstra’s algorithm for the city provided at the command line if the
city is in the map (40 points). Note in the test cases below, the default output from test case 3 is
omitted for brevity.
!
a
$ ./hw5 Portland Astoria
* -> Astoria.
b
c
!
95 miles.
$ ./hw5 Portland BakerCity
* -> TheDalles -> Pendleton -> BakerCity. 304 miles.
!
$ ./hw5 Astoria Ontario
* -> Portland -> TheDalles -> Pendleton -> BakerCity -> Ontario. 474
miles.
d
!
$ ./hw5 Bend Bend
0 miles.
and so forth.
!To receive 100/100 points on this assignment, you must utilize good programming practice (20
points):
• Submit your seg-fault free source organized into these files: main.c, main.h, graph.h, graph.c, and
makefile.
• Submit your assignment in a zipfile with your username in the zipfile name.
• Always give variables meaningful names. Never use global variables.
• Use #define preprocessor directives for constant values; you may not necessarily need constant values
for your solution.
• Avoid redundant code.
• Document code with useful comments. See example program on website for five pieces of guidance
on how to adequately document code with comments.
!To achieve maximum points on your submission, consider using this submission checklist before
submitting your program to the Moodle course website:
!
___ “The name of my program files are: main.c, main.h, graph.h, graph.c, and makefile.”
___ “My program compiles successfully using a make command with my makefile.”
___ “My program executes without any seg faults.”
___ “I followed the five pieces of guidance on commenting programs.”
___ “I compressed my files into a zipfile named with my username, e.g., crenshaw16.zip”
___ “I did not compress my files using .rar, .z7, or some other proprietary compression program.”
___ “I did not compress a DIRECTORY of files.”
___ “I uploaded my zipfile to Moodle.”
!
If you are missing any of the above checklist items,
up to 10 points may be deducted from your score.
Date: 4/5/2015
5