Are you sure?
This action might not be possible to undo. Are you sure you want to continue?
These questions are pulled from several previous finals. Your actual final will include
fewer questions and may include different topics since we did not cover the same topics
in previous classes. However, these problems should give you an idea of what type of
questions you will be asked.
Short Answer
Give a short answer (i.e. 1 to 3 sentences) for the following questions:
a) Give two reasons why it may be preferable to implement a O(n
2
) algorithm over an
O(n) algorithm that solves the same problem.
b) In the BuildHeap algorithm, the O(lgn) Heapify routine is invoked n times. This
seems to indicate that BuildHeap is O(nlgn). Describe why the better bound for
BuildHeap is really O(n).
c) Given a set of n numbers, we wish to find the i largest in sorted order. One solution
is to use the expected running time of the Selection algorithm to find the ith largest
number, partition around that number, and sort the i largest numbers (say, using
Merge Sort). What is the running time in terms of n and i? Explain your answer.
d) Describe an O(n) algorithm to union two binary heaps together (not binomial heaps).
e) The algorithm below, “Fibsort”, sorts an input array of numbers. Its parameters are
the array and the starting/ending indices of the range within the array that we would
like to sort.
Write a recurrence relation describing the runtime of the algorithm. Do not solve the
recurrence.
Fibsort(long A[], int Start, int End)
If End <= Start Then
Return
If A[Start] > A[End] Then
Swap A[Start], A[End]
Fibsort(A, Start + 1, End – 1)
If A[Start] > A[Start + 1] Then
Swap A[Start], A[Start+1]
Fibsort(A, Start + 1, End)
Sample call for A[] = { 5, 4, 99, 21, 44 } :
Fibsort(A, 1, 5);
NPCompleteness
1) Show that the Subset Sum problem is NPComplete.
Subset Sum: Given a set S of n numbers and a target number C, you want to know what
subset of S sums to exactly C.
Assume that you know the Set Partition problem to be NPComplete. In the Set Partition
problem, you are given a set S of n numbers and you want to know if the set can be
divided into two sets so that the sum of all elements in the first set is equal to the sum of
all elements in the second set.
Note that this is backwards from the homework problem. In the homework, you were
given that Subset Sum is NPComplete and showed that Set Partition is NPComplete.
Here, you are given that Set Partition is NPComplete and must show that Subset Sum is
NPComplete.
2) Show that the decision version of the longest path problem is NPComplete.
The longest path problem is: Given an undirected graph G(V,E) of n vertices and a
positive integer k, does G contain a simple cycle containing at least k vertices?
A simple cycle is a cycle that doesn’t repeat any edges or vertices.
Show that the longest path problem is NPComplete. You can use the knowledge that the
Hamilton Circuit problem is NPComplete.
Dynamic Programming
1) Planning a Party
Professor McKenzie is consulting for the president of AB corporation, which is planning
a company party. The company has a hierarchical structure; that is, the supervisor
relation forms a tree rooted at the president. A person has only one supervisor.
The personnel office has ranked each employee with a conviviality rating, C[employee],
which indicates how fun that person will be at the party. C[employee] is a real number.
In order to maximize the party atmosphere, the president does not want both an employee
and his or her immediate supervisor to attend.
a) Describe an algorithm to make up the guest list. The goal should be to maximize
the sum of the conviviality ratings of the guests.
b) How can the professor ensure that the president gets invited to his own party?
2) Trading Posts
There are n trading posts along a river numbered n, n 1, … 3, 2, 1.. At any of the posts
you can rent a canoe to be returned at any other post downstream. (It is impossible to
paddle against the river since the water is moving too quickly). For each possible
departure point i and each possible arrival point j (< i), the cost of a rental from i to j is
known. It is C[i,j]. However, it can happen that the cost of renting from i to j is higher
than the total costs of a series of shorter rentals. In this case you can return the first canoe
at some post k between i and j and continue your journey in a second (and, maybe, third,
fourth ...) canoe. There is no extra charge for changing canoes in this way.
Give a dynamic programming algorithm to determine the minimum cost of a trip by
canoe from each possible departure point i to each possible arrival point j. Analyze the
running time of your algorithm in terms of n. For your dynamic programming solution,
focus on computing the minimum cost of a trip from trading post n to trading post 1,
using up to each intermediate trading post.
3. Coin Changer
In the year 2012, the US Congress passes an amendment allowing foreignborn citizens
to become president and Arnold Schwarzenegger is elected president of the United
States. To commemorate his achievement a new 20 cent coin is created in his honor:
While a great honor to president Schwarzenegger, the new coin means that we can no
longer use the greedy strategy to return change with the fewest number of coins. The
greedy strategy is to simply dole out the largest of each coin possible. For example, to
give 40 cents in change, the greedy strategy would return 1 quarter, 1 dime, and 1 nickel.
However, we could instead dole out 2 Schwarzeneggers which would be the fewest
number of coins possible.
Devise a dynamic programming formulation that determines the fewest number of coins
to return as change for coin denominations D
0
, D
1
, D
2
, D
3
and D
4
. D
0
is always one cent.
D
1
through D
4
could be arbitrary amounts (e.g. perhaps D
1
=5, D
2
=10, D
3
=20, and D
4
=25
cents).
To get you started, the minimum number of coins to return is denoted by C[N], where N
is the amount of change:
C[N] = 0 if N = 0
; if no change to return, minimum number of
; coins is zero
You complete the dynamic programming formulation for N > 0:
C[N] = if N > 0
Recurrence Relations and Algorithm Design
1) Min/Max Finding
Given an array A of n real numbers, a(1), a(2) … a(n) , the algorithm below is to find the
maximum and minimum of these n numbers. It is invoked via:
FindMinMax(A,1,n,min,max). It works as follows:
FindMinMax(A, lowindex, highindex, &min, &max)
If lowindex=highindex then
Max=A(lowindex)
Min=A(lowindex)
Return
(
¸
(
¸
+
←
2
highindex lowindex
mid
FindMinMax(A, lowindex, mid, min1, max1)
FindMinMax(A, mid+1, highindex, min2, max2)
Min Minimum(min1,min2)
Max Maximum(max1,max2)
Let T(n) be the number of comparisons needed using this algorithm. Find a recursive
equation for T(n) and solve it using any method you like.
2) Nuts and Bolts
You have a bucket that contains exactly n nuts and n bolts. For each nut in the bucket,
there is exactly one bolt in the bucket that fits it, and vice versa. You want to find the nut
that fits each bolt. More formally, the nuts may be defined as N[1..n] and the bolts as
B[1..n].
The only technique that you may use to assess the relative sizes of the nuts (and bolts) is
to try to screw a particular nut N[i] onto a particular bolt B[j]. If the nut is too big for the
bolt it will go on loosely, and you can tell that. If the nut is too small for the bolt it won't
go on at all, and you can tell that. And if the nut exactly fits the bolt, you can tell that,
too. In other words, you can see if a nut is <, =, or > a bolt, but you can’t compare nuts to
nuts or bolts to bolts.
Describe an expected (i.e. average case) O(nlgn) algorithm to solve the Nuts and Bolts
problem. Justify that your solution is O(nlgn) by providing a recurrence relation
and solving it using any method.
If you cannot think of a O(nlgn) solution you can get half credit by describing a O(n
2
)
solution.
3) Sorting
Below are the contents of an array as various algorithms are sorting it. Each line does not
necessarily represent the next step in the algorithm, but only some later step during the
course of the algorithm’s execution. Key elements are depicted in bold or underline.
Identify each algorithm and provide a brief rationale for each answer to make it clear that
you did not just guess at a sorting algorithm.
Algorithm 1
23 9 2 12 77 39
12 9 2 23 77 39
12 9 2 23 77 39
2 9 12 23 77 39
2 9 12 23 77 39
2 9 12 23 77 39
2 9 12 23 77 39
2 9 12 23 77 39
2 9 12 23 77 39
2 9 12 23 39 77
Algorithm 2
12 39 2 94 23 77 52 9
12 39 2 94 23 77 52 9
2 12 39 94 23 77 52 9
2 12 39 94 23 77 52 9
2 12 23 39 94 77 52 9
2 12 23 39 77 94 52 9
2 12 23 39 52 77 94 9
2 9 12 23 39 52 77 94
Algorithm 3
12 39 2 94 23 77 52 9
94 39 77 12 23 2 52 9
77 39 52 12 23 2 9 94
52 39 9 12 23 2 77 94
39 23 9 12 2 52 77 94
23 12 9 2 39 52 77 94
12 2 9 23 39 52 77 94
9 2 12 23 39 52 77 94
2 9 12 23 39 52 77 94
Algorithm 4
12 39 02 94 23 77 52 09
12 02 52 23 94 77 39 09
02 09 12 23 39 52 77 94
4) Recurrence relations
Give asymptotically tight BigO bounds for the following recurrence relations. Justify
your solution with the indicated method for solving the recurrence relation.
a) 1 ) 2 ( ) ( + − = n T n T Use iterative substitution method
2 for 1 ) ( ≤ = n n T
b) . 1
2
2 ) ( + 
¹

\

=
n
T n T Use induction. Guess T(n)=O(n).
1 ) 1 ( = T
c) n
n
T n T +

