0% found this document useful (0 votes)
25 views10 pages

Final Cheat Sheet PDF

Uploaded by

thinhbuiquoc10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

Final Cheat Sheet PDF

Uploaded by

thinhbuiquoc10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Towers of Hanoi

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

A[I,J] := A[I,J] * B[I,J] 3 x B x C

A[I,J] := A[I,J] * B[J,I]


B x (B + C + C)
A[J,I] := A[I,J] * B[I,J]

A[I,J] := A[J,I] * B[J,I]


B x (B + B + C)
A[J,I] := A[J,I] * B[I,J]

Key
A[J,I] := A[J,I] * B[J,I] B x (B + B + B)
A = words

Column-Major Order B = array size

A[I,J] := A[I,J] * B[I,J] 3 x B x B C = B / A

A[I,J] := A[I,J] * B[J,I]


B x (B + B + C)
A[J,I] := A[I,J] * B[I,J]

A[I,J] := A[J,I] * B[J,I]


B x (B + C + C)
A[J,I] := A[J,I] * B[I,J]

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.

Height of an AVL Tree


For any positive value of h, describe an AVL tree of height h and a value x in that tree such that deleting x is guaranteed to result in
no rebalancing operations at all.

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.

Related to Array Comparisons


(1) Determine the least number of comparisons that allow you to conclude that a given array with n elements is sorted!

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.

𝑎? ∙ 𝑎? = 𝑎 H 𝑎 D ∙ 𝑎 D = 𝑎?E 𝑎 EI ∙ 𝑎 EI = 𝑎?HD 𝑎 F?H ∙ 𝑎 HFE = 𝑎 JED 𝑎 DEI ∙ 𝑎? = 𝑎 DEF


𝑎H ∙ 𝑎H = 𝑎I 𝑎?E ∙ 𝑎?E = 𝑎 KH 𝑎?HD ∙ 𝑎?HD = 𝑎 HFE 𝑎 JED ∙ 𝑎 EI = 𝑎 DKH
𝑎I ∙ 𝑎I = 𝑎D 𝑎 KH ∙ 𝑎 KH = 𝑎 EI 𝑎 HFE ∙ 𝑎 HFE = 𝑎 F?H 𝑎 DKH ∙ 𝑎 KH = 𝑎 DEI
Related to Euclid’s Algorithm
Compute the multiplicative inverse of 65 536 with respect to 331 679.
𝑖 𝑎 𝑥 𝑦 𝑞
1 65536 0 1
2 331679 1 0
3 65536 1 0 0
4 3999 -5 1 5
5 1552 81 -16 16
6 895 -167 33 2
7 657 248 -49 1
8 238 -415 82 1
9 181 1078 -213 2
10 57 -1493 295 1
11 10 5557 -1098 3
12 7 -29278 5785 5
13 3 34835 -6883 1
14 1 -98948 19551 2

331679 ∙ 19551 + 65536 ∙ −98948 = 1


Value 𝑥 in row 14, of which 𝑎 = 1, is negative, so add it to the w.r.t. value. 331679 + −98948 = 232731
The multiplicative inverse of 65536 with respect to 331679 is 232731.

Compute the multiplicative inverse of 65 536 with respect to 32 677.


𝑖 𝑎 𝑥 𝑦 𝑞
1 65536 1 0
2 32677 0 1
3 182 1 -2 2
4 99 -179 359 179
5 83 180 -361 1
6 16 -359 720 1
7 3 1975 -3961 5
8 1 -10234 20525 5

32677 ∙ 20525 + 65536 ∙ −10234 = 1


Value 𝑥 in row 8, of which 𝑎 = 1, is negative, so add it to the w.r.t. value. 32677 + −10234 = 22443
The multiplicative inverse of 65536 with respect to 32677 is 22443.

Compute the multiplicative inverse of 65 536 with respect to 32 765.


𝑖 𝑎 𝑥 𝑦 𝑞
1 65536 0 1
2 32765 1 0
3 6 1 -2 2
4 5 -5460 10921 5460
5 1 5461 -10923 1

32765 ∙ −10923 + 65536 ∙ 5461 = 1


