Midterm Exam (100 points)
Closed books and notes (except one page of notes)
1. (25 points) Design an algorithm for matching four pairs of symbols in java:
/* and */, ( and ), [ and ], cfw_ and . The text between /* and */ also need t
Homework #30 sample solution:
Problem: Dense Subgraph
Input: A graph G, and integers k and y.
Output: Does G contain a subgraph with exactly k vertices and at least y edges?
To prove Dense Subgraph is NP-complete, we show that Dense Subgraph is in NP
Homework # 29 sample solution:
a. We prove that Low Degree Spanning Tree problem is NP-hard by reducing
the Hamiltonian Path problem to it.
Low Degree Spanning Tree
Instance: A graph G and an integer k.
Question: Does G contain a spanning tree such th
Homework #10 sample solution:
(a) It does not matter. Finding the maximum element in both structure needs
O(1) time since the maximum element is just the root of max-heap and
the maximum is either the first or the last element of a sorted array.
HW#9 Sample Solution
sort algorithm to sort the array of size 2n in descending order. The time
complexity is O(nlogn).
Now, divide the players as fair as possible:
First, add the best player i
Homework #7 sample solution:
3-13. We construct a binary search tree and place all the elements of array
on the leaf nodes. For each node, we also place the number of leaf nodes and
the sum of the values of leaf nodes in the subtree rooted by this node. T
Sample Solution for Homework 6, 22c:31:2
3-8. We could use two extra pointers to maintain the minimum and maximum
respectively which ensures minimum and maximum take O(1) time. Each time
you do an insert, compare the value to be added with both the minimu
Homework 5 sample solution:
(a) Suppose we have an array that is filled at half of its size. If one
element is deleted, the array will be cut in half. If then an element is
added, it has to expand. Repeatedly doing deletion and insertion gives a
Homework 4 sample solution:
Label each bag with number from 1 to 10. Place 1 coin from bag
#1, 2 coins from bag #2, 3 coins from bag #3 10 coins from bag #10
on the scale. The total number of coins is 55. If all coins are good, this
would weigh 550
1. Problem 2-25
g(n) = ln(n)
g(n) = n
g(n) = n log(n)
g(n) = n log(n)
2. Problem 2-38
Suppose A, B are two n-digit numbers.
for i :=1 to n do
for j := 1 to n do
multiply jth digit of B by ith digit of A
So the time complexity o
1. Problem 2-1:
j =i+1 j
i=1 (n + n i i)/2
= (n n + i=1 i n=11 i)/2
= (n3 n + (n(n + 1)(2n + 1)/6 n2 ) (n2 n)/2)/2
= O(n3 )
2. Problem 2-19:
log log n
log n, ln n
Suppose S [1.n] is the integer array storing the set S = cfw_s1 , ., sn , T is the target
integer and X is the solution set.
1. First-Fit algorithm
first-fit(S, T) cfw_
X = emptyset;
for (i = 0; i < n; i+)
if (S[i] <= T) cfw_ T -= S[i]; inset i
CS:3330 (Prof. Pemmaraju ): Assignment #3 Solutions
(a) Consider the following algorithm with input being an adjacency list representation L of the given
Algorithm 1 NeighborhoodDeg(L)
1: Initialize degree to be an array of si
CS:3330 (Prof. Pemmaraju ): Assignment #2 Solutions
Consider the following algorithm that uses brute force maximum independent set algorithm on each connected
Algorithm 1 Maximum Independent Set Algorithm
1: Find all conne
CS:3330 Exam 1, Fall 2015
Thursday, Sept 24 2015, 6:30 pm to 8:30 pm
1. Here are two problems on understanding the growth rate of functions that represent running
times of algorithms and the use of asymptotic notation.
(a) Take the following list of funct
CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions
(a) Consider input with n men m1 , m2 , . . . , mn and n women w1 , w2 , . . . , wn with the following preferences:
All men have the same preferences for women: w1 > w2 > > wn .
Homework#11 sample solution:
The way to ensure mergesort is a stable sorting algorithm is that each
time you compare two values within a sublist, instead of using >=, you
should always use > to decide weather or not to swap two values. To
Homework#12 sample solution:
Initially, we group red and white together, meaning (red = white), and
blue > red = white. Then apply quick sort on the array to separate blue
from red and white using blue as pivot. The last step is to separate red
Homework #13 sample solution:
Topological sort of the graph G is:
A, B, D, E, C, F, H, G, I, J
(If you use the online version of the textbook, the graph for this problem has a loop.)
We could use DFS combined with post-order traversal to evaluat
Homework # 28 sample solution:
9-1. We use for AND and + for OR.
(x + y + a) (
) ( + w + c)( + u + )( + y + d)( + z + e)(+ + f)(+ u + v)(x + + g)( + + h)(
+ w + i)( + u + )(x + + j )( + x + )
Instance: A graph G = (V, E), and integer
Homework # 26 sample solution:
8-13 Lets assume that
ES is the table of m text strings.
D is the data string of n characters.
L[i] stores the minimal number of encoding strings to encode the last n i + 1
characters of D.
The recurrence relation
L[i] = c
Homework # 25 sample solution:
Suppose a[1.n] is an array of n numbers, in order to form a circle, we will copy
the subarray a[1 n-1] to the end of a[1.n] to obtain an array of N= 2n 1 numbers.
First we compute the optimal sum A[i] of the subarray en
Homework # 24 sample solution:
part 1: LCS
First we use the dynamic programming technique to compute the length of the LCS of two input strings
x (of length m) and y (of length n), which is stored in the array C[0.m][0.n].
int lengthLCS (string x
Homework # 23 sample solution:
(1) cchocohilaptes is a shuffle of chocolate and chips
cchocohilaptes red highlighted chocolate, green highlighted chips
(2) chocochilatspe red highlighted chocolate, unable to highlight chips
since s and p out of order.
Homework # 21 sample solution:
Since a given number has 3 or 4 possible candidates except that # 1 and # 0 has
none, we just need to construct all permutations with a given digit sequence and possible
Assume that the input data is an arr
1. 7-8 MAX-SAT In the Maximum Satisability (MAX-SAT) problem one looks
for an assignment of truth values to all variables that maximizes the number of
satised clauses of a boolean formula in conjunctive normal form.
Let be a boolean formula in
Sample solution #19
The following algorithm will work both for a multiset or distinct set of elements.
Step 0: Suppose the elements are numbered from 1 to n. Initially the elements are stored in an
array A with indices 1 to m, in the sorted o
Homework #18 sample solution:
For each vertex in the tree, we use BFS algorithm to find the last discovered vertex,
the path between these two is the longest distance of this current vertex. Then we