¹

\

=
4
9 ) ( Use the master theorem.
1 ) 1 ( = T
5) Lab Section Scheduling
100 students have registered for CS201. Each student must attend one of 5 lab sessions.
A lab session cannot hold more than 25 students. Each student has specified three
choices for their lab section. A “happiness” rating is assigned to each choice. A student
assigned to their first choice is worth 10 points, the second choice is worth 5 points, and
the third choice is worth 2 points. An assignment outside the top three is worth 0 points.
Describe how students can be assigned to lab sections so that the happiness of all students
is maximized while not exceeding the capacity of any individual lab section.
6) String Matching
Give the pattern "abbab" and the text "abbacabbab":
Initially the pattern and text would line up as follows:
Text: abbacabbab
Pattern: abbab
Using the naïve algorithm, there would be a mismatch at the characters in bold:
Text: abbacabbab
Pattern: abbab
Next, the naïve algorithm would shift the pattern over one character and resume
comparisons at the underlined characters:
Text: abbacabbab
Pattern: abbab
a) For the same initial lineup of the pattern and text, show where the mismatch would
occur and then how the pattern would be shifted and what characters would be
compared next using the KnuthMorrisPratt algorithm.
b) For the same initial lineup of the pattern and text, show where the mismatch would
occur and then how the pattern would be shifted and what characters would be
compared next using the Boyer Moore algorithm.
7) GossipNet
Through years of meticulous observation you have determined how many days it takes on
average for gossip to travel between members of your family and friends. This data has
been compiled into the graph below. For example, Mom spreads gossip daily to your
sister, yourself, and Aunt Kiki. She only gossips to Uncle Ned every three days and to
Uncle Jed every 42 days.
Use Dijkstra's Algorithm to compute how long it will take gossip to travel from you to
everyone else in your social network.
As you run through the algorithm, put a number next to each vertex in the order that you
extract them from the minqueue. Next to each vertex you should also give the final
distance/cost to the source vertex (You), and the parent vertex that is necessary to
recreate the actual shortest path.
Sister Mary
You
Bill
Mom
Aunt Kiki
Uncle Ned
Uncle Jed
3
1
1
7
4
2
1
2
42
1
1
3
19
does G contain a simple cycle containing at least k vertices? A simple cycle is a cycle that doesn’t repeat any edges or vertices. In the Set Partition problem. Show that the longest path problem is NPComplete.NPCompleteness 1) Show that the Subset Sum problem is NPComplete. Subset Sum: Given a set S of n numbers and a target number C. you are given a set S of n numbers and you want to know if the set can be divided into two sets so that the sum of all elements in the first set is equal to the sum of all elements in the second set. The longest path problem is: Given an undirected graph G(V. Here. you were given that Subset Sum is NPComplete and showed that Set Partition is NPComplete. In the homework. you are given that Set Partition is NPComplete and must show that Subset Sum is NPComplete.E) of n vertices and a positive integer k. Assume that you know the Set Partition problem to be NPComplete. 2) Show that the decision version of the longest path problem is NPComplete. . You can use the knowledge that the Hamilton Circuit problem is NPComplete. Note that this is backwards from the homework problem. you want to know what subset of S sums to exactly C.
. the cost of a rental from i to j is known. Give a dynamic programming algorithm to determine the minimum cost of a trip by canoe from each possible departure point i to each possible arrival point j. However.. A person has only one supervisor. fourth . b) How can the professor ensure that the president gets invited to his own party? 2) Trading Posts There are n trading posts along a river numbered n. In this case you can return the first canoe at some post k between i and j and continue your journey in a second (and. n 1. The goal should be to maximize the sum of the conviviality ratings of the guests. it can happen that the cost of renting from i to j is higher than the total costs of a series of shorter rentals. The personnel office has ranked each employee with a conviviality rating. which indicates how fun that person will be at the party. There is no extra charge for changing canoes in this way.j]. the president does not want both an employee and his or her immediate supervisor to attend.Dynamic Programming 1) Planning a Party Professor McKenzie is consulting for the president of AB corporation.. For your dynamic programming solution. … 3. (It is impossible to paddle against the river since the water is moving too quickly). that is. C[employee] is a real number. third. C[employee]. At any of the posts you can rent a canoe to be returned at any other post downstream. It is C[i. Analyze the running time of your algorithm in terms of n. For each possible departure point i and each possible arrival point j (< i). maybe. 2. the supervisor relation forms a tree rooted at the president. which is planning a company party.. using up to each intermediate trading post. focus on computing the minimum cost of a trip from trading post n to trading post 1. In order to maximize the party atmosphere. 1. a) Describe an algorithm to make up the guest list. The company has a hierarchical structure.) canoe.
3. D1. D0 is always one cent. To get you started. D3 and D4. Coin Changer In the year 2012. D1 through D4 could be arbitrary amounts (e. where N is the amount of change: C[N] = 0 if N = 0 . the US Congress passes an amendment allowing foreignborn citizens to become president and Arnold Schwarzenegger is elected president of the United States. The greedy strategy is to simply dole out the largest of each coin possible. to give 40 cents in change. perhaps D1=5. Devise a dynamic programming formulation that determines the fewest number of coins to return as change for coin denominations D0. To commemorate his achievement a new 20 cent coin is created in his honor: While a great honor to president Schwarzenegger. D2=10. if no change to return. coins is zero You complete the dynamic programming formulation for N > 0: C[N] = if N > 0 . the new coin means that we can no longer use the greedy strategy to return change with the fewest number of coins. the minimum number of coins to return is denoted by C[N]. However. D3=20. 1 dime. For example.g. and 1 nickel. D2. the greedy strategy would return 1 quarter. minimum number of . and D4=25 cents). we could instead dole out 2 Schwarzeneggers which would be the fewest number of coins possible.
2) Nuts and Bolts You have a bucket that contains exactly n nuts and n bolts.. If the nut is too small for the bolt it won't go on at all. the nuts may be defined as N[1. but you can’t compare nuts to nuts or bolts to bolts.max2) Let T(n) be the number of comparisons needed using this algorithm. For each nut in the bucket. In other words.n. Justify that your solution is O(nlgn) by providing a recurrence relation and solving it using any method. too. You want to find the nut that fits each bolt. min2. you can see if a nut is <. there is exactly one bolt in the bucket that fits it. Find a recursive equation for T(n) and solve it using any method you like. mid+1. and vice versa. and you can tell that. a(2) … a(n) . or > a bolt. It works as follows: FindMinMax(A. It is invoked via: FindMinMax(A.e.Recurrence Relations and Algorithm Design 1) Min/Max Finding Given an array A of n real numbers. you can tell that. highindex.max).n] and the bolts as B[1. and you can tell that. . If you cannot think of a O(nlgn) solution you can get half credit by describing a O(n2) solution. min1. Describe an expected (i. average case) O(nlgn) algorithm to solve the Nuts and Bolts problem. highindex. The only technique that you may use to assess the relative sizes of the nuts (and bolts) is to try to screw a particular nut N[i] onto a particular bolt B[j]. If the nut is too big for the bolt it will go on loosely.n].. max1) FindMinMax(A.min2) Max Maximum(max1. &min. max2) Min Minimum(min1. mid. lowindex. a(1). And if the nut exactly fits the bolt. lowindex.min. &max) If lowindex=highindex then Max=A(lowindex) Min=A(lowindex) Return lowindex + highindex mid ← 2 FindMinMax(A. More formally. =.1. the algorithm below is to find the maximum and minimum of these n numbers.
Algorithm 1 23 12 12 2 2 2 2 2 2 2 Algorithm 2 12 12 2 2 2 2 2 2 Algorithm 3 12 94 77 52 39 23 12 9 2 Algorithm 4 12 12 02 39 02 09 02 52 12 94 23 23 23 94 39 77 77 52 52 39 77 09 09 94 39 39 39 39 23 12 2 2 9 2 77 52 9 9 9 9 12 12 94 12 12 12 12 2 23 23 23 23 23 23 23 2 39 39 39 39 77 2 2 2 52 52 52 52 52 52 52 9 77 77 77 77 77 77 9 9 94 94 94 94 94 94 94 39 39 12 12 12 12 12 9 2 2 39 39 23 23 23 12 94 94 94 94 39 39 39 23 23 23 23 23 94 77 52 39 77 77 77 77 77 94 77 52 52 52 52 52 52 52 94 77 9 9 9 9 9 9 9 94 9 9 9 9 9 9 9 9 9 9 2 2 2 12 12 12 12 12 12 12 12 23 23 23 23 23 23 23 23 23 77 77 77 77 77 77 77 77 77 39 39 39 39 39 39 39 39 39 39 77 . Each line does not necessarily represent the next step in the algorithm. Key elements are depicted in bold or underline. but only some later step during the course of the algorithm’s execution. Identify each algorithm and provide a brief rationale for each answer to make it clear that you did not just guess at a sorting algorithm.3) Sorting Below are the contents of an array as various algorithms are sorting it.
. A lab session cannot hold more than 25 students. 5) Lab Section Scheduling 100 students have registered for CS201. Guess T(n)=O(n). A student assigned to their first choice is worth 10 points. n c) T ( n ) = 9T + n 4 T (1) = 1 Use the master theorem. 2 T (1) = 1 Use induction. and the third choice is worth 2 points. An assignment outside the top three is worth 0 points.4) Recurrence relations Give asymptotically tight BigO bounds for the following recurrence relations. a) T (n) = T (n − 2) + 1 T (n) = 1 for n ≤ 2 Use iterative substitution method n b) T (n) = 2T + 1. Each student has specified three choices for their lab section. A “happiness” rating is assigned to each choice. the second choice is worth 5 points. Each student must attend one of 5 lab sessions. Justify your solution with the indicated method for solving the recurrence relation. Describe how students can be assigned to lab sections so that the happiness of all students is maximized while not exceeding the capacity of any individual lab section.
show where the mismatch would occur and then how the pattern would be shifted and what characters would be compared next using the KnuthMorrisPratt algorithm. the naïve algorithm would shift the pattern over one character and resume comparisons at the underlined characters: Text: Pattern: abbacabbab abbab a) For the same initial lineup of the pattern and text.6) String Matching Give the pattern "abbab" and the text "abbacabbab": Initially the pattern and text would line up as follows: Text: Pattern: abbacabbab abbab Using the naïve algorithm. b) For the same initial lineup of the pattern and text. show where the mismatch would occur and then how the pattern would be shifted and what characters would be compared next using the Boyer Moore algorithm. . there would be a mismatch at the characters in bold: Text: Pattern: abbacabbab abbab Next.
For example. As you run through the algorithm. and Aunt Kiki. yourself. Use Dijkstra's Algorithm to compute how long it will take gossip to travel from you to everyone else in your social network. Mom spreads gossip daily to your sister. put a number next to each vertex in the order that you extract them from the minqueue. Next to each vertex you should also give the final distance/cost to the source vertex (You). This data has been compiled into the graph below. 1 You 1 1 1 Mom Mary 3 Sister 42 1 3 7 Uncle Jed 19 4 Aunt Kiki 2 Uncle Ned 2 Bill . She only gossips to Uncle Ned every three days and to Uncle Jed every 42 days.7) GossipNet Through years of meticulous observation you have determined how many days it takes on average for gossip to travel between members of your family and friends. and the parent vertex that is necessary to recreate the actual shortest path.
This action might not be possible to undo. Are you sure you want to continue?