Value 𝑥 in row 5, of which 𝑎 = 1, is positive, so that is the solution.
The multiplicative inverse of 65536 with respect to 32765 is 5461.

Use the excel calculator for this one.


Compute the multiplicative inverse of 131 072 with respect to 32 765.
𝑖 𝑎 𝑥 𝑦 𝑞
1 131072 0 1
2 32765 1 0
3 12 1 -4 4
4 5 -2730 10921 2730
5 2 5461 -21846 2
6 1 -13652 54613 2

32765 ∙ 54613 + 131072 ∙ −13652 = 1


Value 𝑥 in row 6, of which 𝑎 = 1, is negative, so add it to the w.r.t. value. 32765 + −13652 = 19113
The multiplicative inverse of 131072 with respect to 32765 is 19113.

Compute the multiplicative inverse of 131 072 with respect to 93 761.


𝑖 𝑎 𝑥 𝑦 𝑞
1 131072 0 1
2 93761 1 0
3 37311 1 -1 1
4 19139 -2 3 2
5 18172 3 -4 1
6 967 -5 7 1
7 766 93 -130 18
8 201 -98 137 1
9 163 387 -541 3
10 38 -485 678 1
11 11 2327 -3253 4
12 5 -7466 10437 3
13 1 17259 -24127 2

93761 ∙ −24127 + 131072 ∙ 17259 = 1


Value 𝑥 in row 13, of which 𝑎 = 1, is positive, so that is the solution.
The multiplicative inverse of 65536 with respect to 331679 is 17259.

Compute the multiplicative inverse of 93 775 with respect to 131 072.


𝑖 𝑎 𝑥 𝑦 𝑞
1 93775 0 1
2 131072 1 0
3 93775 1 0 0
4 37297 -1 1 1
5 19181 3 -2 2
6 18116 -4 3 1
7 1065 7 -5 1
8 11 -123 88 17
9 9 11815 -8453 96
10 2 -11938 8541 1
11 1 59567 -42617 4

131072 ∙ −42617 + 93775 ∙ 59567 = 1


Value 𝑥 in row 11, of which 𝑎 = 1, is positive, so that is the solution.
The multiplicative inverse of 65536 with respect to 331679 is 59567.
Related to Binary Search Trees
(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:
4 6 7 8 9 0 1 2 3 5
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
6 8 2 5 4
(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:
4 6 7 8 9 0 1 2 3 5
(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 8 2 5 4

(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

Symbol Code Length Frequency


a 01 2 15/64
b 1011 4 5/64
c 110 3 10/64
d 000 3 5/64
e 111 3 11/64
f 10101 5 3/64
g 001 3 6/64
h 100 3 8/64
i 10100 5 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

Symbol Code Length Frequency


a 00 2 12/64
b 1011 4 5/64
c 110 3 10/64
d 1111 4 5/64
e 01 2 14/64
f 10101 5 3/64
g 1111 4 6/64
h 100 3 8/64
i 10100 5 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

[1 3 5 5 6 8 10 12 14] 1+3=4 [10 11 12 14 17] 10 + 11 = 21


[4 5 5 6 8 10 12 14] 4+5=9 [12 14 17 21] 12 + 14 = 26
[5 6 8 9 10 12 14] 5 + 6 = 11 [17 21 26] 17 + 21 = 38
[8 9 10 11 12 14] 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) 2/64 (g) 6/64 (h) 9/64 (i) 1/64

Symbol Code Length Frequency


a 00 2 12/64
b 1001 4 5/64
c 110 3 10/64
d 1110 4 5/64
e 01 2 14/64
f 10001 5 2/64
g 1111 4 6/64
h 101 3 9/64
i 10000 5 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

[1 2 5 5 6 9 10 12 14] 1+2=3 [10 11 12 14 17] 10 + 11 = 21


[3 5 5 6 9 10 12 14] 3+5=8 [12 14 17 21] 12 + 14 = 26
[5 6 8 9 10 12 14] 5 + 6 = 11 [17 21 26] 17 + 21 = 38
[8 9 10 11 12 14] 8 + 9 = 17 [26 38] 26 + 38 = 64

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy