Final Cheat Sheet PDF
Final Cheat Sheet PDF
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥1, 𝐴𝑢𝑥2} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐷𝑒𝑠𝑡 , 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝑆𝑡𝑎𝑟𝑡 }. Estimate the time complexity of
your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 2 𝑛 − 1 + 1 1 = 𝑂 2;
Space Complexity 𝑂(𝑛)
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1, 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥2} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡 , 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥1 }. Estimate the time complexity of
your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 3 𝑛 − 1 + 2 1 = 𝑂 3;
Space Complexity 𝑂(𝑛)
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1, 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡 , 𝐷𝑒𝑠𝑡, 𝑆𝑡𝑎𝑟𝑡 }. Estimate the time complexity of
your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 3 𝑛 − 1 + 2 1 = 𝑂 3;
Space Complexity 𝑂(𝑛)
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1, 𝐴𝑢𝑥2, 𝐴𝑢𝑥3, 𝐷𝑒𝑠𝑡} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐷𝑒𝑠𝑡 , 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝐴𝑢𝑥3 , 𝐴𝑢𝑥3, 𝑆𝑡𝑎𝑟𝑡 }. Estimate
the time complexity of your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 2 𝑛 − 1 + 1 1 = 𝑂 2;
Space Complexity 𝑂(𝑛)
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1, 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥3} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡 , 𝐷𝑒𝑠𝑡, 𝐴𝑢𝑥3 , 𝐴𝑢𝑥3, 𝐴𝑢𝑥1 }. Estimate the
time complexity of your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 2 𝑛 − 1 + 1 1 = 𝑂 2;
Space Complexity 𝑂(𝑛)
Design a function (algorithm) that solves the Towers of Hanoi game for the following graph 𝐺 = (𝑉, 𝐸) with 𝑉 =
{𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1, 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡} and 𝐸 = { 𝑆𝑡𝑎𝑟𝑡, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥1, 𝑆𝑡𝑎𝑟𝑡 , 𝐴𝑢𝑥1, 𝐴𝑢𝑥2 , 𝐴𝑢𝑥2, 𝐴𝑢𝑥1 , 𝐴𝑢𝑥2, 𝐷𝑒𝑠𝑡 }. Estimate the time
complexity of your function, in terms of the number 𝑛 of disks to be moved.
Time Complexity 𝐻 𝑛 = 3 𝑛 − 1 + 2 1 = 𝑂 3;
Space Complexity 𝑂(𝑛)
Related to Virtual Memory Management
Row-Major Order
Key
A[J,I] := A[J,I] * B[J,I] B x (B + B + B)
A = words
Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 512
words, the active memory set size is 256 (i.e., at any time no more than 256 pages may be in main memory), and the replacement
strategy is LRU (the Least Recently Used page is always replaced); also assume that all two-dimensional arrays are of size (1:1024,
1:1024), with each array element occupying one word,
for I := to 1024 do
for J := to 1024 do
{ A[I,J] := A[I,J] * B[J,I] }
provided the arrays are mapped into the main memory space in column-major order.
C = B / A = 1024 / 512 = 2
B x (B + B + C) = 1024 x (1024 + 1024 + 2) = 2,099,200 page transfers
Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 256
words, the active memory set size is 256 (i.e., at any time no more than 256 pages may be in main memory), and the replacement
strategy is LRU (the Least Recently Used page is always replaced); also assume that all two-dimensional arrays are of size (1:512,
1:512), with each array element occupying one word,
for I := to 512 do
for J := to 512 do
{ A[I,J] := A[I,J] * B[J,I] }
provided the arrays are mapped into the main memory space in column-major order.
C = B / A = 512 / 256 = 2
B x (B + B + C) = 512 x (512 + 512 + 2) = 525,312 page transfers
Printing Trees in Order
You are given a general search tree (not necessarily AVL tree). Formulate a function that prints out the n values of the search tree in
descending order. Determine the worst-case time and space complexities of your function.
The worst-case time complexity is 𝑂(𝑛) for a general search tree (binary search tree) because each node needs to be printed.
The worst-case space complexity is also 𝑂(𝑛) because it is based on the height of the tree.
Consider a fully balanced AVL tree where each node’s children, both left and right, have equal height. If the value to be deleted, x, is
any of the leaves of this tree, 1, 3, 5, or 7, the tree will remain balanced once the deletion has taken place. Therefore, there will be
no rebalancing needed after the deletion.
The least number of comparisons that allows us to conclude that a given array with n elements is sorted is 𝑛 − 1 because we
must compare each element, which we’ll call 𝑎= , with its successor element 𝑎=>? , to determine if it is greater or not.
(2) Determine the worst-case complexity (in number of comparisons of two array elements) that allow you to conclude that a given
array with n elements is not sorted!
Comparisons cannot be done faster than 𝑂(𝑛 ∗ log 𝑛 ), so this is the worst-case complexity that allows us to conclude that a
given array with n elements is not sorted.
Exponentiation
If exponentiation is carried out using multiplications as discussed in class, how many multiplications are sufficient to compute 𝑎 DEF ,
for some number? Explain how you arrived at your answer.
(A)
(B)
(A) The following questions apply to a (not necessarily balanced) binary tree.
(a) Insert the following values into an initially empty search tree; show the tree after each insertion:
5 6 7 8 9 1 2 3 4 0
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
5 4 6 1
(B) The following questions apply to a balanced binary search tree (AVL tree).
(a) Insert the following values into an initially empty AVL tree; show the AVL tree after each insertion, indicating
precisely the rotations used:
5 6 7 8 9 1 2 3 4 0
(b) Delete the following elements from the AVL tree you constructed in (a); make sure you rebalance as needed and
show your AVL tree after each deletion, indicating precisely the rotations used:
6 2 8 4 5
(A)
(B)
Related to Optimal Huffman Codes
Construct a Huffman code for the symbols 𝑎 through 𝑖, listed below together with their probabilities. For each of the nine symbols,
give its binary code. Then determine the expected length of your resulting code!
(a) 15/64 (b) 5/64 (c) 10/64 (d) 5/64 (e) 11/64 (f) 3/64 (g) 6/64 (h) 8/64 (i) 1/64
15 5 10 5 11 3 6 8 1 190
2∙ +4∙ +3∙ +3∙ +3∙ +5∙ +3∙ +3∙ +5∙ = = 2.96875
64 64 64 64 64 64 64 64 64 64
When building the tree, the values are stored in a priority queue, so the smaller two values are always branched together first.
Example:
[1 3 5 5 6 8 10 11 15] 1 + 3 = 4 [10 11 11 15 17] 10 + 11 = 21
[4 5 5 6 8 10 11 15] 4+5=9 [11 15 17 21] 11 + 15 = 26
[5 6 8 9 10 11 15] 5 + 6 = 11 [17 21 26] 17 + 21 = 38
[8 9 10 11 11 15] 8 + 9 = 17 [26 38] 26 + 38 = 64
Construct a Huffman code for the symbols 𝑎 through 𝑖, listed below together with their probabilities. For each of the nine symbols,
give its binary code. Then determine the expected length of your resulting code!
(a) 12/64 (b) 5/64 (c) 10/64 (d) 5/64 (e) 14/64 (f) 3/64 (g) 6/64 (h) 8/64 (i) 1/64
12 5 10 5 14 3 6 8 1 190
2∙ +4∙ +3∙ +4∙ +2∙ +5∙ +4∙ +3∙ +5∙ = = 2.96875
64 64 64 64 64 64 64 64 64 64
Construct a Huffman code for the symbols 𝑎 through 𝑖, listed below together with their probabilities. For each of the nine symbols,
give its binary code. Then determine the expected length of your resulting code!
(a) 12/64 (b) 5/64 (c) 10/64 (d) 5/64 (e) 14/64 (f) 2/64 (g) 6/64 (h) 9/64 (i) 1/64
12 5 10 5 14 2 6 9 1 188
2∙ +4∙ +3∙ +4∙ +2∙ +5∙ +4∙ +3∙ +5∙ = = 2.9375
64 64 64 64 64 64 64 64 64 64