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

Exam2 Cheat Sheet PDF

This document provides example problems related to virtual memory management, parameter passing, binary search trees, optimal Huffman codes, and other computer science exam topics. It includes multiple parts for each problem testing different concepts. The problems would provide practice for a computer science exam on these essential topics.

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)
72 views10 pages

Exam2 Cheat Sheet PDF

This document provides example problems related to virtual memory management, parameter passing, binary search trees, optimal Huffman codes, and other computer science exam topics. It includes multiple parts for each problem testing different concepts. The problems would provide practice for a computer science exam on these essential topics.

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

COSC 3320 Exam 2 Cheat Sheet

Related to Virtual Memory Management


Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 128
words, the active memory set size is 100 (i.e., at any time no more than 100 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:256,
1:256), with each array element occupying one word, provided the arrays are mapped into the main memory space in column-major
order:
for I := to 256 do
for J := to 256 do
{ A[I,J] := A[I,J] * B[N-I+1,J] }

There are 2 matrices to be read and 1 to be written to.


(2 x 256 x 256 reads) + (1 x 256 x 256 writes) = 196608 total page transfers

Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 128
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, provided the arrays are mapped into the main memory space in column-major
order:
for I := to 512 do
for J := to 512 do
{ A[I,J] := A[I,J] * B[512-I+1,J] }

There are 2 matrices to be read and 1 to be written to.


(2 x 512 x 512 reads) + (1 x 512 x 512 writes) = 786432 total page transfers

Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 64
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, provided the arrays are mapped into the main memory space in column-major
order:
for I := to 512 do
for J := to 512 do
{ A[I,J] := A[I,J] * B[512-I+1,J] }

There are 2 matrices to be read and 1 to be written to.


(2 x 512 x 512 reads) + (1 x 512 x 512 writes) = 786432 total page transfers
Related to Parameter Passing
Consider the following function CheckSums(A,B,C) where A is a 2D array of size (1:n, 1:n) and B and C are vectors of size (1:n), n
a positive integer. The body of CheckSums implements the following computations:
B[i] = A[i,1] + A[i,2] + A[i,3] + … + A[i,n]
C[i] = A[1,i] + A[2,i] + A[3,i] + … + A[n,i]
(a) Give the complete function CheckSums(A,B,C), including in particular the code of the function body and the method of
passing the three parameters.
(b) Determine for your function its time and space complexity (space complexity is the amount of space of memory required by
the function, in addition to the space for the actual parameters).
Warning: Your function must work correctly for calls such as CheckSums(D[1:n,1:n],D[1:n,1],D[1,1:n])! Here D[1:n,1] is the first column of the 2D array D, D[1,1:n] the first row.

(a) CheckSums(A,B,C) { // all passed by reference


int newB[n], newC[n];
for (int i = 1; i <= n; i++) {
newB[i] = newC[i] = 0;
for (int j = 1; j <= n; j++) {
newB[i] += A[i,j];
newC[i] += A[j,i];
}
}
for (int k = 1; k <= n; k++) {
B[k] = newB[k];
C[k] = newC[k];
}
}
(b) Time Complexity: The nested for loop (loops 𝑖 and 𝑗) creates a time complexity of 𝑂(𝑛& ), and the separate for loop (loop 𝑘)
has a time complexity of 𝑂(𝑛). In total, we have a time complexity of 𝑂(𝑛& + 𝑛). As our 𝑛 becomes larger, our time
complexity simplifies down to 𝑂(𝑛& ).
Space Complexity: The two new arrays created in the function, bNew and cNew, have no parameter values, only two 1D
arrays which makes the space complexity 𝑂(𝑛).

Consider the following function CheckProds(A,B,C) where A is a 2D array of size (1:n, 1:n) and B and C are vectors of size (1:n),
n a positive integer. The body of CheckProds implements the following computations:
B[i] = A[i,1] * A[i,2] * A[i,3] * … * A[i,n]
C[i] = A[1,i] * A[2,i] * A[3,i] * … * A[n,i]
(a) Give the complete function CheckProds(A,B,C), including in particular the code of the function body and the method of
passing the three parameters.
(b) Determine for your function its time and space complexity (space complexity is the amount of space of memory required by
the function, in addition to the space for the actual parameters).
Warning: Your function must work correctly for calls such as CheckProds(D[1:n,1:n],D[1:n,1],D[1,1:n])! Here D[1:n,1] is the first column of the 2D array D, D[1,1:n] the first row.

(a) CheckProds(A,B,C) { // all passed by reference


int newB[n], newC[n];
for (int i = 1; i <= n; i++) {
newB[i] = newC[i] = 1;
for (int j = 1; j <= n; j++) {
newB[i] = newB[i] * A[i,j];
newC[i] = newC[i] * A[j,i];
}
}
for (k = 1; k <= n; k++) {
B[k] = newB[k];
C[k] = newC[k];
}
}

