Exam 2 Answers
1. Find the longest common subsequence of the following two strings. Use the dynamic
programming technique described in the lecture to build up a table. (30 pts)
S1 = TTFAFGTAFT
S2 = AAFTATAFAT
. Consider following classroom assignment prob
Tree Notes
Very similar to evaluating postfix expressions
Evaluating a postfix expression
Scan the input one symbol at a time
If it is a number
push it onto the stack
If it is an operator,
pop 2 numbers from the stack
apply the operator to the 2 numbers
p
Algorithm Notes
Algorithms are the ideas behind computer programs.
An algorithm is the thing that stays the same whether the program is in C+ running on a
Cray or is in BASIC running on a Macintosh!
To be interesting, an algorithm has to solve a general,
Algorithm Properties Notes
Input: A number n
Output: Yes if n is odd, no if n is even
Which of the following algorithms solves Odd Number best?
Count up to that number from one and alternate naming each number as
odd or even.
Factor the number and see if
Constant Notes
RAM model introduces errors in constants
Do all instructions take equal time?
Specific implementation (hardware, code optimizations) can speed up an
algorithm by constant factors
We want to understand how effective an algorithm is independe
Heap Notes
Print queue
Each job has a priority.
The highest priority job is printed next.
Event Simulation
Each new event is put into a priority queue.
Each event has a time at which it occurs
The simulator gets the next event from the priority queue and
Homework 6 Solution
1. P[i,j] : The length of the longest palindrome from A[i] to A[j].
if (i<j) cfw_
if A[i] = A[j] then P[i,j] = P[i+1,j-1] + 2
else P[i,j] = max(P[i+1][j], P[i][j-1])
else if (i>j) P[i][j] = 0;
else if (i=j) P[i][j] = 1;
2.
V[k]: the o
Recursive Notes
A recursive algorithm is an algorithm that calls itself.
Components of a recursive algorithm
Base cases: computation with no recursion
Recursive cases
recursive calls
house keeping: combining recursive results
Recursive functions can be ve
Homework 3
1. Professor Bunyan thinks he has discovered a remarkable property of binary
search trees. Suppose that the search for key k in a binary search tree ends up in a
leaf. Consider three sets: A, the keys to the left of the search path; B, the keys