Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CPSC 320 2021W2: Assignment 2

 

1    Statement on Collaboration and Use of Resources

To develop good practices in doing homeworks, citing resources and acknowledging input from others, please complete the following. This question is worth 2 marks.

1. All group members have read and followed the guidelines for groupwork on assignments in CPSC 320              (seehttps://www.students.cs.ubc.ca/~cs-320/2021W2/index.php?page=assignments&menu=1&submenu= 3).

 Yes               No

2. We used the following resources (list books, online sources, etc. that you consulted):

3. One or more of us consulted with course staff during office hours.  Yes               No

4. One or more of us collaborated with other CPSC 320 students, none of us took written notes during our consultations and we took at least a half-hour break afterwards.

 Yes               No

If yes, please list their name(s) here:

5. One or more of us collaborated with or consulted others outside of CPSC 320, none of us took written notes during our consultations and we took at least a half-hour break afterwards.

 Yes               No

If yes, please list their name(s) here:

6. One or more of us collaborated with or consulted others CPSC 320 students or people outside of CPSC 320, but we took written notes during our consultations or we did not take at least a half-hour break afterwards.

 Yes               No

If yes, please list their name(s) here:


2    Recurrence relations

1.  [5 points] Give a tight upper bound (O) on the solution of the following recurrence relation.

T(n) = (T(⌊⌋) + log2 n

if n ≥ 2

if n = 1


Hint:  in order to determine how many times one can take the floor of the square root of n before it becomes equal to 1, write n as a power of 2 and consider what happens to its exponent when you take

the square root.

let n = 2x, then

T(n) = T(2x ) = 2T(⌊√2x ⌋) + log22x   if x ≥ 1

= 2[2T(⌊√2x − 1 ⌋) + log22x − 1] + log22x

= 2[2T(⌊√2x − 1 ⌋) + x − 1 + x

= 4T(2x −2) + 2x -1


2.  [5 points] Give a tight bound (Θ) on the solution of the following recurrence relation. Assume that a, b and c are constants and that a < cb . To simplify the algebra, you may assume that n is a multiple of b.


T(n) = (T(n − b) + cn

if n ≥ b

if n < b


3    Significant inversions

Recall the problem of computing the number of inversions from the assigned readings:  we are given a sequence of n numbers a1 , a2 , . . . , an, which we assume are all distinct, and we want the compute the number of inversions: pairs i < j such that ai > aj. The number of inversions is a measure of how different two orderings are. However, one might feel that this measure is too sensitive. Let us call a pair a significant inversion if i < j and ai > 2aj .

1.  [6 points] Give an O(nlog n) algorithm to count the number of significant inversions between two orderings.


4    Rank by element, rather than element by rank

In worksheet #4, we will describe an algorithm QuickSelect to find the kth smallest element of an unsorted array.  In this problem, we are considering the converse problem: given a value x, find the rank of x in a collection of data. For instance, if we have n elements, the rank of the smallest element of the set is 1, and the rank of the largest element of the set is n.

1.  [1 point] Give a simple O(n) time algorithm to solve this problem in the case where the input consists of an unsorted array A with n elements, and the value x.

2.  [3 points] Now assume that we want to answer q such queries, with the same array A each time but different values of x.

(a) Describe a way to answer these queries that will be more efficient than your algorithm from the previous question, at least for some values of q .

(b) Give a tight bound on the worst-case running time of a sequence of q queries using your algorithm from the previous part, as a function of both n and q .

(c) For which values of q is this more efficient than calling the algorithm from question 1 repeatedly?

3.  [5 points] Suppose now that the collection of values we have is dynamic, instead of being a static array A:  we will be inserting new values, and deleting old ones.  In CPSC 221, you learned that we could store this data in a balanced binary search tree (such as an AVL tree) so every insertion and deletion operation takes in O(log n) time, where n is the size of the tree.  So assume that we have a balanced binary search tree containing our data, and that in addition to the usual fields each node N also contains the size of tree rooted at N .

Describe an efficient algorithm that takes in such a tree T and a value x, and returns the rank of x. What is the worst-case running time of your algorithm?

Note:  you can think of the Quicksort algorithm as implicitly constructing a binary search tree:  the pivot is the value stored at the root of the tree, the array Lesser contains the elements of the left subtree of the root, and Greater contains the elements of the right subtree.


5    Lies, spies and double-agents (oh my!)

The intelligence agency for the small but highly agressive country of Ratelland consists of n spies, any one of whom might have been bought or in some other way suborned by another country. When two spies meet for one hour, each one will be able to determine whether or not the other spy is still faithful to Ratelland. A spy that is faithful to Ratelland will correctly report whether or not the other spy is faithful. However a spy that is working for another country may freely report “faithful” or “unfaithful” regardless of the other spy’s loyalty.

The spy agency wants to determine which spies are still loyal to Ratelland and which ones are now working for the enemy.  The head of the agency is a bureaucrat, not a spy, and so is unable to determine a spy’s true loyalty by talking to him/her/them.  However they can listen to what the spies under their command report (whether truthfully or not), and are confident that more than n/2 of the spies are still

faithful to Ratelland.

1.  [1 point] Imagine that the head of the spy agency has found one spy who is definitely faithful to Ratelland. Describe an efficient algorithm that will identify all faithful spies, and state the worst-case number of one on one meetings required by your algorithm.

2.  [1 point] Fill in the following table with the possible conclusions about the faithfulness of two spies A and B that one could derive from a one to one meeting between them.

Spy A says

Spy B says

Conclusion

B is faithful     B is faithful     B is unfaithful B is unfaithful

A is faithful     A is unfaithful A is faithful     A is unfaithful

 

3.  [6 points] Give an algorithm that, reduces the problem to one of nearly half the size using a single round of meetings. That is, your algorithm should produce a set of no more than n/2 + k spies, more than half of whom are faithful, for some small integer k.  During a round of meetings, a number of encounters between pairs of spies take place.  No encounter involves more than two spies, and no spy can participate in more than one encounter per round. For instance, if n = 10, then in one round spy 1 might meet with spy 4, spy 2 might meet with spy 6, spy 5 might meet with spy 9, and spies 3, 7, 8 and 10 might not meet with anyone.

Hint: The algorithm I have in mind can be stated simply.  The only tricky part is the last statement in the algorithm, which should be “If there is a spy who has not participated in one of the one-hour chats, and some simple condition on the number of pairs where both spies said the other was faithful, then discard that spy”. You will need to decide what condition to use in the sentence in italics (look at some examples!).

4.  [4 points] Prove that your algorithm from part (3) will retain no more than about half of the spies AND that more than half of the remaining spies are faithful to Ratelland.  Getting all the cases right is tricky, but there will be part marks if you get some of them right, and it’s the reason why this part is only out of 4.

5.  [4 points] Show that the spies faithful to Ratelland can be identified with Θ(n) one-hour chats, assuming that more than n/2 of the spies are faithful. Give and solve the recurrence that describes the number of one-hour chats.