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
(B) (D)
(A) (F)
(C) (E)
and show how the MST is grown by showing a snapshot of it after each edge is
added.
0 inf inf inf inf inf
Q: ( C A B D E F )
Note that 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.
Note that it isn't strictly necessary to maintain the array d[] separately, since the value d[u] may be obtained as key[handleToHeap[u]] (but you might want to maintain d[] anyway). (Of course in the "array implementation", you will only have d[] -- and no handles to worry about).
You can 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 -- you don't have to include code for it if you don't need it
(but it does seem necessary for the "array implementation").
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: