Data Structures - Assignment 4 IDC, Spring 2022

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Data Structures – Assignment 4

IDC, Spring 2022

Lecturer: Prof. Yael Moses, Dr. Ben Lee Volk

TAs: Yael Hitron, Guy Kornowski, Elad Tzalik

Submission day: 8.4.22 (you can use an extension and submit by 12.4.22)

Honor code:
• Do not copy the answers from any source.
• You may work in small groups but write your own solution. Add whom you
worked with.
• Cheating students will face Committee on Discipline (COD).
• Do not forget – you are here to learn!

Submission:
• Submit your solution as a PDF file only. Other formats will not be graded.
• Typed submissions will get a bonus of 3 points.
• If you choose not to type your solution, make sure the scan looks good. We will
deduct points for hard-to-read submissions.

Question A
1. The aim of this question is to prove that for every constants α > 0 and β > 0,
(log(n))α = O(nβ ). In other words, any power of log(n), no matter how large, is
asymptotically much smaller than any small power of n.
• Let α > 0 be some positive constant. Prove that there exists some constant Cα ,
which may depend on α, such that for any n ≥ 2,
(log(n))α ≤ Cα n,
and deduce (log(n))α = O(n).
Hint: you should be able to prove this statement with Cα = αα . There could
be several ways to show this. You may find the inequality log(n) ≤ n useful as
well as some logarithm laws.

1
• Use the previous item to show that for every constants α > 0 and β > 0,

(log(n))α = O(nβ ).

2. Analyze the worst case running time of the following SqrtSearch algorithm which
gets a sorted array of size n (which we assume is a square integer for simplicity) and
a key k and returns 1 if there is an element of key k in it and 0 otherwise. Give a
tight bound in terms of Θ relation, and explain your answer shortly.
Algorithm 1: SqrtSearch(A, k)

for i = 0,
√ i < n, i + + do√
if A[ n · i] ≤ k and √ A[ n · (i + 1) − 1] ≥ k then
for j = 0,√ j < n, j + + do
if A[ n · i + j] = k then
return 1
end
end
end
end
return 0

3. Apply part 1 of this question and show that SqrtSearch is far worse then binary
search. Explicitly, show that if T (n) is the worst case running time of SqrtSearch
then T (n) = Ω(logβ (n)) for any β > 0.

Question B
Provide an O(n log(n)) time algorithm (worst case) that solves the following tasks. Give a
short description of the algorithm and explain the running time.

1. Given an unsorted array of numbers, A. Suggest an efficient algorithm that detects


a pair of indices {i, j} such that A[i] and A[j] satisfy A[i] = A[j]3 .
Provide an analysis of the worst-case time complexity of the algorithm.

2. Given an unsorted array of numbers, A. Consider the function f (i) = A[i]−i. Explain
how to use similar ideas to those in (1) to give an efficient algorithm that returns
TRUE if there are two indices of A, i, j with the same value f (i) = f (j) and FALSE
otherwise. For example, for A = [8, 2, 10, 4, 10] we have f (1) = f (3) and f (2) = f (4)
therefore the output should be TRUE. The additional space that the algorithm is
allowed to use is O(1).
Provide an analysis of the worst-case time complexity of the algorithm.

3. For an array A of n numbers two indices i < j are called a slope-1 pair if A[i]−A[j]
i−j
= 1.
For example the array [1, −1, 3, 1] has two slope-1 pairs 0, 2 and 1, 3. Suggest an

2
efficient algorithm that returns 1 if there is a slope-1 pair and 0 otherwise .
The additional space that the algorithm is allowed to use is O(1).
Provide an analysis of the worst-case time complexity of the algorithm.
Hint: you can use (2) in your solution.

Question C
A student suggested the following algorithm as an improvement of the 32 sorting algorithm
seen in the recitation:
Algorithm 2: NewSort(A, p, r)
t ← ⌊ r−p
3

if (r − p) ≤ 6 then
return BubbleSort(A)
end
MergeSort(A,p,r-t) ▷ Sort left two thirds of A
MergeSort(A,p+t,r) ▷ Sort right two thirds of A
NewSort(A,p,r-t) ▷ Sort left two thirds of A again
return A

1. Formulate a recurrence relation for the worst-case running time of N ewSort denoted
as T (n).

2. Prove a tight upper bound for T (n) (in term of big-O).

3. Prove a tight lower bound for T (n) (in term of Ω).

Question D
1. Draw a decision tree of InsertSort applied to 3 elements.

2. Consider an array A with 10n elements. We call an array A 10-sorted if each set
of 10 elements, A[10i + 1], . . . , A[10i + 10], is sorted for 0 ≤ i ≤ n − 1). Suggest a
comparison based algorithm to sort a given 10-sorted array A with a worst case time
complexity O(n). If no such an algorithm exists, explain why.

3. A comparison based search algorithm is an algorithm that, given a sorted array A and
an input x, returns the index of x in A if x appears in A, and “not found” otherwise,
and can only access its input through queries of the form “A[i] ≤ x?” or “A[i] ≥ x?”.
Prove that the worst case running time of any comparison based search algorithm is
Ω(log n).

You might also like