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

Assignment 01 - FA24

Uploaded by

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

Assignment 01 - FA24

Uploaded by

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

COMSATS University, Islamabad

Islamabad Campus
Department of Computer Science

Read before Attempt

FA24 Page 1
Assignment No. 1:Mathematical Analysis of Iterative and Recursive Algorithms
COMPLEXITY FUNCTIONS AS ASYMPTOTIC NOTATION
Subject: Design and Analysis of Algorithm Instructor: Tanveer Ahmed Siddiqui

Class: BS(CS)- 5A, BS(CS)- 5B, BS(AI), BS(DS)


Assigned Date: September 23, 2024 Due Date: October 7, 2024
CLO Mapping
CLO-4: Analyze best, average, and worst-case behaviors of an algorithm.
Instructions
Read the following instructions before attempting to solve this assignment.
1. Submission: This is an individual assignment. Submit handwritten soft copy. (Word or PDF format).
2. Understand the Assignment Thoroughly:
3. Make sure you fully grasp the requirements and objectives of the assignment.
4. Clarify any doubts with your instructor if necessary.
5. Read recommended books to gain a deeper understanding of various concepts in this assignment.
6. Try to consolidate key concepts and ideas from these questions.
7. Ensure academic integrity and prevent plagiarism: Try to make the solution by yourself and protect your
work from other students. If I found the solution files of some students are same then I will reward

zero marks to all those students.


8. Performance Indicators for Every Assignment (Grading rubric)
The following criteria will be used to grade the assignment.
Category Criteria
Correctness of Complexity • 30%: Correctly identifies the time and space complexity in Big-O
Analysis (30%) notation for all sections.
• 20%: Mostly correct, but with some minor errors or omissions in
complexity analysis.
• 10%: Partially correct complexity analysis but contains significant
errors.
• 0%: Incorrect or missing complexity analysis.

Justification of Complexity • - 25%: Provides thorough and well-explained reasoning for time and
(25%) space complexity calculations.
• 15%: Adequate reasoning but lacks depth or completeness.
• 10%: Some justification provided, but it is weak or incorrect.
• 0%: No justification for the complexity analysis.

Comparison of Algorithms • 20%: Clear and accurate comparison of multiple algorithms in terms
(20%) of efficiency, scalability, and trade-offs.
• 15%: Comparison is mostly correct but lacks detail or misses key
points.

FA24 Page 2
• 10%: Limited comparison with some inaccuracies or omissions.
• 0%: No comparison or completely incorrect.

Edge Case and Worst-Case • 15%: Thorough analysis of best-case, average-case, and worst-case
Analysis (15%) complexities, with edge case examples.
• 10%: Provides analysis but lacks completeness (e.g., missing worst-
case or edge cases).
• 5%: Only one scenario (e.g., best-case) is analyzed.
• 0%: No consideration of edge or worst cases.

Clarity and Structure of • 10%: Well-structured, with clear language and supporting diagrams or
Explanation (10%) examples where needed.
• 7%: Generally clear but could use more structure or examples.
• 3%: Unclear or confusing, with minimal supporting details.
• 0%: Poorly structured, hard to understand, or no explanations
provided.

In order to discourage copying/cheating, 30% marks may be awarded based on the marks obtained either in the
corresponding quiz or a question(s) in the Midterm exam that will be used as multiplying factor. Hence, the
final grades (out of 100) will be calculated as follows:
Obtain Marks = 40% of (marks obtain using

Submission marks (given by the letter grades shown above) + 30 * multiplying factor

FA24 Page 3
Question # 1
Consider the following Algorithm Test(n). Provide a line-by-line analysis and construct function T(n) that gives
the runtime of this algorithm as a function of “n”. Also determine the Big-Oh of this algorithm.
Algorithm Test(n)
// Input:
// Output:
1. sum ← 0
2. for i ← 0 to n
3. if ( i%2 ≠ 0)
4. sum ← sum + Test1( i ) + Test1( i+1)
5. else
sum ← sum + Test1 ( i ) + Test2( i )
6. end for
7. return sum
Algorithm Test1(a) Algorithm Test2(b)
// Input: // Input:
// Output: // Output:
1. for i ← 0 to n2 1. for i ← 0 to n
2. a←a*i 2. for j ← 0 to i
3. end for 3. b ← b * (i +j)
4. return a 4. end for
5. end for
6. return b
Question # 2
Consider the following Algorithm Test(n). Provide a line-by-line analysis and construct function T(n) that gives
the runtime of this algorithm as a function of “n”. Also determine the Big-Oh of this algorithm.
Algorithm Test(n)
// Input:
// Output:
1. for i ← 1 to n
2. if ( i%2 = 0)
3. Test1( i ))
4. else
5. for j ← 1 to i
6. Print(Test2( i ))
7. Print ( )
8. end for
9. end for

