CSE373 Fall 2013 Example Exam Questions On Dijkstra's Algorithm (And One On Amortized Analysis)
CSE373 Fall 2013 Example Exam Questions On Dijkstra's Algorithm (And One On Amortized Analysis)
Step through Dijkstra’s algorithm to calculate the single-source shortest paths from A to every other
vertex. Show your steps in the table below. Cross out old values and write in new ones, from left to
right within each cell, as the algorithm proceeds. Also list the vertices in the order which you marked
them known. Finally, indicate the lowest-cast path from node A to node F.
Solution:
Known vertices (in order marked known): A B C G E D or F D or F
A Y 0
B Y 1 A
C Y 32 AB
D Y 87 BE
E Y 65 BC
F Y 10 7 AE
G Y 3 B
(a) Even though the graph has negative weight edges, step through Dijkstra’s algorithm to calculate
supposedly shortest paths from A to every other vertex. Show your steps in the table below.
Cross out old values and write in new ones, from left to right within each cell, as the algorithm
proceeds. Also list the vertices in the order which you marked them known.
Solution:
Known vertices (in order marked known): A B D F C G E
(b) Dijkstra’s algorithm found the wrong path to some of the vertices. For just the vertices where
the wrong path was computed, indicate both the path that was computed and the correct path.
(c) What single edge could be removed from the graph such that Dijkstra’s algorithm would happen
to compute correct answers for all vertices in the remaining graph?
Solution:
3. Suppose we define a different kind of graph where we have weights on the vertices and not the edges.
Does the shortest-paths problem make sense for this kind of graph? If so, give a precise and formal
description of the problem. If not, explain why not. Note we are not asking for an algorithm, just what
the problem is or that it makes no sense.
Solution:
Yes, this problem makes sense: Given a starting vertex v find the lowest-cost path from v to every
other vertex. The cost of a path is the sum of the weights of the vertices on the path.
Name:
4. Consider using a simple linked list as a dictionary. Assume the client will never provide duplicate
elements, so we can just insert elements at the beginning of the list. Now assume the peculiar situation
that the client may perform any number of insert operations but will only ever perform at most one
lookup operation.
(a) What is the worst-case running-time of the operations performed on this data structure under the
assumptions above? Briefly justify your answer.
(b) What is the worst-case amortized running-time of the operations performed on this data structure
under the assumptions above? Briefly justify your answer.
Solution:
(a) inserts are O(1) (push on the front of a linked list), but the lookup is O(n) where n is the number
of inserted items since the lookup may be last and be for one of the earliest inserted items
(b) amortized all operations are now O(1). Inserts are still O(1). And the lookup can take at most
time O(n) where n is the number of previously inserted items. So the total cost of any n operations
is at most O(n), which is amortized O(1).