COMP2119 Assignment 4
COMP2119 Assignment 4
Part A
Submit your answers for Part A only. Unless stated otherwise, assume all logarithmic functions
are in base 2.
1. The flats are arranged in some linear order and are allocated to residents according to the
stated order.
2. A flat will not be allocated to any person unless the flats in front of it have all be allocated.
3. Residents queue up in a waiting list. The resident at the 1st position of the waiting list
can check the first available flat. If the flat matches his/her preference, he/she will accept
the flat and leaves the waiting list. Or else, he/she will not accept the flat and will be put
at the end of the waiting list waiting for another allocation chance.
Suppose n residents are waiting for a flat, and there are m flats available, where n and m are
positive integers. You want to find the number of people who are not allocated a flat at last.
The flat data is saved in a list F , which stores the types of the n flats as “A” or “B” in order.
The resident waiting list data is saved in a list R, which stores the housing preference of the n
residents in the waiting list as “A” or “B”.
Example:
Suppose F = [A, A, B, A], R = [B, A, B]. The number of people who cannot have a flat is 2.
Explanation:
• The 1st resident in the current waiting list does not want the 1st flat available. Therefore,
he/she goes to the back of the waiting list and the waiting list becomes [A, B, B]. The list
of flats is still [A, A, B, A].
• Now, the 1st resident in the waiting list wants Type A, which is the same type of the 1st
flat available. Therefore, he/she takes the flat and the waiting list becomes [B, B]. The
list of flats becomes [A, B, A].
• As both residents want Type B flat, it is impossible for them to take the 1st flat available
in the list of flats (which is Type A). Therefore, the number of persons who cannot have
a flat is 2.
1
(a) By using stack and/or queue, write the pseudo-code to complete function noF lat(F, R)
that returns the number of persons who are not allocated a flat.
Notes:
• You are not allowed to access the contents in the queue and/or stack directly.
• You can only use push, pop, top operations for a stack.
• You can only use enqueue, dequeue, f ront operations for a queue (f ront method
returns the element at the front of the queue without dequeuing it).
• Other than stack and queue, any variables you declare should only use O(1) space
in total.
• You are not allowed to change the contents in the lists F and R.
(b) Explain how your algorithm in (a) works and analyze the time complexity of it.
(a) A hash table of size 13 with hash function h(k) = k mod 13. Collisions are handled by
open addressing with linear probing, using the function f (i) = 4i
(b) A hash table of size 5 with hash function h(k) = k mod 5. Collisions are handled by
chaining.
(c) A hash table of size 13 with double hashing using the probe sequence h(k, i) = (h1 (k) +
i h2 (k)) mod 13, where h1 (k) = k mod 13 and h2 (k) = 5 − (k mod 3).
2
Question 3 - Find the Most Valuable Object [30%]
You have a group of m objects where each object has a distinct price pi and value vi (1 ≤ i ≤ m,
pi , vi are positive integers). Design a data structure that takes O(m) space to store the objects,
such that it supports the following operation:
Given two prices x and y (x < y), return the most valuable object (i.e., the one with
the highest value) whose price is between x and y (x < pi < y). You may assume
that there is at least one object with the price x < pi < y. The operation should
take O(log m) time.
• The construction method, details (e.g., type of data structure, attributes, etc.) and space
complexity of the data structure.
• The pseudo-code of the algorithm to find the most valuable object. You should also give
brief verbal explanations on it and prove it’s time complexity.
Note that you can directly use conclusions from lecture notes without proof, e.g., from lecture
note xxx, we know...
For example, if there are three players, and get scores [(52,36), (68,67), (60,80)], then the second
and the third players will each have a star. After the fourth player with a score (75,75) joins,
the second player loses its star because he/she is dominated by the fourth player. The third
player, however, is not dominated by the new player and thus will retain the star. The fourth
player will acquire a star because the player is not dominated by anyone.
Design a data structure that will efficiently perform the following operations:
• Insert: Accept the score of a new player (x, y).
If there are currently m players and l players have stars, the operation Insert should have time
complexity O(m log m) (for m inserts), and the operation Report should have time complexity
O(l) (for one report). You can assume all the scores are unique.
Please describe how the data structure is constructed, how the operations are performed, the
rationale to derive the time complexity, and support with brief explanations. The conclusions
from lecture notes can be directly used without proof (with annotation from lecture notes xxx).
3
Part B
For your practice only. Do NOT submit your answer to this part.
(a) Write algorithms for implementing enqueue and dequeue operations using only the stack
operations (isEmpty, push, pop and top).
(b) Starting from two empty stacks, describe the content in them after executing these oper-
ations:
(c) Explain your code step by step in the last operation of (b)(iii), i.e., the enqueue(5) oper-
ation.
Submission
Please submit your assignment (one PDF file) to moodle by the deadline. Make sure the
content is readable. Feel free to post your questions on moodle forum, or contact the TAs if
you encounter any difficulty in this assignment. We are happy to help you!