Midterm1 Sol Essay

Submitted By 3lastic
Words: 1171
Pages: 5

CPSC 320 Midterm 1
Monday, October 18th, 2010
[13] 1. Short answers
[4] a. We frequently prove an O(n2 ) bound on the worst-case running time T (n) of an algorithm, without proving any Ω bound on T (n). However, we would never prove an Ω(n2 ) bound on T (n) without also proving a O bound on T (n). Why not?
Solution : Proving an Ω(n2 ) bound on T (n) simply means that the algorithm runs slowly. There is no point in giving an algorithm without providing an upper-bound on its running time (since then the algorithm might be arbitrarily bad).
[4] b. Why is Dijkstra’s algorithm greedy?
Solution : Because at each iteration of the for loop, it adds to the tree the vertex with the smallest cost at that point, without considering what that might mean for future iterations.
[5] c. Mr. Isulo, a famous alien computer scientist, has designed a greedy algorithm to solve the Clique problem (you don’t need to know what it is) on a type of graphs called circular arc graphs (you don’t need to know what they are either). His algorithm starts by choosing the vertex with the most neighbours.
Mr. Isulo wants to prove the following lemma: “Every circular arc graph has a maximum clique that contains the vertex with the most neighbours”, but he eventually finds a counter-example to his conjecture. What does this imply for Mr. Isulo’s algorithm, and why?
Solution : All of the solutions constructed by Mr. Isulo’s algorithm will contain the vertex with the most neighbours, since a greedy algorithm never removes the vertices it has chosen. Because the lemma is false, his algorithm will not return the correct answer for some graphs (that is, it doesn’t work).
[5] 2. Give an example of a function f : N → R+ that is not in O(n log n), but is not in ω(n log n) either. Justify your answer briefly.
Solution : Consider the function f : N → R+ defined by f (n) =

n n2 if n is even if n is odd

Because f (n) = n for every even value of n, f (n) is not in ω(n log n). And because f (n) = n2 for every odd value of n, f (n) is not in O(n log n) either.

page 2
[5] 3. A long string consists of the four characters A, C, G and T ; they appear with frequencies
31%, 20%, 9% and 40% respectively. What code would Huffman’s algorithm return for each of these characters (that is, list the sequence of bits that would be used to represent each character)? Justify your answer.
Solution :
Character
A
C
G
T

Code
01
000
001
1

[5] 4. Recall that a simple path from a vertex s of a graph G to another vertex v of G is a path from s to G that contains each vertex at most once. A student who was interested in finding the maximum cost simple path from a vertex s of G to every other vertex of G decided that he could achieve this by using Dijkstra’s algorithm, using a Max-Heap instead of a Min-Heap, and replacing the line if (Cost(v) > Cost(u) + cost(u,v)) then
Cost(v) ← Cost(u) + cost(u,v) by if (Cost(v) < Cost(u) + cost(u,v)) then
Cost(v) ← Cost(u) + cost(u,v)
Give an example of a graph where this student’s algorithm will fail. Show both the output from the modified version of Dijkstra’s algorithm, and a path from s to v that is longer than the path found by the algorithm.
Solution : Consider the following graph:

page 3
The modified version of Dijkstra’s algorithm will first select vertex u (with a cost of 3) and then vertex t (with a cost of 7). However the longest simple path from s to u is the path stu which has a cost of 6.
[12] 5. Given a graph G = (V, E), and a parity function p : V → {”odd”, ”even”} (that is, we assign a value ”odd” or ”even” to each vertex of G), the parity matching problem consists in finding a subset E ′ of the edges of G such that for every v ∈ V , the number of edges of
E ′ having v as an endpoint is odd if p(v) = ”odd”, and even if p(v) = ”even”.
For instance, if p(v) = ”even” for every vertex v of G, then the empty…