CS 4521 Fall Semester, 2009

25 Points

**
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.

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
by showing the priority queue after each iteration of the**C**loop. Also show the key value for each node. The initial queue should look like:`while`0 inf inf inf inf inf Q: ( C A B D E F )

- (15 points) an
**array implementation**, in which the queue is maintained by thearray and an auxiliary array**key[]**of booleans that indicates whether vertex**inQ[]**is currently in the queue or not (**u**would be true or false respectively). To initialize the queue (Line 3), for**inQ[u]**= 1 to**u**insert**n**into the priority queue. I changed the**d[u]**-loop to a**while**-loop and iterated**for**times (though it is simple enough to keep count of the number of items in the queue and quitting the while-loop when this number drops to 0). Also, it is sufficient to iterate**n**as indicated by Exercise 24.3-3 (whose short answer is "yes"). The**n-1**and**initSingleSource()**functions (and**relax()**for that matter) can be member functions of the**dijkstra()**class. To keep**Graph**and**d[v]**"synchronized", Line 2 of**key[v]**should involve two operations:**relax()**

**then**(1)`d[v] <- d[u] + w(u,v)`and (2)`DecreaseKey( Q, v, d[u] + w(u,v) )`

(remember that this is a decrease-key operation). To implement, just do a linear search for the location of the minimum**extractMin()**value that is still in**key[]**. Note that a vertex**inQ[]**is an array index and therefore a handle into the priority queue, and conversely an array index**u**of**i**or**key[]**is a handle to vertex**inQ[]**of the graph - moreover, neither of these handles ever change.**i** - (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, so**handleToHeap[]**points to the node in the heap with**handleToHeap[u]**'s key value. These handles would be integer indexes or pointers for binary heaps or binomial heaps respectively.**d** - (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

After initialization and at the end of each iteration of
the
` while` loop
of Dijkstra's algorithm,
your program should print out a list of all vertices,
their

- The graph and weights
of Figure 24.2 page 585, with source**G,w**( = 1 ) - you may get one of the solutions shown in (b) or (c) of Figure 24.2, or something different:**s**This is essentially Exercise 24.3-1 on page 600.`graph 24.2, source s.` - The graph and weights
of Figure 24.2 page 585, but with source**G,w**( = 5 ):**z**`graph 24.2, source z.` - The graph and weights
of Figure 24.6 page 596, with source**G,w**( = 1 ) - you may very well get the solution of Figure 24.6, or something different:**s**`graph 24.6, source s.` - The graph and weights
of Figure 24.6 page 596, but with source**G,w**( = 5 ):**z**`graph 24.6, source z.`

Here is what the output from the first test should look like:

Built graph, n = 5 Run Dijkstra with source = 1 Iteration 1, Distances, d[1] = 0, d[2] = 3, d[3] = 1000, d[4] = 5, d[5] = 1000 Iteration 2, Distances, d[1] = 0, d[2] = 3, d[3] = 9, d[4] = 5, d[5] = 1000 Iteration 3, Distances, d[1] = 0, d[2] = 3, d[3] = 9, d[4] = 5, d[5] = 11 Iteration 4, Distances, d[1] = 0, d[2] = 3, d[3] = 9, d[4] = 5, d[5] = 11 Iteration 5, Distances, d[1] = 0, d[2] = 3, d[3] = 9, d[4] = 5, d[5] = 11 Final distances and predecessors: Distances, d[1] = 0, d[2] = 3, d[3] = 9, d[4] = 5, d[5] = 11 Predecessors, pi[1] = 0, pi[2] = 1, pi[3] = 2, pi[4] = 1, pi[5] = 4This corresponds to Figure 24.2 (b) on page 585. It is possible that changing the order of the adjacency lists could produce the shortest paths tree of Figure 24.2 (c).

Figure 24.6 (page 596) shows how the third test run should proceed.

** Helpful code: **
Here is a .h file for a new Graph class
** graph.h**
and skeleton code for the Graph class implementation

**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.