Second Midterm Exam - Data Structures and Algorithms | CMPSC 465, Exams of Computer Science

Material Type: Exam; Class: Data Structures and Algorithms; Subject: Computer Science; University: Penn State - Main Campus; Term: Spring 2009;

Typology: Exams

Pre 2010

Uploaded on 09/24/2009

koofers-user-j94
koofers-user-j94 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name 1 2 3 4 5 6 7 total
Section
I will not disclose any content of this exam for 44 hours. Signature:
Second Midterm Exam, CMPSC 465, Spring 2009
This is open book exam, you can also have notes (several sheets or one notebook). The
use of communication equipment and calculators is not permitted the exam.
When you describe an algorithm, you should also justify its correctness and your estimate
of the running time.
If you use the extra space on page 4 and 8, indicate what problem and subproblem it is.
1.Treaps, 5 pt.
Draw a treap for the following set of keys with priorities (the largest priority should be
at the root): key 1 2 3 4 5 6 7 8 9 10 11
priority 5 2 7 0 20 6 2 15 4 1 12
5
3
41
2
8
6
7
11
9
10
5
2
7
0
20
6
2
15
4
1
12
1
pf3
pf4
pf5

Partial preview of the text

Download Second Midterm Exam - Data Structures and Algorithms | CMPSC 465 and more Exams Computer Science in PDF only on Docsity!

Name 1 2 3 4 5 6 7 total

Section

I will not disclose any content of this exam for 44 hours. Signature:

Second Midterm Exam, CMPSC 465, Spring 2009

  • This is open book exam, you can also have notes (several sheets or one notebook). The use of communication equipment and calculators is not permitted the exam.
  • When you describe an algorithm, you should also justify its correctness and your estimate of the running time.
  • If you use the extra space on page 4 and 8, indicate what problem and subproblem it is.
  1. Treaps, 5 pt.

Draw a treap for the following set of keys with priorities (the largest priority should be at the root): key 1 2 3 4 5 6 7 8 9 10 11 priority 5 2 7 0 20 6 2 15 4 1 12

  1. B-trees, 10 pt.

a. Draw a 2-3 tree that results from the tree below if we delete I. M

G

C I

P

N O T U

M

C G

P

N O T U

M

C G N O

P

T U

b. Draw a 2-3 tree that results from the tree below if we insert Q.

M

G

C I

P

N O T U

M

G

C I

P

N O Q T U

M

G

C I

P

N 0 Q

T

U

  1. Problem Reduction, 10 pt.

Given a number array A[n] we can in time O(n) compute array S[n] that allows to answer the the following queries: what is the value of

∑b i=a A[i]?^ (We can have^ S[−1] = 0, S[i] = S[i − 1] + A[i], and the query answer is S[b] − S[a − 1].) Use this to solve the following problem in time O(n^2 ): find i, j such that 0 ≤ i < j < n and the following quantity is maximal:

  ∑^ j

k=i

A[k]

 

2 −

∑^ j

k=i

A[k]^2

To achieve running tim e O (n^2 ) it suffices to evaluate the above expression for every possible

pair of i and j, 0 ≤ i ≤ j ≤ n and record w hen w e get the largest value. B ecause there are

n^2 such pairs, the only problem is to m ake each evaluation in tim e O (1).

The idea that w e can use is to pre-com pute partial sum s

S [ 0 ] = 0

for (i = 0; i < n; i++)

S [ i+1 ] = S [ i ] +A [ i ]

Then w e get

∑j

k=i A[i]^ as S [ j+1 ] -S [ i ].

W e also have sum of squares. A gain, w e pre-com pute partial sum s

SQ [ 0 ] = 0

for (i = 0; i < n; i++)

SQ [ i+1 ] = SQ [ i ] +A [ i ] *A [ i ]

N ow the expression can be evaluated as follow s

expr = S [ j+1 ] -S [ i ]

expr *= expr

expr += SQ [ j+1 ] -SQ [ i ]

  1. Radix sort, 10 pt.

Sort given sequence of words alphabetically using Radix sort: ACB, B, CCC, ACA, C, AB, CA, BC, CAC, BCA, CB Show the sequence that results from each round. If your method does not check all words in each round, indicate which words were checked in a round.

W e sort first by length: B , C, A B , CA , B C, CB , A CB , CCC, A CA , CA C, B CA.

N ext, w e list sequences of length 2, follow ed by those of length 3 that have A /B /C on the

third position:

[length 2] A B , CA , B C, CB , [A ] A CA , B CA , [B ] A CB , [C] CCC, CA C,

N ext, w e list sequences of length 1, follow ed by those of length 2/3 that have A /B /C on

the second position (from the last list):

[length 1] B , C, [A ] CA , CA C, [B ] A B , CB , [C] B C, A CA , B CA , A CB , CCC,

Lastly, w e form and joint lists according to the fist position:

[A ] A B , A CA , A CB , [B ] B , B C, B CA , [C] C, CA , CA C, CB , CCC

  1. Hashing, 5 pt.

Draw the array of chain headers and the chains that form a hash table with hash function h(x) = x mod 11 and which stores the following keys: 3, 5, 9, 12, 14, 17, 21, 33, 42, 50, 55, 62.

I guess you know how to do it, but here is the picture:

0 33 55 1 12 2 3 3 14 4 5 5 6 17 50 7 62 8 9 9 42 10 21