Beruflich Dokumente
Kultur Dokumente
Homework 3
Due: 05/14/2019 (Tuesday) (11:55 PM)
ID: 1010
2. Consider the following recursive algorithm. What does this algorithm compute?
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.]
For the homework, solve the following recurrence relation as described above. You have to present intermediate
steps and time complexity as described above.
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.]
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:
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.