
Model Solutions
COMP202 Class Exam 2
Complexity of Algorithms 29 April 2009
1. (a) Here’s the table format that we talked about in class, where we fill
in the top four rows from left-ro-right, and then having done that,
we find the last two rows for jand kfrom right-to-left:
a306 264 42 12 6
b264 42 12 6 0
q1 6 3 2 -
r42 12 6 0 -
j19 −3 1 0 1
k−22 19 −310
This tells us that the gcd is d= 6.
(b) From above, we see that 6 = (19) ·(306) + (−22) ·(264) (as can also
be easily checked).
Hence j= 19 and k=−22 are the relevant jand k.
2. We can use Dijkstra’s algorithm to find shortest paths for this graph. We
start with the distance from Ato itself to be 0, and distances to neighbors
of Aset to the edge lengths. At each step after that, a vertex having
smallest temporary distance label becomes “fixed”, and we update (non-
fixed) vertex distances that are neighbors of the newly fixed vertex.
The entries in the table are the distance labels (temporary or permanent)
and the predecessor vertex so that we can reconstruct the shortest paths
by tracing backwards through the table at the end of the algorithm.
Fixed A B C D E F G H J K
A0,∅ ∞ ∞ 4, A 2, A ∞ ∞ 2, A 4, A ∞
E0,∅ ∞ 5, E 3, E 2, A 7, E ∞2, A 4, A ∞
H0,∅ ∞ 5, E 3, E 2, A 7, E ∞2, A 2, H 6, K
J0,∅ ∞ 5, E 3, E 2, A 7, E ∞2, A 2, H 5, J
D0,∅6, D 5, E 3, E 2, A 4, D ∞2,A 2, H 5, J
F0,∅6, D 5, E 3, E 2, A 4, D ∞2,A 2, H 5, J
K0,∅6, D 5, E 3, E 2, A 4, D ∞2,A 2, H 5, J
C0,∅6, D 5, E 3, E 2, A 4, D 5, C 2, A 2, H 5, J
G0,∅6, D 5, E 3, E 2, A 4, D 5, C 2, A 2, H 5, J
B0,∅6, D 5, E 3, E 2, A 4, D 5, C 2, A 2, H 5, J
3. (a) To encrypt the message M= 100, we compute n=p·q= 221 and
then we compute
C= 1005mod 221.
1