CMPS256 - Exam Review: Graph Algorithms and Minimum Spanning Trees, Exams of Algorithms and Programming

Aub programming algorithms and data structures finals review questions and solutions

Typology: Exams

2021/2022

Uploaded on 05/14/2023

unknown user
unknown user 🇱🇧

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPS256 - Exam Review
Let G be a connected weighted undirected graph. Suppose that the maximum weight in G is m, and there is
only one edge, e, that has that weight. For example, G might look like this, where m = 5 and e = {d, e}:
a
b
c
d
e
2
23
1
3
5
Prove the following statement:
If there is some spanning tree that does not contain the edge e, then no Minimum Spanning Tree can
contain e.
[We are expecting: A formal proof. ]
Let Tbe a spanning tree that contains the edge e. We want to show that Tis not an MST. Let Tbe
the spanning tree that does not contain the edge e, as guaranteed by the problem statement.
If we remove efrom T, we are left with two trees, call them T1and T2, and consider the cut formed
by the vertices of T1and the vertices of T2.
Since Tis a spanning tree, it must cross this cut. Say that {u, v}is the edge in Tthat crosses this
cut. Since ehas the unique max weight, the weight of {u,v }must be smaller. Consider the spanning
tree that you get by removing efrom Tand adding in {u,v }to T. This still spans, b ecause we haven’t
changed the number of vertices it touches, and it is still a tree since it spans and has only n1 edges.
But now the cost is smaller than that of T, so Tcannot have been an MST.
1
pf3
pf4
pf5

Partial preview of the text

Download CMPS256 - Exam Review: Graph Algorithms and Minimum Spanning Trees and more Exams Algorithms and Programming in PDF only on Docsity!

CMPS256 - Exam Review

Let G be a connected weighted undirected graph. Suppose that the maximum weight in G is m, and there is only one edge, e∗, that has that weight. For example, G might look like this, where m = 5 and e∗^ = {d, e}:

a

b

c

d

e

Prove the following statement:

If there is some spanning tree that does not contain the edge e∗, then no Minimum Spanning Tree can contain e∗.

[We are expecting: A formal proof. ]

Let T be a spanning tree that contains the edge e∗. We want to show that T is not an MST. Let T ∗^ be the spanning tree that does not contain the edge e∗, as guaranteed by the problem statement. If we remove e∗^ from T , we are left with two trees, call them T 1 and T 2 , and consider the cut formed by the vertices of T 1 and the vertices of T 2. Since T ∗^ is a spanning tree, it must cross this cut. Say that {u, v} is the edge in T ∗^ that crosses this cut. Since e∗^ has the unique max weight, the weight of {u, v} must be smaller. Consider the spanning tree that you get by removing e∗^ from T and adding in {u, v} to T. This still spans, because we haven’t changed the number of vertices it touches, and it is still a tree since it spans and has only n − 1 edges. But now the cost is smaller than that of T , so T cannot have been an MST.

Consider the graph G below.

A B C

D E F

  1. In what order does Prim’s algorithm add edges to an MST when started from vertex C?
  2. In what order does Kruskal’s algorithm add edges to an MST?

[We are expecting: For both, just a list of edges. You do not need to draw the MST, and no justification is required. ]

  1. Prim’s algorithm adds edges in the order: {C,F}, {F, E}, {E, D}, {A, D}, {A, B}
  2. Kruskal’s algorithm returns the same tree, and adds edges in the order: {A,D}, {A, B}, {D, E}, {F, C}, {E,F}

You join a nomadic tribe traveling through their fixed route of n ancient queendoms, going through queen- doms in increasing order. This is a cool adventure, but also a nice opportunity to make money! In each queendom, you can sell any foreign currency and receive the local currency in exchange (but no other ex- changes are allowed). You have an n × n table of the exchange rates between every pair of currencies. You start the journey with 1 unit of currency of the first queendom. Your goal is to buy and sell during the trip in order to maximize the money you’ll have in the n-th queendom’s currency, since you plan to stay there for a while. There is one more restriction: in order to not attract too much attention to your side business, you should only exchange money in k queendoms. Design an algorithm to determine the maximum of the n-th queendom’s currency you can have. Input: n: the number of queendoms; k: the number of exchanges allowed; A: an n × n table of exchange rates. Output: the number of units of the n-th queendom’s currency

Additional assumptions: You may assume that k is much smaller than n, but not as small as O(1). Also, you do not exchange in the first queendom (since you already have that currency), and you always exchange in the last queendom. Example 1 In this example, the optimal strategy is to exchange in both the second and third queendom, and end up with 2 × 2 = 4 units of the last currency (exhanging only in the last one would give 2. 5 ). Note: In the example A[i][j] = c means that exchanging 1 unit of currency i yields c units of currency j. I n p u t : n = 3 k = 2 A = [ [ 1. 0 2. 0 2. 5 ] [ 0. 5 1. 0 2. 0 ] [ 0. 4 0. 5 1. 0 ] ] Output : 4

Example 2 In this example, the optimal strategy is to exchange only in the third queendom, and end up with 2.5 units of the last currency (exhanging in both second and third one would give 0. 5 × 2 = 1). I n p u t : n = 3 k = 2 A = [ [ 1. 0 0. 5 2. 5 ] [ 2. 0 1. 0 2. 0 ] [ 0. 4 0. 5 1. 0 ] ] Output :

  1. 5

Give a short but clear English description of your algorithm.

[We are expecting: A clear yet thorough English description of your algorithm.]

We maintain a n × k one-indexed array M , in which entry M [i][j] gives the optimal amount of currency i that can be obtained after j exchanges and output M [n][k]. To compute each entry M [i][j] where j = 1, directly convert currency i to currency j. For remaining entries M [i][j], take the maximum of the product of the exchange rate and the currency held for j − 1 exchanges among the set of previous queendoms.

Is your algorithm correct (Yes/No)?

[We are expecting: A clear answer of YES or NO. If YES, no explanation is necessary, if NO, explain why your algorithm is incorrect.]

Yes

Provide the pseudocode for your algorithm.

[We are expecting: Detailed pseudocode that matches your English description. You are free to use an interface of any of the algorithms covered in lecture.]

def exchangeCurrency(n, k, A): M = an n by k array for i in 1..n: M[i][1] = A[1][i] for j in 1..k: M[1][j] = 1 for all remaining (i, j): M[i][j] = max of M[i’][j-1] * A[i’][i] for all i’ < i return M[n][k]

Provide an analysis of the runtime of your algorithm.

[We are expecting: A detailed analysis of the runtime of your algorithm including the Big-O time in terms of n and k.]

There are O(nk) entires in M. To compute each entry, we must check O(n) prior entries for an overall runtime of O(n^2 k).