0% found this document useful (0 votes)
38 views2 pages

Assignment 1: TH TH

The document contains 3 questions about algorithm analysis: 1. Analyzes a nested loop algorithm and determines its time complexity is O(n^2) and Ω(n^2). 2. Describes an algorithm to find the smallest m elements across k lists using a min heap. The time complexity is O(mlogk + k). 3. Analyzes the number of edges and key comparisons in a binomial heap during a sequence of k insertions. The average cost per insertion is O(1).

Uploaded by

Angel Akanksha
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)
38 views2 pages

Assignment 1: TH TH

The document contains 3 questions about algorithm analysis: 1. Analyzes a nested loop algorithm and determines its time complexity is O(n^2) and Ω(n^2). 2. Describes an algorithm to find the smallest m elements across k lists using a min heap. The time complexity is O(mlogk + k). 3. Analyzes the number of edges and key comparisons in a binomial heap during a sequence of k insertions. The average cost per insertion is O(1).

Uploaded by

Angel Akanksha
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/ 2

ASSIGNMENT 1

Q1.

a. Since all assignments,comparisons and arithmetic operations take constant time, we


care about the time taken by both the for loops.
In the worst case, line 3 executes n-1 times + 1 time for the loop guard. The inner j loop
in line 4 executes i-1 times from j=1 to j = i-1. This can be overestimated to n-1 times,
since i-1 << n-1. In the WC, the function returns only in line 6.
Hence the function has runtime = (n-1)(n-1) + c where c is constant time.
T(n) = O(n2)
Yes T(n) = (n2).
b.
Assume an example where in the worst case, all the elements in the list are arranged in
an increasing order of sequence incrementing by 1. Since the first element gets
assigned to 1 in line 1, we don't care about it's value originally. In this case the function
would never return on line 5 since with each iteration of the loop the values previous to
the ith element gets incremented by 1 thereby making it equal to the ith elements value.
This means that the first for loop in line 3 executes n-1 times and the for loop in line 4
executes n times when overestimated. Therefore in worst case the function runs in at
least (n2) time. Example list:
x 2 3 4 5

Q2.
a. The algorithm is as follows:
Create a min heap of size k (number of lists) containing the first element from
each of the k lists. Extract the minimum (root node) from this min heap and
replace it with the next element of the array from which the root node was taken.
Repeat this m times until we get the smallest m elements(or in the special case,
all the elements in all the lists).

This algorithm works since it stores only k elements at a time in the heap (initially the first
element from all the lists). Then it will keep replacing the root node (minimum element)
with the next one from it's respective list until we have all m items (this is done using a
for loop from 1 to m). Hence the overall heap is in ascending order (because we will call
Insert which will ensure the heap is ordered correctly and later when extracting the
minimum, it will also reorder the heap with the minimum element at the root node) so
is called m
when ExtractMin times, it retrieves the smallest m elements from all the lists.

b. This algorithm runs in O(mlogk + k) because of the following:


We initially create a min heap of size k and populate that resulting in the O(k).
The number of elements in our heap will always be k, so when we call ExtractMin, it runs
in O(logk).
We need to repeat the process described above m times to get the m smallest elements
so we call ExtractMin m times (inside a for loop), so the total runtime of this would be
O(mlogk).
Everything else is done in constant time (for example initializing output array, variables,
etc.)
Therefore, putting this all together, the total runtime will be O(mlogk + k).

Q3.
a. For a binomial heap H with size n where (n) is the number of 1s in the binary
representation, (n) is the number of trees in the heap. For every tree Ti where
1 i (n), with size ni the number of edges is ni - 1.
(n)
So for the sum of edges = { ni } - (n) = n - (n)
i=1
b. Referring to the hint given in the question, we need to relate the number of key
comparisons required to the number of extra edges formed in H. We know the number of
comparisons will equal the number of extra edges formed because a comparison only
happens when there are two trees of the same size after an insertion and thus resulting
in one extra edge being formed to merge those tree.
The number of extra edges formed in H:
Initially H has n elements (given in the question). So from part (a), it has n - (n)
edges. After k insertions, the size of H is n + k, so the number of edges is (n+k) -
(n+k) (also from part (a)).
So the number of extra edges formed is:
(n+k) - (n+k) - [n - (n)] = k - (n+k) + (n)
<= k + (n) #upperbound by removing
subtraction

Now we know that there will be (at most) k + (n) pairwise comparisons. However, as
stated in part (a), (n) is the number of 1s in the binary representation. Therefore, (n)
<= floor(logn) + 1. Going back to the number of pairwise comparisons, k + (n) <= k +
floor(logn) + 1. However, as stated in the question we assume k > logn, so k + (n) <=
2k + 1 = O(k). Since k is a constant, the worst-case total cost of the k successive
insertions divided by k is bounded above by a constant.

You might also like