Assignment 1
Problem 1. (20 points)
Consider the following algorithm that given an array A of length n produces ank −1 other array B of length n − k where B[i ] = ∑ij+A [ j ] =i
function algorithm(A, k)
n ← length of A
B ← new array of size n − k
for i ∈ [0 : n − k] do
B [i ] ← 0
for j ∈ [0 : k ] do
B [i ] ← B [i ] + A [i + j ]
return B
▷ i ranges from 0 to n − k − 1
▷ j ranges from 0 to k − 1
Assuming that k < n/2 (but not necessarily a constant) use O-notation to upperbound the running time of the following algorithm in terms of both n and k.
Problem 2. (40 points)
We want to build a queue for integer elements that in addition to the operations we saw during the lecture, also supports a seeSaw() operation that on a given queue holding the elements Q0 , Q1 , . . . , Qn−1 returns
All operations should run in O(1) time. 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: a) Design a data structure that supports the required operations in the required time and space. 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.
Problem 3. (40 points)
We want to build a stack for elements having a colour attribute (e.g., blue, red, orange, etc). We say that a bunch of consecutive positions in the array for a monochromatic stretch if all the elements stored in those positions have the same colour. In addition to the usual stack operations, we want to support a maxMono() operation that on a given stack returns the maximum length of any monochromatic stretch currently stored in the stack.
All operations should run in O(1) time. Your data structure should take O(n) space, where n is the number of elements currently stored in the data structure. a) Design a data structure that supports the required operations in the required time and space. 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.
Written 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 algorithm as you would to a friend over the phone, such that you completely and unambiguously describe your algorithm, including all the important (i.e., nontrivial) 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 end you can also include pseudocode, but this is optional. • In particular, when designing an algorithm or data structure, it might help you (and us) if you briefly describe your general idea, and after that you might 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 multiple answers, then we will give you marks only for "your worst answer", as this indicates 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 it. You do not need to write more than necessary (see comment above). • When giving answers to questions, always prove/explain/motivate your answers. • When giving an algorithm as an answer, the algorithm does not have to be given as (pseudo-)code. • If you do give (pseudo-)code, then you still have to explain your code and your ideas in plain English. • Unless otherwise stated, we always ask about worst-case analysis, worst case running times, etc. • As done in the lecture, and as it is typical for an algorithms course, we are interested in the most efficient algorithms and data structures. • If you use further resources (books, scientific papers, the internet,...) to formulate your answers, then add references to your sources and explain it in your own words. Only citing a source doesn’t show your understanding and will thus get you very few (if any) marks. Copying from any source without reference is considered plagiarism.