comp2123Assignment 1 s1 2025This assignment is due on March 25 and should be submitted on Gradescope.All submitted work must be done individually without consulting someone else’s
solutions in accordance with the University’s “Academic Dishonesty and Plagiarism” policies.
Before you read any further, go to the last page of this document and readthe Written Assignment Guidelines section.
Problem 1. (10 pointsGiven an array A consisting of n integers, we want to compute a matrix B wherefor any 0 ≤ i < j < n we have
B[i][j] = f([A[i], A[i + 1], ..., A[j − 1]])Consider the following algorithm for computing B:
Algorithm 1 Range Function Computation
- ❡✈❡♥✲❞✐❢❢([1, 3, 50, 48]) returns 4,
- ❡✈❡♥✲❞✐❢❢([1, 3, 50, 48, 30]) returns 4,
- ❡✈❡♥✲❞✐❢❢([3, 50, 48, 30]) returns 65.We are to design an implementation of the methods ❡♥q✉❡✉❡, ❞❡q✉❡✉❡, and❡✈❡♥✲❞✐❢❢ so that all operations run in O(1) time. You can assume that thedatastructure always starts from the empty queue.
Your data structure should take O(n) space, where n is the number of elements currently stored in the data structure.Your task is to:
Design a data structure that supports the required operations in the required time and space.
- a)
- b) Briefly argue the correctness of your data structure and operations.
- c) Analyse the running time of your operations and space of your data structure.2comp2123Assignment 1 s1 2025
Problem 3. (25 pointsA skyline is defined by an array of n distinct integers A = [h0, h1, h2, h3, h4, ...., hn−1
]representing the heights of buildings in a one-代写comp2123 Range Function Computationdimensional city, given in the order they appear from left to right. Suppose you are standing on the rooftop ofone of these buildings. You want to determine the closest taller building to your
left and the closest taller building to your right. The goal is to find an efficientalgorithm to compute this for ALL n buildings.
pecifically, for every building x ∈ [0, n − 1], compute the two closest indices i
and j to x such that:
i < x, j > x, A[i] > A[x] and A[j] > A[x]Your algorithm should return two arrays of length n:L[0...n − 1] where L[x] denotes the index (i) of the nearest taller building tothe left of building x (or ◆♦♥❡ if no such building exists).R[0...n − 1] where R[x] denotes the index (j) of the nearest taller building tothe right of building x (or ◆♦♥❡ if no such building exists).
Note:
a)
- b) Prove your algorithm is correct.
- c) Analyse the running time of your algorithm.3comp2123Assignment 1 s1 2025Written Assignment Guidelines
- Assignments should be typed and submitted as pdf (no pdf containing text
as images, no handwriting).
- Start by typing your student ID at the top of the first page of your submission. Do not type your name.
- Submit only your answers to the questions. Do not copy the questions.
- When asked to give a plain English description, describe your algorithas you would to a friend over the phone, such that you completely andunambiguously describe your algorithm, including all the important (i.e.non-trivial) details. It often helps to give a very short (1-2 sentence) description of the overall idea, then to describe each step in detail. At the endyou can also include pseudocode, but this is optional.
- In particular, when designing an algorithm or data structure, it might helpyou (and us) if you briefly describe your general idea, and after that youmight want to develop and elaborate on details. If we don’t see/understand your general idea, we cannot give you marks for it.
- Be careful with giving multiple or alternative answers. If you give multipleanswers, then we will give you marks only for "your worst answer", as thisindicates how well you understood the question.
- Some of the questions are very easy (with the help of the slides or book)You can use the material presented in the lecture or book without proving
- You do not need to write more than necessary (see comment above).
- When giving answers to questions, always prove/explain/motivate youranswers.
- When giving an algorithm as an answer, the algorithm does not have to begiven as (pseudo-)code.
- If you do give (pseudo-)code, then you still have to explain your code andyour ideas in plain English.
- Unless otherwise stated, we always ask about worst-case analysis, worstcase running times, etc.
- As done in the lecture, and as it is typical for an algorithms course, weare interested in the most efficient algorithms and data structures, thoughslower solutions may receive partial marks.
- If you use further resources (books, scientific papers, the internet,...) toformulate your answers, then add references to your sources and explain itin your own words. Only citing a source doesn’t show your understanding
nd will thus get you very few (if any) marks. Copying from any sourcewithout reference is considered plagiarism4