AMTH 377/COEN 279 TRAN
Name:
FINAL EXAM SOLUTIONS
AMTH 377/COEN 279
DESIGN AND ANALYSIS OF ALGORITHMS
FALL 2104
1. (10 points) Write an O(n) divideconquer algorithm for the following problem:
input: an array A[lo.hi] of n 2 integers;
output: the smallest
Bonus Assignment 2
COEN279 Design and analysis of Algorithm
Department of Computer Engineering
Santa Clara University
1. Time complexity for recursion code:
int FN(int n)
cfw_
If(n>2)
return FN(n1) + FN(n2) else
return 1;

1
2
3
4
Lets take the time
Bonus Assignment 6
COEN279 Design and analysis of Algorithm
Department of Computer Engineering
Santa Clara University
1. Show how to compute the length of an LCS using only 2 * min( m, n) entries in the c table plus
O(1) additional space. Then show how to
Homework 4
10/25/15, 4:29 PM
Homework 4
(due 10/28)
1. (10 pts.) Write an O(n2 ) algorithm to solve the following graph problem:
INPUT: the adjacency lists of a graph G = (V, E) .
OUTPUT: an n n matrix C , where C[i][j] = 1 if there is a path (not edge) f
Homework 3
10/25/15, 4:28 PM
Homework 3 Solutions
1. (10 pts.) Write a divideconquer algorithm to solve the following problem:
INPUT:
A , a sorted list of m integers
B , a sorted list of n integers
k , a positive integers between 1 and m + n inclusive.
O
Homework 3
10/25/15, 4:28 PM
Homework 3 Solutions
1. (10 pts.) Write a divideconquer algorithm to solve the following problem:
INPUT:
A , a sorted list of m integers
B , a sorted list of n integers
k , a positive integers between 1 and m + n inclusive.
O
Homework 2
10/25/15, 4:27 PM
Homework 2
(due 10/14)
1. (10 pts.) There are 22 = 4 pairs of zeroes and ones: (0, 0), (0, 1), (1, 0), (1, 1) , and in general, there are
2n n tuples of zeroes and ones.
a. Write pseucode to generate all n tuples of zeroes a
Homework 2 Solutions
10/25/15, 4:27 PM
Homework 2 Solutions
1. (10 pts.) There are 22 = 4 pairs of zeroes and ones: (0, 0), (0, 1), (1, 0), (1, 1) , and in general, there are
2n n tuples of zeroes and ones.
a. Write pseucode to generate all n tuples of
Homework 1
10/25/15, 4:26 PM
Homework 1
(due 10/7)
1. (10 pts.) Find a closedform formula for M(n) , the number of * performed in the following algorithm:
unsigned Q1(unsigned n)
cfw_
if (n = 0)
return 1*2*3*4;
else
return Q1(n1) * Q1(n1) * Q1(n1) * Q
Homework 1
10/25/15, 4:27 PM
Homework 1 Solutions
1. (10 pts.) Find a closedform formula for M(n) , the number of * performed in the following algorithm:
unsigned Q1(unsigned n)
cfw_
if (n = 0)
return 1*2*3*4;
else
return Q1(n1) * Q1(n1) * Q1(n1) * Q1
Bonus Assignment 5
COEN279 Design and analysis of Algorithm
Department of Computer Engineering
Santa Clara University
Review Question:
1. y is the minimum node of right subtree of z. Thus y.left must be T.nil.
20 (z)
10
30
5
Nil
12
Nil
Nil
25 (y)
Nil
Nil
Bonus Assignment 4
COEN279 Design and analysis of Algorithm
Department of Computer Engineering
Santa Clara University
1. What is the time complexity of function idct_swap()? (10 points)
The function idct_swap() contains an ifelse condition in the beginni
Homework 0 Solutions
1. (10 pts.) Find a closedform formula for C(n), the number of times A[j] < A[right] is
performed in the following algorithm, where n = rightleft+1 is the size of the input
subarray:
int partition(int A[], int left, int right)
cfw_
Homework 1 Solutions
1. (10 pts.) Find a closedform formula for M(n), the number of * performed in the following
algorithm:
unsigned Q1(unsigned n)
cfw_
if (n = 0)
return 1*2*3*4;
else
Q1(n1);
return Q1(n1) * Q1(n1) * Q1(n1) * Q1(n1) *
Answer:
M sa
Homework 2 Solutions
1. (10 pts.) There are 22=4 pairs of zeroes and ones: (0,0),(0,1),(1,0),(1,1), and in general,
there are 2n ntuples of zeroes and ones.
a. Write pseucode to generate all ntuples of zeroes and ones, given n. Answer:
#include <iostrea
Homework 3 Solutions
1. (10 pts.) Write a divideconquer algorithm to solve the following problem:
INPUT:
A, a sorted list of m integers
B, a sorted list of n integers
k, a positive integers between 1 and m+n inclusive.
OUTPUT: the kth smallest value i
Homework 4 Solutions
1. (10 pts.) Write an O(n2) algorithm to solve the following graph problem:
INPUT: the adjacency lists of a graph G=(V,E).
OUTPUT: an nn matrix C, where C[i][j]=1 if there is a path (not edge) from vertex i to vertex
j, or 0 otherwise
Homework 5 Solutions
1. (10 pts.) Write a dynamicprogramming algorithm to solve the following variant of the COIN
CHANGE problem in which the supply of each denomination D[i] is limited to A[i] coins:
INPUT: n, a positive integer; D[1.m], an array of pos
Homework 6 Solutions
1. (10 pts.) We developed in class a dynamicprogramming algorithm to find the length of longest
common subsequences of two given strings. Modify this program to find an actual longest
common subsequence, not just its length.
Answer:
Homework 7 Solutions
1. (20 pts.) Problem 26.1 (Escape problem, p. 760, Cormen, 3rd ed.)
Answer:
a. Suppose we allow vertices in a flow network to have entering capacity as well as edges.
Such modified flow network is described as a 6tuple G=(V,E,s,t,ce,
Bonus Assignment 2
COEN279 Design and analysis of Algorithm
Department of Computer Engineering
Santa Clara University
1. Time complexity for recursion code:
int FN(int n)
 1
cfw_
If(n>2)
 2
return FN(n1) + FN(n2)  3
else
return 1;
 4
Lets take the
Homework 0 Solutions
10/25/15, 4:26 PM
Homework 0 Solutions
1. (10 pts.) Find a closedform formula for C(n) , the number of times A[j] < A[right] is performed in the
following algorithm, where n = rightleft+1 is the size of the input subarray:
int parti
Homework 0
10/25/15, 4:26 PM
Homework 0
(due 9/30)
1. (10 pts.) Find a closedform formula for C(n) , the number of times A[j] < A[right] is performed in the
following algorithm, where n = rightleft+1 is the size of the input subarray:
int partition(int
Homework 4
10/25/15, 4:29 PM
Homework 4
(due 10/28)
1. (10 pts.) Write an O(n2 ) algorithm to solve the following graph problem:
INPUT: the adjacency lists of a graph G = (V, E) .
OUTPUT: an n n matrix C , where C[i][j] = 1 if there is a path (not edge) f
Homework 3
10/25/15, 4:28 PM
Homework 3 Solutions
1. (10 pts.) Write a divideconquer algorithm to solve the following problem:
INPUT:
A , a sorted list of m integers
B , a sorted list of n integers
k , a positive integers between 1 and m + n inclusive.
O
Homework 2 Solutions
10/25/15, 4:27 PM
Homework 2 Solutions
1. (10 pts.) There are 22 = 4 pairs of zeroes and ones: (0, 0), (0, 1), (1, 0), (1, 1) , and in general, there are
2n n tuples of zeroes and ones.
a. Write pseucode to generate all n tuples of
Homework 2
10/25/15, 4:27 PM
Homework 2
(due 10/14)
1. (10 pts.) There are 22 = 4 pairs of zeroes and ones: (0, 0), (0, 1), (1, 0), (1, 1) , and in general, there are
2n n tuples of zeroes and ones.
a. Write pseucode to generate all n tuples of zeroes a
Homework 1
10/25/15, 4:27 PM
Homework 1 Solutions
1. (10 pts.) Find a closedform formula for M(n) , the number of * performed in the following algorithm:
unsigned Q1(unsigned n)
cfw_
if (n = 0)
return 1*2*3*4;
else
return Q1(n1) * Q1(n1) * Q1(n1) * Q1
Homework 1
10/25/15, 4:26 PM
Homework 1
(due 10/7)
1. (10 pts.) Find a closedform formula for M(n) , the number of * performed in the following algorithm:
unsigned Q1(unsigned n)
cfw_
if (n = 0)
return 1*2*3*4;
else
return Q1(n1) * Q1(n1) * Q1(n1) * Q
Homework 0 Solutions
10/25/15, 4:26 PM
Homework 0 Solutions
1. (10 pts.) Find a closedform formula for C(n) , the number of times A[j] < A[right] is performed in the
following algorithm, where n = rightleft+1 is the size of the input subarray:
int parti