0% found this document useful (0 votes)
20 views3 pages

DAA F24 Assignment 01 Solution

Uploaded by

afifa.spam
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)
20 views3 pages

DAA F24 Assignment 01 Solution

Uploaded by

afifa.spam
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/ 3

Design and Analysis of Algorithm

Assignment-01 (Fall 2024)

Submission to be done on UCP PORTAL


Due Date: Thursday, November 14, 2024, 5:00 PM

Task 1: Time Complexity Analysis


This question relates to finding the Time Complexity of snippets of C++ like code.
In each of the questions, do the following
a) Find the operations/cost and frequency of each line of code
b) Develop the equation T(n)
Sample
Q: Operations/Cost Freq.
for (int i=1; i<=n; ++i) c1 n+1
display (i) c2 n

T(n) = c1(n+1) + c2(n) = (c1+c2) (n) + c1 = c(n) + c1

Q:1 Operations/Cost Freq.


for (int i = 0; i < n; i = i + 3) c1 𝑛
+1
3
display (i) c2 𝑛
3
for (int j = 0; j < n; j = j + 2) c3 𝑛
+1
2
display (i) c4 𝑛
2
𝑛 𝑛 𝑛 𝑛
𝑇(𝑛) = 𝑐1 ( + 1) + 𝑐2 ( ) + 𝑐3 ( + 1) + 𝑐4 ( )
3 3 2 2

Q:2 Operations/Cost Freq.


for (int i = 1; i <= n; ++i) c1 𝑛+1
for (int j = 1; j <= n; j = j * 3) c2 𝑛. 𝑙𝑜𝑔3 𝑛 + 𝑛
display (i, j); c3 𝑛. 𝑙𝑜𝑔3 𝑛

𝑇(𝑛) = 𝑐1(𝑛 + 1) + 𝑐2(𝑛. 𝑙𝑜𝑔3 𝑛 + 𝑛) + 𝑐3(𝑛. 𝑙𝑜𝑔3 𝑛)

Q:3 Operations/Cost Freq.


for (int i = n; i >= 1; i = i / 2) c1 𝑙𝑜𝑔2 𝑛 + 1
for (int j = 1; j <= n; ++j) c2 𝑛. 𝑙𝑜𝑔2 𝑛 + 𝑙𝑜𝑔2 𝑛
display (i, j); c3 𝑛. 𝑙𝑜𝑔2 𝑛

𝑇(𝑛) = 𝑐1(𝑙𝑜𝑔2 𝑛 + 1) + 𝑐2(𝑛. 𝑙𝑜𝑔2 𝑛 + 𝑙𝑜𝑔2 𝑛) + 𝑐3(𝑛. 𝑙𝑜𝑔2 𝑛)

Faculty of Information Technology


University of Central Punjab
Lahore, Pakistan
Design and Analysis of Algorithm
Assignment-01 (Fall 2024)

Submission to be done on UCP PORTAL


Due Date: Thursday, November 14, 2024, 5:00 PM

Q:4 Operations/Cost Freq.


for (int i = 0; i < n; i++) c1 𝑛+1
if (arr[i] == target) c2 𝑛
return i; c3 𝑛
return -1; c4 1

𝑇(𝑛) = 𝑐1(𝑛 + 1) + 𝑐2(𝑛) + 𝑐3(𝑛) + 𝑐4(1)

Task 2 : Algorithm Comparison

Compare the time complexities of Binary Search, Selection Sort, and Bubble Sort algorithms.
For each algorithm:
a) Provide a brief description of the algorithm.
Solution: Available in Slides
b) Write the equation T(n) for the time complexity of each algorithm.
Solution:
Binary Search: O(logn)
Selection Sort: O(n2)
Bubble Sort: O(n2)
c) Compare the asymptotic complexities of these algorithms in terms of best-case,
average-case, and worst-case scenarios.
Solution:
Algorithm Best Case Average Case Worst Case
Binary Search Ω(1) Θ(logn) O(logn)
Selection Sort Ω(n2) Θ(n2) O(n2)
Bubble Sort Ω(n) Θ(n2) O(n2)

Faculty of Information Technology


University of Central Punjab
Lahore, Pakistan
Design and Analysis of Algorithm
Assignment-01 (Fall 2024)

Submission to be done on UCP PORTAL


Due Date: Thursday, November 14, 2024, 5:00 PM

Task 3: Recursive Algorithm Analysis


Consider the following recursive algorithm:
RecursiveAlgorithm(n):
if n <= 0:
return
RecursiveAlgorithm(n // 2)
RecursiveAlgorithm(n // 2)

a) Write the recursive equation for T(n).


b) Solve the recursive equation.
Solution:
𝑛
a) 𝑇(𝑛) = 2𝑇 ( 2) + 1

b) Recursive Equation Solution

𝑛
𝑇(𝑛) = 2𝑇 ( ) + 1
2
𝑛 𝑛
𝑇 ( ) = 2𝑇 ( ) + 1
2 2
2
𝑛
𝑇(𝑛) = 2 (2𝑇 ( 2 ) + 1) + 1
2
𝑛 𝑛
𝑇(𝑛) = 22 𝑇 (22 ) + 2 + 1 = 22 𝑇 (22 ) + 3
𝑛 𝑛
𝑇(𝑛) = 23 𝑇 (23 ) + 4 + 3 =23 𝑇 (23 ) + 7
𝑛
𝑇(𝑛) = 24 𝑇 ( ) + 15
24

𝑛
𝑇(𝑛) = 2𝑘 𝑇 ( ) + (2𝑘 + 1)
2𝑘
So far for 𝑛 ≥ 𝑘, we have
𝑛
𝑇(𝑛) = 2𝑘 𝑇 ( ) + (2𝑘 + 1)
2𝑘
What if k = logn => 2𝑘 = n ?
𝑛
𝑇(𝑛) = 𝑛𝑇 ( ) + (𝑛 + 1)
𝑛
𝑇(𝑛) = 𝑛 + (𝑛 + 1)

Faculty of Information Technology


University of Central Punjab
Lahore, Pakistan

You might also like