0% found this document useful (0 votes)
18 views

COMP2123 Assignment 1

comp2123 study notes

Uploaded by

efe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

COMP2123 Assignment 1

comp2123 study notes

Uploaded by

efe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ds & a Assignment 1 s1 2024

This assignment is due on March 22. Submit your assignment via Grade-
scope. All submitted work must be done individually without consulting some-
one else’s solutions in accordance with the University’s “Academic Dishonesty
and Plagiarism” policies.
As a first step, go to the last page and read the section: Advice on how to do
the homework.

Problem 1 (10 points)


Consider the following snippet of pseudocode that takes as input an array of n
integers.

1: function algorithm(A)
2: n ← length of A
3: num_matches ← 0
4: for i ∈ [0 : n] do ▷ i ranges from 0 to n − 1
5: for j ∈ [i + 1 : n] do ▷ j ranges from i + 1 to n − 1
6: if A[i ] == A[ j] then
7: num_matches ← num_matches + 1
8: return num_matches

Your task is to:

a) upperbound the running time of the algorithm in terms of n using O-


notation.
b) lowerbound the running time of the algorithm in terms of n Using Ω-
notation.

Problem 2 (25 points)


Consider a stack where each element stores an integer value. We want to extend
the stack ADT we saw in the lectures with the additional operations listed below.
The operations should run in O(1) time and the running time of the other stack
operations should remain the same as those of a regular stack:

Count() returns the number of items on the stack

Sum() returns the sum of all the elements on the stack

Average() returns the average of all the elements on the stack

For each operation, describe their implementation in English (pseudocode is


optional), argue the correctness and the running time.

1
ds & a Assignment 1 s1 2024

Problem 3 (25 points)


As input we are given a sorted array B containing n positive integers, together
with an integer m. The aim is to compute how many indices i and j (with i < j)
there are such that the sum of the ith and the jth element of B is at least m, i.e.,
A[i ] + A[ j] ≥ m. For full marks, your algorithm needs to run in O(n) time.

Example:
B = [1, 4, 4, 6], m = 7 → return 4

a) Design an algorithm that solves the problem.


b) Argue the correctness of your algorithm.
c) Analyze the running time of your algorithm.

2
ds & a Assignment 1 s1 2024

Advice on how to do the home work


• Assignments should be typed and submitted as pdf (no handwriting)

• 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 points 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 lecture notes 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.

• 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 in the lecture, and as is typical for an algorithms course, we are inter-


ested in the most efficient algorithms and data structures.

• If you use additional resources (books, scientific papers, the internet, etc.)
to formulate your answers, then add references to your sources.

• If you refer to a result in a scientific paper or on the web, you need to


explain the results to show that you understand the results and how it was
proven.

You might also like