You are on page 1of 5

1.

(5 points) Using the mergesort pseudocode in our textbook, sort the numbers 13, 22, 57, 99, 39, 64, 57, 48, 70
in the ascending order. You should describe your answer as Figure 5.2 of our textbook. Note that our
textbook’s example has 8 numbers. However, this problem has 9 numbers. Thus, you should read the
pseudocode carefully and identify how it sorts the 9 numbers. Note that the second “copy” statement of the
Mergesort algorithm in the textbooks has a typo: for odd number of elements, the second copy should have
one more element than the first copy.
[Note: If it is difficult to draw the operation on the word file, draw it on paper by hand. Then take a picture of the
paper and attach the picture here.]
2. (15 points) Assume that you want to solve the Traveling Salesman Problem for a graph given below. Your
starting vertex is the vertex (a).
2
a b
1
8 3
5
d c
7

(a) Present a travelling path to solve the problem.

a→b→d→c→a

(b) Based on the travelling path of your answer to the question (a), present the travelling distance (or cost) of the
path.

15

(c) Do you have only one optimal solution for the graph? (Yes/No). If your answer is no, present other optional
path(s).

No, it is a reverse of the first optimal solution

a→c→d→b→a

3. (20 points) (a) Assume that you are going to develop an algorithm to find the position of the largest number in
an array with n integer numbers using a divide-and-conquer technique. For example, if your algorithm has an
input array such as 1, 3, 11, 7, 5, 6, 4, 9, your algorithm should return 2 because the index 2 has the largest
number (= 11) in the array. Describe the basic idea of your divide-and-conquer algorithm in English.

Answer:
1. create a function that takes 3 parameters: array of numbers, starting index, ending index
2. create base case when the algorithm should stop: in our case this would be when start index and end
index are the same that means in array just one element and this element is the largest.
3. Use divide-and-conquer technique to get two indexes and compare elements at that indexes: divide array
in half first, then divide each part in half recursively till reach the base case in each halves.
4. Compare elements at indexes getting after recursive calls. Return the index of a greater element.

(b) Based on the basic idea of (a), write a pseudocode of your divide-and-conquer algorithm. Note that the array
index of your algorithm should start from zero.

Answer:

int largestElementIndex(int A[], int start, int end){


if( start == end){
return start;
}else{
index1 ← largestElementIndex(A, start, (start+end)/2);
index2 ← largestElementIndex(A, ((start+end)/2 +1), end);
if ( A[index1] >= A[index2]){
return index1;
}else{
return index2;
}
}
}

(c) Determine the time complexity of your algorithm. Set up a recurrence relation and apply the Master Theorem
to calculate the time complexity. So, you should provide the values of symbols “a”, “b”, and “d”. And then
provide the time efficiency of your algorithm

Answer:

T(n) = 2T(n/2) + 1;
a = 2, b = 2, d = 0
2 > 2^0
T(n) = thetha( n^log(base b) a) = thetha(n^log(base2) 2) = thetha(n), third case of Master theorem a>b^d

5. (5 points) Consider the quicksort algorithm covered in the class. Present the first partitioning
operation for the list “6 10 13 5 8 3 2 11 ”, You should use the first number, 6, as a pivot for the
partitioning. In your answer, you have to present the indexes i and j clearly.
4. (10 points) (a) Find gcd(270, 192) by applying Euclid’s algorithm as we covered in the class. Present the
intermediate steps of the algorithm clearly.

Answer:
gcd(270, 192)
remainder = 270 mod 192
gcd(192, 78)
remainder = 192 mod 78
gcd(78, 36)
remainder = 78 mod 36
gcd(36, 6)
remainder = 36 mod 6
gcd(6, 0)
gcd = 6;

5. (10 point) This is the Euclid’s algorithm to find the greatest common divisor (gcd).

Determine the return value for the Euclid (60, 25) using the algorithm. Show the intermediate steps.

Euclid (60, 25)


Euclid (25,60% 25)
Euclid (25, 10)
Euclid (10, 25%10)
Euclid (10, 5)
Euclid (5, 10 %5)
Euclid (5, 0)
n=5

1. (10 points) Suppose you have three jars, A, B, and C, in a room. Jar A has 5 large black balls, 4 large red balls,
and 3 large green balls. Jar B has 5 small black balls, 4 small red balls, and 3 small green balls. Jar C is empty.
Now, you will pick a few balls from the jar A in the dark and place them in the jar C. After that, you will pick a
few balls from the jar B in the dark and place them in the jar C. Note that the color of the selected balls at the
jars A and B cannot be confirmed because the surroundings are dark. Also, the numbers of balls selected from
the jars A and B need not always be the same. Once you're done, you can turn on the lights in the room and see
the balls in the jar C.

(a) Assuming the best case occurs, what is the minimum number of balls you have to choose to get a matching
pair? Here, a matching pair means that there must be one large ball and one small ball of the same color in the
jar C. But the color itself of the pair is not important. Present just the number of balls. You don’t need to
explain your answer.

(b) Assuming the worst case occurs, what is the minimum number of balls you have to choose to get a matching
pair? Here, a matching pair means that there must be one large ball and one small ball of the same color in the jar
C. But the color itself of the pair is not important. Present just the number of balls. You don’t need to
explain your answer.

11
4. (10 points) A detachment of n soldiers must cross a wide and deep river with no bridge in sight.
They notice two 12-year-old boys playing in a rowboat by the shore. The boat is so tiny that it can only
hold two boys or one soldier. Note that the boat needs at least one boy or one soldier to pass from one
shore to another.
(a) How can the soldiers get across the river? Answer:
1. 2. 3. 4. 5. 6. 7. 8. 9.
First boy goes in a boat to the shore with solders, second boy stays at other shore side First solder goes to the
other shore side Second boy goes in a boat to the shore with remaining solders Two boys return to the other
side of the shore.
First boy goes in a boat to the shore with solders, second boy stays at other shore side Second solder goes to
the other shore side Second boy goes in a boat to the shore with remaining solders Two boys return to the other
side of the shore.
.........
(b) How many times need the boat pass from shore to shore?
Answer: 4*n

You might also like