FA24 Page 4
Algorithm Test1(n) Algorithm Test2(a)
// Input: // Input:
// Output: // Output:
1. for i ← 0 to n 1. sum ← 0
2. for j ← 0 to i 2. for i ← 0 to n
3. Print (j) 3. sum ← sum + a * i
4. end for 4. end for
5. for k ← 0 to 3n 5. return sum
6. Print (j)
7. end for
8. end for
Question # 3A
Find time complexity of the following algorithms. Use the most suitable notation among O, Ω,
and  to specify the time efficiency class of the given algorithm.

FA24 Page 5
Where Merge function has time complexity O(n)

FA24 Page 6
Consider modifying Algorithm selection sort as shown in Algorithm modselectionsort.

a) What is the minimum number of element assignments performed by Algorithm modselectionsort? When is this
minimum achieved?
b) What is the maximum number of element assignments performed by Algorithm modselectionsort? Note that each
interchange is implemented using three element assignments. When is this maximum achieved?

a) What is the minimum number of element comparisons performed by the BUBBLESORT algorithm?
When is this minimum achieved?
b) What is the maximum number of element comparisons performed by the BUBBLESORT algorithm?
When is this maximum achieved?
c) What is the minimum number of element assignments performed by the BUBBLESORT algorithm?
When is this minimum achieved?
d) What is the maximum number of element assignments performed by the BUBBLESORT algorithm?
When is this maximum achieved?
e) Express the running time of Algorithm BUBBLESORT in terms of the O and Ω notations.

FA24 Page 7
f) Can the running time of the algorithm be expressed in terms of the Θ-notation? Explain.
Consider the following insertion sort algorithm.

Computing the average number of comparisons performed by Algorithm INSERTIONSORT.


Question # 3B
uppose you have algorithms with the five running times listed below. (Assume these are the exact
running times.) How much slower does each of these algorithms get when you
1. Double the input size
2. Increase the input size by one?
Solution:
(a)
T(n) T(2n) Time Complexity increased by

(b)
T(n) T(n+1) Time Complexity
increased by

Question # 4
Set up a recurrence relation for the given functions. Solve recurrence relation using Recursion Tree Method.
Use appropriate asymptotic notation among O, Ω, and  to specify the time efficiency class of the given
function.
Test(n) Test(n)
{ {
if(n == 1) if(n = 0)

FA24 Page 8
return return
else else
{ {
for (i = 1; i < n; i++) for (i = 1; i < n; i= i*2)
{ {
Print(i) Print(i)
} }
Test(n-1)
Test(n/2) }
Test(n/4) }
Test(n/8)
}
}
Test(n) Test(n)
{ {
if(n = 0) if(n == 0)
return return
else else
{ {
for (i = 1; i <= n; i++) for( i = n/2; i <= n; i++)
{ for( j = 1; j+n/2 <= n; j++)
for (j = 1; j < n; j += i) for( k = 1; k <= n;
{ k=k*2)
// Some O(1) task Print(k)
} Test (n/2)
} }
for (k = 1; k < 5; k++) }
{
Test(n/2)
}
}
}
Test(n) Test(n)
{ {
if(n = 0) if(n = 0)
return return
else else
{ {
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
{ {

// Some O(1) task // Some O(1) task


} }

Test(n/3) Test(n-1)
Test(n/3) Test(n/2)
Test(n/3) }
Test(n/3) }

FA24 Page 9
}
}
Test(n) Test(n)
{ {
if(n = 0) if(n = 0)
return return
else else
{ {
O(1) Task for (i = 1; i <= n; i++)
Test(n-1) {
Test(n-1)
} // Some O(1) task
} }

Test(n/3)
Test(2n/3)
}
}
Question # 5
Consider the following recursion for integer multiplication of two positive number a and b:
a* 1 = a
a*b = a(b -1) + a
This can be implemented using following recursive algorithm as follows:
Algorithm recursive_multiplication(a, b)
if b = 1 then
return a
else
return a + recursive_multiplication ( a, b-1)
a) Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed.
Consider the following recursion for integer multiplication of two positive number a and b

