Assignment 12 -- Due Wednesday, April 25
(at the beginning of lab)
CS 4521 Spring Semester, 2007
25 Points
Topics: Minimum Spanning Trees and Single Source Shortest Paths
The assignment
consists of two parts.
In the first part
you will hand-trace the algorithms of Kruskal and Prim to find
minimum spanning trees.
In the second part, you will implement Dijkstra's
Algorithm to solve the single-source shortest paths problem.
Part I: Minimum Spanning Tree Exercises (10 points)
Consider the following undirected graph:
5
(B)----------(D)
|\ / | \
| \10 20/ | \
| \ / | \
| \ / 12| |
3| \ / | |
| (A)----(F) |11
| 2 \ |
| 4\ |
| \|
(C)--------------(E)
15
- Trace the MST-Kruskal algorithm on this graph. Start by drawing the
vertices only:
(B) (D)
(A) (F)
(C) (E)
and show how the MST is grown by showing a snapshot of it after each edge is
added.
- Trace the MST-Prim algorithm on this graph starting with root
C
by showing the priority queue
after each iteration of the while loop. Also show the key
value for each node. The initial queue should look like:
0 inf inf inf inf inf
Q: ( C A B D E F )
Part II: Implementation of Dijkstra's Algorithm (15 points)
For this part, implement
Dijkstra's algorithm (page 595),
and run it on two graphs.
As discussed on page 599,
the min-priority queue used in Dijkstra's algorithm may be implemented in
four ways (the key values will always be the distance estimate
d[]):
- (15 points) an array implementation, in which the queue is
maintained in
d[] and an auxiliary array
inQ[]) of booleans that indicates whether vertex
u) is currently in the queue or not
(inQ[u] would be true or false respectively).
I have implemented this version.
To initialize the queue (Line 3), I simply set
inQ[u] to true for u = 1 to n.
I changed the while-loop to a for-loop and
iterated n times.
I made the extractMin() and relax()
functions (and dijkstra() for that matter) member functions
of the Graph class. Their implementations were just as I
presented in the lab session.
In particular, for extractMin(), just do a linear search
for the location of the minimum d[] value that is still
in inQ[u].
- (17 points) the binary heap or (19 points) binomial heap
implementations.
In these implementations, the vertices and corresponding heap elements must
maintain
handles to each other (though, since the vertices don't change position,
the handle to a vertex (stored with its key) in the heap doesn't change
once it is set initially;
however the handles from the vertices into the heap will change --
they can be stored in an array
handleToHeap[], so handleToHeap[u]
points to the node in the heap with d's key value.
These handles would be integer indexes or pointers for binary heaps
or binomial heaps respectively.
- (25 points) the Fibonacci heap implementation. The main
problem here is to implement a Fibonacci heap. You have to have the
same kind of handles for the Fibonacci heap implementation as for the
binomial heap implementation. But there is a subtle difference: once
initialized, neither of the handles change, so this implementation is
somewhat easier (once you have a Fibonacci heap).
Discussion:
Use numbers 1, 2, 3, 4, 5 instead of the letters
s, t, x, y, z to identify the vertices.
Also, the set S
is only used to prove correctness of the
algorithm -- so you don't have to include code for it.
You can use a large number, say 1000
(I think 1 + the sum of the weights of all the edges is
enough), instead of infinity in the initialization
(I don't think there will be any arithmetic problems caused by using
1000 instead of INT_MAX or other fancy arithmetic).
Also, you can use 0 for NIL, the intialization value for the
pi field.
At the end of each iteration of
the
while loop of Dijkstra's algorithm,
your program should print out a list of all vertices
and their d-values. Also, it should print the
final d-values and predecessor values
(pi-values)
of all the vertices just before terminating.
Run this implementation on four different (G,w,source)
combinations:
- The graph and weights G,w of Figure 24.2 page 585,
with source s (you may get one of the solutions
shown in (b) or (c) of Figure 24.2, or something different):
graph 24.2, source s.
- The graph and weights G,w of Figure 24.2 page 585,
but with source z:
graph 24.2, source z.
- The graph and weights G,w of Figure 24.6 page 596,
with source s (you may very well get the solution
of Figure 24.6, or something different):
graph 24.6, source s.
- The graph and weights G,w of Figure 24.6 page 596,
but with source z:
graph 24.6, source z.
Also
for each case, after the program run,
draw
the shortest-paths tree given by the predecessor graph.
The data files have the number of vertices, n, and
the source, source on the first line.
Each subsequent line contains three values
u
v, and
w,
representing an edge,
where the edge goes from vertex u to vertex v
and has weight w.
What to turn in:
- For Part 1, turn in your handwritten solutions to both parts.
- For Part 2, turn in:
- your (annotated) code,
- printouts from each of the four program runs, and
- diagrams of the final shortest-paths tree for each of those runs.
Page URL: http://www.d.umn.edu
/~ddunham/cs4521s07/assignments/a12/assignment.html
Page Author: Doug Dunham
Last Modified: Wednesday, 18-Apr-2007 12:36:08 CDT
Comments to: ddunham@d.umn.edu