Midterm Exam 1 with Solution - Algorithm Implementation | CS 1501, Exams of Computer Science

Material Type: Exam; Class: ALGORITHM IMPLEMENTATION; Subject: Computer Science; University: University of Pittsburgh; Term: Spring 2006;

Typology: Exams

Pre 2010

Uploaded on 09/02/2009

koofers-user-eui
koofers-user-eui 🇺🇸

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 1501 Fall 2005
Midterm Exam 1 Solutions
The solutions are in bold.
1) (1 Point Extra Credit) Which National Football League team won the 2006 Super Bowl?
Pittsburgh Steelers
2) (20 points)..
a) What is most important reason that it is customary to ignore multiplicative constants when
computing running times of algorithms and programs?
Because multiplicative constants are implementation dependent. That is, they depend up the
compiler, the machine architecture, etc.
b) What is the main advantage of using a random pivot/splitter in Quicksort over picking the pivot/
splitter to be the first element of the subarray? Both algorithms have expected running time
Theta(n log n). But what is better about the expectation guarantee for randomized Quicksort?
For randomized quicksort you get an expected run time of Theta(n log n) on all inputs. That is,
there are no bad inputs for randomized quicksort. Recall that for this deterministic version of
quicksort, the running time was Theta(n2) for nearly sorted inputs.
c) Consider a system that has 5 possible messages. Each message is selected with the probability
given below. Give an expression for the entropy of this message system. You need not simplify
this expression.
Message M1 M2 M3 M4 M5
Probability .05 .08 .2 .5 .27
.05 lg (1/.05) + .08 log (1/.08) + .2 log (1/.2) + .5 lg (1/.5) + .27 log (1/.27)
d) Consider the message system given in the previous question. What does the Source Coding
Theorem say about any possible encoding of these messages?
The expected number of bits used by this encoding is at least the entropy. Here the expectation
is over the possible messages.
2) (15 points)
a)Assume that we have a program that is known to run in time Theta(n). Fill in the following
table
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam 1 with Solution - Algorithm Implementation | CS 1501 and more Exams Computer Science in PDF only on Docsity!

CS 1501 Fall 2005

Midterm Exam 1 Solutions

The solutions are in bold.

  1. (1 Point Extra Credit) Which National Football League team won the 2006 Super Bowl? Pittsburgh Steelers
  2. (20 points).. a) What is most important reason that it is customary to ignore multiplicative constants when computing running times of algorithms and programs? Because multiplicative constants are implementation dependent. That is, they depend up the compiler, the machine architecture, etc. b) What is the main advantage of using a random pivot/splitter in Quicksort over picking the pivot/ splitter to be the first element of the subarray? Both algorithms have expected running time Theta(n log n). But what is better about the expectation guarantee for randomized Quicksort? For randomized quicksort you get an expected run time of Theta(n log n) on all inputs. That is, there are no bad inputs for randomized quicksort. Recall that for this deterministic version of quicksort, the running time was Theta(n^2 ) for nearly sorted inputs. c) Consider a system that has 5 possible messages. Each message is selected with the probability given below. Give an expression for the entropy of this message system. You need not simplify this expression. Message M1 M2 M3 M4 M Probability .05 .08 .2 .5. .05 lg (1/.05) + .08 log (1/.08) + .2 log (1/.2) + .5 lg (1/.5) + .27 log (1/.27) d) Consider the message system given in the previous question. What does the Source Coding Theorem say about any possible encoding of these messages? **The expected number of bits used by this encoding is at least the entropy. Here the expectation is over the possible messages.
  3. (15 points)** a)Assume that we have a program that is known to run in time Theta(n). Fill in the following table

Input Size Machine Speed Time 100,000 1,000, operations/second 1 Hour 100,000 3,000, operations/second 1/3 hour = 20 minutes 300,000 1,000, operations/second 3 hours 1,000,000 10,000, operations/second 1 Hour b)Assume that we have a program that is known to run in time Theta(n^3 ). Fill in the following table. Input Size Machine Speed Time 100,000 1,000, operations/second 1 Hour 100,000 3,000, operations/second 1/3 hour = 20 minutes 300,000 1,000, operations/second 27 hours (10)1/3^ *** 100,000 ~ 215,**

operations/second 1 Hour c)Explain the standard argument that the asymptotic run time of a program/algorithm becomes more important as machine speeds increase. You can base your answer on the above two tables if you like. If the goal is to run the program on the largest input that can be handled in a fixed amount of time, then programs with higher asymptotic run times gain less from increased processor speed. In the above examples, increasing the machine speed by a factor of 10 allows the linear time algorithm to handle a 10 times larger input, while the Theta(n^3 ) time algorithm can only handle a factor of 2 larger input. 3) Consider the mismatched character heuristic of the Boyer-Moore string matching algorithm. a) (5 points) Show the skip array for the following pattern

Pattern: ABCAB

OUPUT FILE: 0010 0000 0100 0011 0110

  1. Consider the following Patricia Tree. Assume that the first integer in each node is the position in the string on which to branch (the first position is 1). The bit string on the second line in each node is the bit string stored in the node. Answer the following questions: a) (5 Points) For the node where the bit string is not shown, specify which bits you can deduce from the information shown. So we are looking for answer of the form “Bit 1

NULL

must be __, Bit 3 must be …”, or something like that. For 5 points: Bit value 1 0 3 1 6 0 8 1 For 5 extra credit points Bit value 2 0 4 1 5 0 7 1 b) (5 Points) Specify the node where a Search for the bit string 00111000000 terminates. The left/0 child of the root. That is the one with position = 3. c) (10 Points) Show the tree resulting from inserting the bit string 00111000000. A new node is created between the nodes with positions 3 and 6 in the tree. The 1 pointer of the position 3 node is set to this new node. The picture for this part of the tree is shown below.

Here is a C-like solution: Void Permutation(int n, int partial_perm[], int taken[], int depth) if depth == n then for (i=1; i++; i <= n) do print(partial_perm[i]) print(newline) else for (i=1; i++; i <= n) do if not taken[i] then partial_perm[depth +1]=i taken[i]= Permutation(n, partial_perm, taken, depth+1) Taken[i]=