b) Set up a summation expressing the number of times the algorithm’s basic operation is executed. What is
the efficiency class of this algorithm?
c) How does the given algorithm compare with the straightforward iterative algorithm for computing this
multiplication?
Question # 6
Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. Use
the most suitable asymptotic notation among O, Ω, and  to specify the time efficiency class of the following
given algorithm.
ALGORITHM Sum (X, i, n)
FA24 Page 10
// This algorithm compute the sum of the numbers xi through xn in a list X from left to right
// Pre-Condition: _______________________________
// Post-Condition: ______________________________
if i = n then
sum ← xi
else
sum ← xi + Sum (X, i+1, n)
Question # 7
Find time complexity of the following algorithm. Use the most suitable notation among O, Ω,
and  to specify the time efficiency class of the given algorithm.
Algorithm BinaryInsertionSort (A, n) BinarySearch (A, low, high, key)
{ if (low = high) then
for i  1 to n-1 do return low
ins = BinarySearch (A, 0, i, A[i]) mid = low + ((high - low) / 2)
if (ins < i) then if (key > A[mid]) then
temp = A[i] return BinarySearch (A, mid + 1, high, key)
for j  i-1 to ins do else if (key < A[mid])
A[j + 1] = A[j] return BinarySearch (A, low, mid, key)
A[ins] = temp return mid;

Question # 8A
Find time complexity of the following algorithms. Use the most suitable asymptotic notation
among O, Ω, and  to specify the time efficiency class of the given algorithm.

Algorithm binomialCoeff(n, k)
// Base Cases
if (k > n)
return 0;
if (k = 0 || k = n)
return 1;
// Recursive definition
return binomialCoeff(n - 1, k - 1)+ binomialCoeff(n - 1, k);

Algorithm binomialCoeff( n, k)

for i  0 to n do
for j  0 to min(i, k); do
if (j = 0 || j = i) then
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
return C[n][k];
Solution:
O(n*k)
Question # 8B

FA24 Page 11
Compute the time complexity of the following algorithms. Use the most suitable asymptotic
notation among O, Ω, and  to specify the time efficiency class of the given algorithm.
Algorithm lcs(X, Y, m, n )

if (m = 0 || n = 0)
return 0;
if (X[m-1] = Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));

Algorithm lcs(X, Y, m, n)
if (m = 0 || n = 0)
return 0;
if (X[m - 1] == Y[n - 1])
return dp[m][n] = 1 + lcs(X, Y, m - 1, n - 1, dp);

if (dp[m][n] != -1)
return dp[m][n];

return dp[m][n] = max(lcs(X, Y, m, n - 1, dp),lcs(X, Y, m - 1, n, dp));

Algorithm lcs(X, Y, m, n)
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}

return L[m][n];
}
Question # 9
Consider the following algorithm.
i. Set up a recurrence relation for the number of times the algorithm’s basic operation is executed.
ii. Solve the recurrence relation that you obtained in part (i) using:
a. Iteration Method
b. Recursion Tree Method
c. Substitution Method

ALGORITHM Search(A[l….r], l, r, key)


// A recursive search function.

FA24 Page 12
// Input: An Array A[l..r]
// Output: It returns location of key in given array A, otherwise -1
{
if (r ≥ l) then
{
m1 = l + (r - l)/3;
m2 = m1 + (r - l)/3;
// If key is present at the m1
if (A[m1] == key) then
return m1;
// If key is present at the m2
if (A[m2] == key) then
return m2;
// If key is present in left one-third
if (A[m1] > key) then
return Search (A, l, m1-1, key);
// If key is present in right one-third
if (A[m2] < key) then
return Search (A, m2+1, r, key);
// If key is present in middle one-third
return Search (A, m2+1, m2-1, key);
}
// We reach here when element is not present in array
return -1;
}

Question # 10
Consider the following algorithm.
iii. Set up a recurrence relation for the number of times the algorithm’s basic operation is executed.
iv. Solve the recurrence relation that you obtained in part (i) using
a. Iteration Method
b. Recursion Tree Method
c. Maser Theorem

FA24 Page 13
Question # 11
a) Give asymptotic upper and lower bounds for T (n) of the following recurrence relation.
 
T (n) = 2T ( n ) + lg n
b) Use a recursion tree to give an asymptotically tight solution to the recurrence T (n) = bT (n − c) + f (n)
c) The recurrence T(n) = 7T (n/2)+n2 describes the running time of an algorithm A. A competing algorithm
A′ has a running time of T’(n) = a T’(n/4) + n2. What is the largest integer value for a such that A′ is
asymptotically faster than A?
d) Can the master method be applied to the recurrence T (n) = 4T(n/2) + n2 lg n? Why or why not? Give an
asymptotic upper bound for this recurrence.
Question # 12
State the general formula for the following recurrence of the form:

FA24 Page 14

You might also like