Docsity

Weighted Graph, Euclid's Algorithm-Algorithm Design and Data Representation-Exam Paper Solution, Exams of Data Representation and Algorithm Design

Toggle sidebar
bg1
Model Solutions
COMP202 Class Exam 2
Complexity of Algorithms 28 March 2007
[NB: This examination contributes 10% to your overall final course grade.]
Answer all questions!
1. Suppose that we use Kruskal’s algorithm, considering edges in order of
increasing weight. Then we will add the edges to the MST in the following
order: First AB, then DH , and then (in some order) CD and E H. Then
we take edge FG, followed by one of CF and DG. Finally, we take one of
AC and BD. This gives a connected graph, hence we’re done.
Because of the choices, there are four possible minimum spanning trees.
Of course each has the same minimum weight of 10. One example (taking
edges DG and AC) is shown below.
2. (a) The main observation used in Euclid’s algorithm is that
gcd(a, b) = gcd(b, a mod b),provided b6= 0.
(b) a 224 63 35 28 7
b 63 35 28 7 0
Hence, gcd(224,63) = 7.
1
docsity.com
pf3

Partial preview of the text

Download Weighted Graph, Euclid's Algorithm-Algorithm Design and Data Representation-Exam Paper Solution and more Exams Data Representation and Algorithm Design in PDF only on Docsity!

Model Solutions

COMP202 Class Exam 2 Complexity of Algorithms 28 March 2007

[NB: This examination contributes 10% to your overall final course grade.]

Answer all questions!

  1. Suppose that we use Kruskal’s algorithm, considering edges in order of increasing weight. Then we will add the edges to the MST in the following order: First AB, then DH, and then (in some order) CD and EH. Then we take edge F G, followed by one of CF and DG. Finally, we take one of AC and BD. This gives a connected graph, hence we’re done. Because of the choices, there are four possible minimum spanning trees. Of course each has the same minimum weight of 10. One example (taking edges DG and AC) is shown below.
  2. (a) The main observation used in Euclid’s algorithm is that

gcd(a, b) = gcd(b, a mod b), provided b 6 = 0.

(b) a 224 63 35 28 7 b 63 35 28 7 0 Hence, gcd(224, 63) = 7.

  1. We find the value index, vi, of each item, defined as benef itweight.

item a b c d e f weight 2 1 3 6 4 5 benefit 3 4 5 18 10 6 vi (^32 4 53 3 2 )

Then we select items in (decreasing) order of their value indices until we fill up the knapsack (including, possibly, some fractional amount of an item during the last step). Also note that 2 12 > 53 > 32 > 65. Hence, the order in which we consider the items is b, d, e, c, a, f. Items b, d, and e together have a total weight of 11 and a total benefit of

  1. Adding item c brings the total weight up to 14 and benefit up to 37. Then we can add one-half of item a to fill up the knapsack, giving a total benefit of 38 12. To summarize, we take items b, d, e, c, and 12 a in our knapsack, giving us a total benefit of 38 12.
  2. D and G are not in the same strongly connected component. We can note that all of the edges AE, BK, BF , and JD are directed from left to right. Hence there is no way to “get back” to the vertices A, B, G, and J once we cross one of the four edges mentioned. Thus, there is no directed path from vertex D to vertex G. On the other hand H and I are in the same strongly connected component. This is demonstrated by the directed paths

H → K → C → I and

I → E → K → C → H.

  1. Here we use Dijkstra’s algorithm to do this. I’ll just give a table that illustrates the intermediate steps in the algorithm. The first line is the initialization step (where all distances are set to have the value of infinity (or some large number)), and then each line represents one full step in Dijkstra’s algorithm. Obviously as we’re looking for shortest paths from vertex A, this is the first vertex that becomes fixed (with distance 0). The table that you may have constructed could have the vertex distances being fixed in a different order than mine, but (of course) the distances of the shortest paths will be the same. I have circled the distance of the vertex during the step in which it becomes fixed.