0% found this document useful (0 votes)
75 views4 pages

COMP2119 Assignment 4

This document provides details for Assignment 4 of the COMP2119B Introduction to Data Structures and Algorithms course. It includes 4 questions related to data structures, hashing, and trees. Question 1 involves writing pseudocode to allocate housing flats using stacks and queues. Question 2 involves drawing hash tables with different collision handling methods. Question 3 involves designing a data structure to efficiently retrieve the most valuable object within a given price range. Question 4 involves designing a data structure to track players and stars in a game.

Uploaded by

meganyaptan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views4 pages

COMP2119 Assignment 4

This document provides details for Assignment 4 of the COMP2119B Introduction to Data Structures and Algorithms course. It includes 4 questions related to data structures, hashing, and trees. Question 1 involves writing pseudocode to allocate housing flats using stacks and queues. Question 2 involves drawing hash tables with different collision handling methods. Question 3 involves designing a data structure to efficiently retrieve the most valuable object within a given price range. Question 4 involves designing a data structure to track players and stars in a game.

Uploaded by

meganyaptan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COMP2119B Introduction to Data Structures and Algorithms

Assignment 4 - Data Structures, Hashing and Trees


Due Date: Nov 21, 2023 5:00pm

Part A
Submit your answers for Part A only. Unless stated otherwise, assume all logarithmic functions
are in base 2.

Question 1 - Housing is the Top Priority! [20%]


Heung Shing is a city known for its shortage of affordable housing. Residents have to queue up
waiting for getting a flat in the public housing estates. Assume that there are two types of flats
(denoted as Type A and Type B) that are available for allocation. Further assume that each
resident has his/her own preference of the flat type. The government uses the following rules
to allocate flats:

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.

Question 2 - Hash Tables [20%]


Insert the keys [53, 62, 75, 18, 27, 33, 40, 98] into the following hash tables. You only need to
draw the final hash table.

(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.

In your answer, please write clearly on:

• 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...

Question 4 - Shooting for the Stars [30%]


In a game, a player is required to complete two tasks: Task 1 and Task 2. The player’s perfor-
mance in the two tasks are assessed and is given two scores x and y correspondingly, denoted
by a pair (x, y). A player A is said to dominate another player B if A obtains superior scores
in both tasks compared against B. If a player C is not dominated by any other players, player
C is awarded a star. However, if a new player D joins the game and surpasses player C’s scores
in both tasks (i.e., C is dominated by the new player D), C will lose its star.

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).

• Report: Print out the scores of all players with a star.

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.

Question 5 - Implementing a Queue by Stack [0%]


Suppose you are given two stacks S1 and S2, describe how you could implement a queue using
only S1 and S2.

(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:

(i) enqueue(1); enqueue(2); enqueue(3);


(ii) dequeue();
(iii) enqueue(4); enqueue(5);

(c) Explain your code step by step in the last operation of (b)(iii), i.e., the enqueue(5) oper-
ation.

Question 6 - Proofs Related to Hash Function [0%]


Consider the following hash function: h(k, i) = (h′ (k) + 21 (i + i2 )) mod m, where m = 2p and
h′ (k) = k mod m for some positive integer p. Prove or disprove that for any k, the probe
sequence < h(k, 0), h(k, 1), ..., h(k, m–1) > is a permutation of < 0, 1, 2, ..., m–1 >.

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!

You might also like