Docsity

Homework 3 Solutions for Algorithm Design and Analysis | CSE 202, Assignments of Computer Science

Toggle sidebar
bg1
CSE 202 Homework 3 Solution
TA: Nan Zang
The solutions in this homework are not complete. I only gave some directions to solve the problem. For
algorithm problem, you must give the algorithm, analysis of correctness and time complexity.
Problem 1 Exercise 5 - 2
Very similar to the algorithm given in class, and only modify the merge procedure given in the
book.
Problem 2 Exercise 5 - 3
(Divide and Conquer Algorithm)
Definition of Subproblem:
Majority(k, S):
Given k cards, if there are more than k/2 cards equivalent to one another, return true and give the
card number.
Base Case:
If there is only one card available, return true and its number.
If there are two cards available, return false if the two cards are different; return true and its
number, otherwise.
DQ Algorithm
(bool, num) Majority (n, S) // S is the set of numbers; return false or (true, value)
1: If(n =1) return true and its number;
2: If (n=2) return false if the two cards are different; return true and its number, otherwise.
3: Else{
//For simplicity, I consider the case that n is even.
4: Split the cards into two equal sets S1 ={1, 2, ….., n/2} S2 = {n/2+1, …, n};
(b1, value1) = Majority (n/2, S1); //b1,value1 are the return values from subproblem 1
(b2, value2) = Majority (n/2, S2); //b2,value2 are the return values from subproblem 2
5: If(b1 is false and b2 is false)
Return false
6: Else if (b1 is false and b2 is true){
Compare value2 with all the cards in S to see if there are more than n/2 cards equivalent to it.
If there are, return true and value2; else return false.
}
7: Else if (b2 is false and b1 is true){
Compare value1 with all the cards in S to see if there are more than n/2 cards equivalent to it.
If there are, return true and value1; else return false.}
8: Else if (b1 is true and b2 is true){
If (value1==value2) return true and value1;
Else { Compare value1 with all the cards in S to see if there are more than n/2 cards
equivalent to it. If there are, return true and value1; Else Compare value2 with all
the cards in S to see if there are more than n/2 cards equivalent to it. If there are,
return true and value1; else return false.
} //end of Else
} //end of if of line 3
} //end of the procedure
pf3

Partial preview of the text

Download Homework 3 Solutions for Algorithm Design and Analysis | CSE 202 and more Assignments Computer Science in PDF only on Docsity!

CSE 202 Homework 3 Solution

TA: Nan Zang

The solutions in this homework are not complete. I only gave some directions to solve the problem. For algorithm problem, you must give the algorithm, analysis of correctness and time complexity.

Problem 1 Exercise 5 - 2

Very similar to the algorithm given in class, and only modify the merge procedure given in the book.

Problem 2 Exercise 5 - 3

(Divide and Conquer Algorithm)

Definition of Subproblem:

Majority(k, S): Given k cards, if there are more than k/2 cards equivalent to one another, return true and give the card number.

Base Case:

If there is only one card available, return true and its number. If there are two cards available, return false if the two cards are different; return true and its number, otherwise.

DQ Algorithm

(bool, num) Majority (n, S) // S is the set of numbers; return false or (true, value) 1: If(n =1) return true and its number; 2: If (n=2) return false if the two cards are different; return true and its number, otherwise. 3: Else{ //For simplicity, I consider the case that n is even. 4: Split the cards into two equal sets S1 ={1, 2, ….., n/2} S2 = {n/2+1, …, n}; (b1, value1) = Majority (n/2, S1); //b1,value1 are the return values from subproblem 1 (b2, value2) = Majority (n/2, S2); //b2,value2 are the return values from subproblem 2 5: If(b1 is false and b2 is false) Return false 6: Else if (b1 is false and b2 is true) { Compare value2 with all the cards in S to see if there are more than n/2 cards equivalent to it. If there are, return true and value2; else return false. } 7: Else if (b2 is false and b1 is true) { Compare value1 with all the cards in S to see if there are more than n/2 cards equivalent to it. If there are, return true and value1; else return false.} 8: Else if (b1 is true and b2 is true) { If (value1==value2) return true and value1; Else { Compare value1 with all the cards in S to see if there are more than n/2 cards equivalent to it. If there are, return true and value1; Else Compare value2 with all the cards in S to see if there are more than n/2 cards equivalent to it. If there are, return true and value1; else return false. } //end of Else } //end of if of line 3 } //end of the procedure

Time complexity: T(n) = 2T(n/2) +O(n). So, T(n) = O(nlogn).