COMP 251 Winter 2017 Professor Jerome Waldispuhl
Assignment 1 – Solutions 2. The below table shows the operations done on the original array A: Operation(s) done since previous line Initial array Build-max-heap(A) – swapped 5 and 4 [End of Build-max-heap(A)] State of array after buildmax-heap Swapped 2 (root) and 6 (last element), assigned i to 6 and discarded it from heap Max-heapify remaining heap – swapped 5 and 2 Max-heapify remaining heap – swapped 4 and 2 [End of Max-heapify] Swapped 1 and 5, reassigned i to 5 and discarded it from heap Max-heapify remaining heap – swapped 4 and 1 Max-heapify remaining heap – swapped 2 and 1 [End of Max-heapify] Swapped 1 and 4, reassigned i to 4 and discarded it from heap Max-heapify remaining heap – swapped 3 and 1 [End of Max-heapify] Swapped 1 and 3, reassigned i to 3 and discarded it from heap
State of array
Value of i
[6, 4, 3, 5, 1, 2] N/A [6, 5, 3, 4, 1, 2] N/A
Value of heapsize 6 6
[6, 5, 3, 4, 1, 2] N/A
6
[2, 5, 3, 4, 1, 6] 6
5
[5, 2, 3, 4, 1, 6] 6
5
[5, 4, 3, 2, 1, 6] 6
5
[1, 4, 3, 2, 5, 6] 5
4
[4, 1, 3, 2, 5, 6] 5
4
[4, 2, 3, 1, 5, 6] 5
4
[1, 2, 3, 4, 5, 6] 4
3
[3, 2, 1, 4, 5, 6] 4
3
[1, 2, 3, 4, 5, 6] 3
2
©2017 Ivaylo Asenov and An Li. All rights reserved.
1
Max-heapify remaining heap [2, 1, 3, 4, 5, 6] 3 2 – swapped 2 and 1 [End of Max-heapify] Swapped 1 and 2, re[1, 2, 3, 4, 5, 6] 2 1 assigned i to 2 and discarded it from heap Discarded last element from [1, 2, 3, 4, 5, 6] 2 1 heap – End of heapsort Note: elements in gray in the array have been discarded from the heap. 𝑛 3. Assuming perfect hashing, there are a total of ( ) possible pairs of elements in 2 1 a hash table and each pair collides with expected probability 𝑚. The expected number of clashes is given by the product of the number of pairs and the expected probability of a collision, which is expressed as: 1 𝑛 ( )× 2 𝑚 𝑛 𝑛! 1 = 2!(𝑛−2)! × 𝑚 by the definition of ( ) 𝑘 𝑛(𝑛−1) 1 = 2 ×𝑚 = =
𝑛(𝑛−1) 2𝑚 𝛼(𝑛−1) 2
𝑛
since 𝛼 = 𝑚
= α × O(n), since
𝑛−1 2
is O(n)
= O(αn), in general = O(n), if load factor is O(1). Source: part of the proof of Theorem 11.9 (p. 279) in Introduction to Algorithms, 3rd Edition by Cormen, Leiserson, Rivest and Stein. 4. RotateRight(B, x) { n = left[x]; // Sets n (node) to be equal to the left child of x left[x] = right[n]; // turns n’s right subtree into x’s left subtree if (right[n] != NIL) { //If the right child of n is not a leaf father[right[y]] = x; } father[n] = father[x]; //links x’s parent to y if (father[x] == NIL) {
2
©2017 Ivaylo Asenov and An Li. All rights reserved.
root[B] = n; } else if (x == right[father[x]]) { right[father[x]] = n; } else { left[father[x]] = n; right[n] = x; //x becomes n’s right child father[x] = y; //x parent becomes n }
Source: similar to the Left-Rotate algorithm (p. 313) in Introduction to Algorithms, 3rd Edition by Cormen, Leiserson, Rivest and Stein. 5. Assume we have a tree with n nodes and we choose a node k to be the root of the tree. There would be n – 1 nodes left that will need to be put in the tree by comparing them to the root. To the left if they are smaller, to the right if they are bigger. If we choose the node k to be the root of the tree, there will be k – 1 nodes smaller than k and n – 1 nodes bigger than k. Because the arrangement of the left sub-tree is independent to the arrangement of the right sub-tree, the number of ways this can be done is determined by the Catalan number Cn, (2𝑛)! 1 2𝑛 which can be obtained using formula Cn = 𝑛+1 ( ) = 𝑛!(𝑛+1)!. 𝑛 For example, for n = 1, there is 1 unique BST: 1
for n = 2, there are 2 unique BSTs: 1 \ 2
2 / 1
for n = 3, there are 5 unique BSTs: 1 \ 3 / 2
3 / 2 / 1
3 / 1 \ 2
2 / \ 1 3
1 \ 2 \ 3
and so on… Source: http://mathworld.wolfram.com/CatalanNumber.html
©2017 Ivaylo Asenov and An Li. All rights reserved.
3
COMP 251 Winter 2017 Professor Jerome Waldispuhl
Assignment 2 – Solutions 3. Take the initial set {x1, x2, …, xk} and sort it to get the set X’ = {x’1, x’2, …, x’k} such that x’1 < x’2 < … < x’k. Start at x’1 (smallest element) and we put the closed interval [x’1, x’1 + 1] in the set S. Then remove all the points that are covered by this interval. Keep doing this for the rest of the elements in X’ until it is not empty. Greedy choice: The largest element in S(x’k) must be contained in some interval that is part of the solution set S and it cannot be better than [x’ i , x’k + 1] Optimal substructure: Assume that Sik is the subset of points between x’i and x’k. Therefore, the solution must be a subset of the solution set for S. Time Complexity: First, we will need to sort the initial set using a sorting algorithm like mergesort which is O(n log(n)). Then we will need to go through all the elements in the sorted list (worst case) which is O(n). Therefore, this is an O(n log(n) + n) algorithm. 4. Running Dijkstra’s algorithm on the following graph would produce an incorrect answer:
Set d[A] d[B] d[C] d[D] {A} 0 5 6 ∞ {A, B} 0 5 6 12 {A, B, C} 0 5 6 12 {A, B, C, D} 0 5 6 12 In reality, the shortest path from A to B has weight 4, and the shortest path from A to D has weight 11.
©2017 Ivaylo Asenov and An Li. All rights reserved.
1
Source: A slightly modified version of Problem 3 of https://www.cs.umd.edu/class/fall2014/cmsc351/HW/hw6-solutions.pdf 5. : Suppose that the graph G is bipartite where V = A ∪ B (set of vertices). Assume that there is a cycle of length k, where k is an odd number: v1, v2, …, vk. Assume that v1 is in A. Because the graph is bipartite, every vi with i being odd will be in A and every vi with i being even will be in B. Then there is an edge between v1 and vk. Contradiction: G is bipartite and there cannot be any edge with both endpoints in A. Thus, a bipartite graph cannot have odd cycles. : Take a spanning tree T of G, pick some vertex r and call it the “root”. For each vertex, there is an unique path in T from r to v. The path length is either odd or even; this defines a bipartition of V. We claim that G is bipartite with this bipartition. Assume that there is an edge e in E between two vertices x and y. If e is in T, then x and y have different parities. If e is not in T, let P be the path from x to y in T. Then P+e is a cycle, which must be even. So P has odd length, and the vertices in P must alternate between odd and even. So x and y must have different parities.
2
©2017 Ivaylo Asenov and An Li. All rights reserved.
COMP 251 Winter 2017 Professor Jerome Waldispuhl
Assignment 3 – Solutions 3. P = array of length N, stores the index of the predecessor of X[k] in the longest increasing subsequence ending at X[k]. M = array of length N + 1, stores the index k of the smallest value X[k] such that there is an increasing subsequence of length j ending at X[k] on the range k ≤ i. L = 0 // Initialize length to 0 for i in 0 to N-1 do Do binary search to find the largest positive j ≤ L such that X[M[j]] < X[i] After searching, lo is 1 greater than the length of the longest prefix of X[i] The predecessor of X[i] is the last index of the subsequence of length newL–1 If we found a subsequence longer than any we've found yet, reassign L Finally, reconstruct the longest increasing subsequence by reversing the array. Source: https://en.wikipedia.org/wiki/Longest_increasing_subsequence 4. A 0 1 ↖1 G 1 A 2 0 T 3 1 Arrows in table above indicate the path
A 2 2 ↖0 1
C 3 3 1 1
T 4 4 2 ↖0
Optimal sequence alignment: AACT GA-T Legend: Black: insertion/deletion Red: substitution Green: match Min cost: 1 – 1 + 1 – 1 = 0
©2017 An Li. All rights reserved.
1
COMP 251 Winter 2017 Professor Jerome Waldispuhl
Assignment 4 – Solutions 1.
According to the graph above, the number of bit operations nearly doubles every time a power of 2 is reached. In addition, the larger the number of bits, the greater the discrepancy between the naive and the Karatsuba methods is. In addition, Karatsuba generally runs faster than the naive algorithm when the number of bits is large. 2. 𝑛
a) T(n) = 25𝑇 (5 ) + n a = 25, b = 5, f(n) = n, logba = log525 = 2 By Case 1, f(n) = O(𝑛𝑙𝑜𝑔5 25−𝜖 ) = O(n2 – ϵ). One can obtain O(n) with ϵ ≥ 1. Therefore, T(n) = Θ(n2). 𝑛
b) T(n) = 2𝑇 ( 3 ) + n log(n) a = 2, b = 3, f(n) = n log(n), logba = log32 = 0.6309
©2017 An Li. All rights reserved.
1
By Case 3, f(n) = Ω(𝑛𝑙𝑜𝑔3 2+𝜖 ). To get Ω(n log(n)), ϵ needs to be slightly larger 𝑛
than 1 – log32 = 0.3691. 2𝑓 ( 3) =
2𝑛 3
𝑛
2
𝑙𝑜𝑔 3 ≤ cn log(n) for large n when 3 ≤ c
2cπk, which violates the regularity condition. 𝑛
3. TA(n) = 7𝑇𝐴 (2) + n2.
a = 7, b = 2, f(n) = n2, logba = log27 = 2.80735 By Case 1, f(n) = O(𝑛𝑙𝑜𝑔2 7−𝜖 ). One can obtain O(n2) with ϵ = log27 – 2 = 0.80735. Therefore, TA(n) = Θ(𝑛𝑙𝑜𝑔2 7). 𝑛
TB(n) = α𝑇𝐵 (4) + n2.
a = α, b = 4, f(n) = n2. Applying Case 1 to this example, one must find the largest α such that log4α < log27 for TB to be asymptotically faster than TA. α < 4𝑙𝑜𝑔2 7 = 49 Setting α to 49 will make both recurrences have the asymptotic running time, so the largest integer value α can be to ensure that TB is asymptotically faster than TA is 48.
2
©2017 An Li. All rights reserved.