Sie sind auf Seite 1von 5

CST 370 – Summer A 2019

Homework 3
Due: 05/14/2019 (Tuesday) (11:55 PM)

Name: William Barajas

ID: 1010

1. Consider the following recursive algorithm.


Algorithm Q(n)
// Input: A positive integer n
if n = 1 return 1
else return Q(n - 1) + 2 * n - 1

(a) Present the return value for Q(1).


1

(b) Present the return value for Q(2).


4

(c) Present the return value for Q(3).


9

(d) Determine what the algorithm computes for a general number n.

The answer will always be 1 because of its recursive nature.

2. Consider the following recursive algorithm. What does this algorithm compute?

The algorithm computes the smallest element in the array.

3. Describe an efficient algorithm for finding all common elements in two sorted lists of numbers. For
example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. As another example, for the lists
20, 30, 50, 70, 90, 100 and 10, 20, 30, 50, 80, the output should be 20, 30, 50.
You should describe your basic idea in English clearly.
[Hint: You can do this task in the linear time.]

You would need 2 arrays a and b for both lists


a ←sorted array
b ←sorted array
i ←0 // you will also need two separate numbers that will increment as you go through the lists
j ←0
//then we would use a while loop
while(i< length of a and j<length of b)
if a[i] == b[j] output a[i] and increment i and j
else if a[i]>b[j] j++//you increment j
else i++ //you increment i
4. In the class, you learned the recurrence relation and backward substitution to get the time complexity of a
recursive algorithm. To remind the topic, read the Google document again at HYPERLINK
"https://goo.gl/HmoUNQ" https://goo.gl/HmoUNQ
Now, this is the example of backward substitution you learned in the class.

M(n) = M(n-1) + 1 // Recurrence relation


M(0) = 0 // Initial condition

M(n) = M(n-1) + 1 // Replace M(n-1) with “M(n-2) + 1”


= [M(n-2) + 1] + 1
= M(n-2) + 2 // Replace M(n-2) with “M(n-3) + 1”
= [M(n-3) + 1)] + 2
= M(n-3) + 3

= M(n-i) + i

= M(n-n) + n
= M(0) + n
=0+n
= n  (n)

For the homework, solve the following recurrence relation as described above. You have to present intermediate
steps and time complexity as described above.

M(n) = 2*M(n – 1) // recurrence relation


M(1) = 3 // initial condition
M(2) = 2(2M(n-2)+1)
=4M(n-2)+2
M(3)= 4(2M(n-3)+1)+2
=[8M(n-3)+4]+2
=8M(n-3)+6

M(2n-1)+1

2n-1

5. Consider the following recursive algorithm.

Algorithm Q(n)
if n = 1
return 1
else
return Q(n - 1) + 2 * n – 1

(a) Set up a recurrence relation for the number of additions made by this algorithm.
T(n) = T(n-1) +1

(b) Provide an initial condition for the recurrence relation you develop at the question (a).
T(1) = 1

(c) Solve the recurrence relation of the question (a) and present the time complexity as described at the
question number 1.
T(n) = T(n-1)+1
T(n-2)+1+1
T(n-3)+1+1+1
T(n)+1//+1 n amount of times
T(n) = O(n)

6. Consider the following recursive algorithm. Set up a recurrence relation and an initial condition for the
comparison operation “if n = 1” in the line number 3.
[Hint: Use C(n) to indicate the number of comparisons for the array A with the size n. Then, the initial condition
would be either C(1) or C(0).]

C(n) = C(n-1) + 1
C(1) = 1

7. Write a C++ program called power.cpp to compute 2n for a nonnegative integer n. In this program, you have
to develop a recursive function to calculate it. And also, you can’t use a library in the program.
For the problem, you can assume that the user always enter a correct integer number which is greater than or
equal to zero.
[Hint: Use the formula: 2n = 2n-1 + 2n-1 for the recursive function.]

The following presents a sample run of the C++ program.


Enter a number: 3
Result: 8

This is another sample run.


Enter a number: 0
Result: 1

8. Consider an equal group problem: given n positive integers, partition them into two disjoint groups with the
same sum of their elements. Of course, the problem does not always have a solution. In the homework, you
should write a C++ program called equal_group.cpp to solve the problem. In the program, you can assume that
the max number of input integer values is 15. And also, you can assume that the input values are all distinct.
This is a sample run of the program on the cloud9:

$ g++ -o equal_group equal_group.cpp


$ ./equal_group
Number of input: 4
Enter 4 numbers: 1 2 3 4
Equal Group: 1 4 vs 2 3
Done.

This is another sample run:


$ g++ -o equal_group equal_group.cpp
$ ./equal_group
Number of input: 3
Enter 3 numbers: 2 7 9
Equal Group: 2 7 vs 9
Done.

This is the last sample run:


$ g++ -o equal_group equal_group.cpp
$ ./equal_group
Number of input: 5
Enter 3 numbers: 2 4 6 8 10
Equal Group: Not exist
Done.

When you display the solution, the output sequence is not important. In other words, the following can be the
answer to the first sample execution:
Equal Group: 1 4 vs 2 3

or
Equal Group: 1 4 vs 3 2

or
Equal Group: 3 2 vs 4 1

For the problem, if more than one partition is available, it’s good enough for you to display only one case.

[Hint]: You can use the binary representation from 0 to 2n – 1 for the number of input n to represent a group. For
example, if the number of input is 3 as the second sample run, all binary numbers from 0 to 2 3 – 1 (= 7) will be
000, 001, 010, 011, 100, 101, 110, and 111. Try to find a correspondence between a group and its binary
representation. For instance, the binary number “000” can be used to indicate the “empty” group while “001”
means the group with the last number {9}. Similarly, you can interpret the binary number “010” means the group
with the second number {7}, and the binary number “011” for the group with the second and third numbers {7,
9}. This way, you can consider all possible groups.

How to turn in? Write your answer to the questions 1, 2, 3, 4, 5, and 6 using the Microsoft Word file and submit
it on the iLearn. You should also submit your two C++ files on the iLearn. Thus, you have to submit three files
(one MS-Word file and two C++ source files) on the iLearn.

Page PAGE 1 of NUMPAGES 4

Das könnte Ihnen auch gefallen