(b) Solution for this is the same as the one above.


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:
1 8 2 5 6 3 4 7 9 10
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
1 2 8 5
(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:
1 8 2 5 6 3 4 7 9 10
(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:
1 2 8 5

(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 1 2 8 6 3 4 7 10 9
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
1 2 8 5
(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 1 2 8 6 3 4 7 10 9
(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:
1 2 8 5

(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:
8 1 2 6 5 3 4 7 10 9
(b) Delete the following elements from the search tree you constructed in (a); show the tree after each deletion:
1 2 8 5
(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:
8 1 2 6 5 3 4 7 10 9
(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:
1 2 8 5
(A)

(B)

Deletion for this one is the same as previous problem.


Related to Optimal Huffman Codes
The ones and zeros on the branches are WRONG. 0 always goes on the branch with the smaller value and 1 on the larger.
Construct a Huffman code for the symbols 𝑎 through ℎ, listed below together with their probabilities. Then determine the expected
length of your resulting code!
(a) 1/40 (b) 2/40 (c) 3/40 (d) 4/40 (e) 5/40 (f) 6/40 (g) 8/40 (h) 11/40

Symbol Code Length Frequency


a 00000 5 1/40
b 00001 5 2/40
c 0001 4 3/40
d 100 3 4/40
e 101 3 5/40
f 001 3 6/40
g 11 2 8/40
h 11 2 11/40

1 2 3 4 5 6 8 11 110
5∙ +5∙ +4∙ +3∙ +3∙ +3∙ +2∙ +2∙ = = 2.75
40 40 40 40 40 40 40 40 40

When building the tree, the values are stored in a priority queue, so the smaller two values are always branched together first.
Example:
[1 2 3 4 5 6 8 11] 1 + 2 = 3 Add 3 to PQ [8 9 11 12] 8 + 9 = 17 Add 17 to PQ
[3 3 4 5 6 8 11] 3+3=6 Add 6 to PQ [11 12 17] 11 + 12 = 23 Add 23 to PQ
[4 5 6 6 8 11] 4+5=9 Add 9 to PQ [17 23] 17 + 23 = 40 Add 40 to PQ
[6 6 8 9 11] 6 + 6 = 12 Add 12 to PQ [40]

Construct a Huffman code for the symbols 𝑎 through 𝑔, listed below together with their probabilities. Then determine the expected
length of your resulting code!
(a) 1/28 (b) 2/28 (c) 3/28 (d) 4/28 (e) 5/28 (f) 6/28 (g) 7/28

Symbol Code Length Frequency


a 0000 4 1/28
b 0001 4 2/28
c 001 3 3/28
d 100 3 4/28
e 101 3 5/28
f 01 2 6/28
g 11 2 7/28

1 2 3 4 5 6 7 74
4∙ +4∙ +3∙ +3∙ +3∙ +2∙ +2∙ = = 2.64
28 28 28 28 28 28 28 28
Related to HeapSort
(a) Construct a heap for the following array of numbers: 1 8 2 5 6 3 4 7 9 10
Show the array after the insertion of each element into the heap.
(b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.

(a) When inserting, check that the inserted value is not greater than its parent.
If it is, swap that node with its parent and continue moving upwards until the value is no longer greater than its parent.
(b) Remove the max value and replace it with the last value. Place the removed value into the max array.
Swap values until the replacement value is no longer less than its children.
If both children are larger, replace with the largest of the two.

1
1 8
8 1 2
8 1 2 5
8 5 2 1 6
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 9
9 8 4 7 5 2 3 1 6 10
10 9 4 7 8 2 3 1 6 5

10 9 4 7 8 2 3 1 6 5
5 9 4 7 8 2 3 1 6 10
9 8 4 7 5 2 3 1 6 10
6 8 4 7 5 2 3 1 9 10
8 7 4 6 5 2 3 1 9 10
1 7 4 6 5 2 3 8 9 10
7 6 4 1 5 2 3 8 9 10
3 6 4 1 5 2 7 8 9 10
6 5 4 1 3 2 7 8 9 10
2 5 4 1 3 6 7 8 9 10
5 3 4 1 2 6 7 8 9 10
2 3 4 1 5 6 7 8 9 10
4 3 2 1 5 6 7 8 9 10
1 3 2 4 5 6 7 8 9 10
3 1 2 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
(a) Construct a heap for the following array of numbers: 5 1 2 8 6 3 4 7 10 9
Show the array after the insertion of each element into the heap.
(b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.

5 Part b of this one is the same as the one on the previous page.
5 1
5 1 2
5 1 2 8
8 5 2 1 6
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 10
10 8 4 7 5 2 3 1 6 9
10 9 4 7 8 2 3 1 6 5

(a) Construct a heap for the following array of numbers: 8 1 2 6 5 3 4 7 10 9


Show the array after the insertion of each element into the heap.
(b) Use your heap to sort the array. Show the resulting heap after the extraction of each maximum.

8 Part b of this one is the same as the one on the previous page.
8 1
8 1 2
8 1 2 6
8 6 2 1 5
8 6 2 1 5 3
8 6 3 1 5 2 4
8 6 4 1 5 2 3 7
8 7 4 6 5 2 3 1 10
10 8 4 7 5 2 3 1 6 9
10 9 4 7 8 2 3 1 6 5
Related to AVL Trees
For a balanced search tree (AVL tree) containing 𝑛 elements, determine (in terms of 𝑛)
(a) the length of a longest path from the root to a leaf.
(b) the length of a shortest path from the root to a leaf.
Hint: Consider an AVL tree with 𝑛 nodes that is of maximal height.

(a) The length of a longest path from the root to a leaf is the equal to the height 𝑛 of the tree. Therefore, it is log & 𝑛 .
(b) The length of a shortest path from the root to a leaf can be found due to AVL height restrictions. Either side of a node can
only have a height difference of at most 1. Because of the height restriction causing both sides to be within one child of
each other, the length of a shortest path from root to a leaf is log & 𝑛 − 1.

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.

Use the excel calculator for this one.

You might also like