0% found this document useful (0 votes)
13 views6 pages

Daa Assignment 1

The document outlines an assignment on the Design and Analysis of Algorithms, detailing the use of recurrence relations in analyzing recursive algorithms, exemplified by binary search and merge sort. It explains the Master Theorem for solving recurrences and applies it to a specific case, determining the time complexity of T(n) = 9T(n/3) + n^2 as O(n^2 log n). Additionally, it describes Strassen's Algorithm for matrix multiplication, providing a step-by-step example to illustrate the process.

Uploaded by

mnssuresh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Daa Assignment 1

The document outlines an assignment on the Design and Analysis of Algorithms, detailing the use of recurrence relations in analyzing recursive algorithms, exemplified by binary search and merge sort. It explains the Master Theorem for solving recurrences and applies it to a specific case, determining the time complexity of T(n) = 9T(n/3) + n^2 as O(n^2 log n). Additionally, it describes Strassen's Algorithm for matrix multiplication, providing a step-by-step example to illustrate the process.

Uploaded by

mnssuresh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY

ASSIGNMENT SUBMISSION DETAILS


Academic Year: 2024-25
Subject Name & Code: – DESIGN AND ANALYSIS OF ALGORITHMS
& 23CST104
Name of the Student M.SURESH
Roll no. 23691A28G2
Year/Department/section I I CST-C
Assignment number 1
Marks(** Max 3 marks)

1. Explain how recurrence relations are used in analyzing


recursive algorithms.

ANS.

What Are Recurrence Relations?

A recurrencee relatiion is a mathematical formula thatt defiines a sequencce wheree each


terrm is related to previous terms. In the context of recursive algorithms, it describes how the
total work or tiime requiired by an algoriithm is related too thee time required for smaller
subproblems. Essentially, a recurrence relation helps us express the time complexity of
recursive algorithms.

How Are Recurrence Relations Used in Recursive Algorithms?

In a recursive algorithm, the time it takes to solve a problem depends on:


1. The work done at each recursive step (like dividing the problem or combining results).
2. The work done by recursive calls (which handle smaller subproblems).

A recurrence relation combines these factors and expresses how the total work growss as thee
sizee off thee iinput increasess.

Examplee: Binary Search

Consider the binary search algorithm. It works by repeatedly dividing the problem size in half
until it finds thee targett. If thee problem size is nnn, the recurrence relation for binary search
is:

T(n)=T(n/2)+O(1)T(n) = T(n/2) + O(1)T(n)=T(n/2)+O(1)

Where:

 T(n/2)T(n/2)T(n/2) representss thee time for the recursiive call onn the smaller problem
off size n/2n/2n/2.
 O(1)O(1)O(1) representss thee constantt tiime spentt att eachh step (like comparing
thee middle element).

By solving this recurrence, we can see that thee tiime complexiity off biinary searchh iis
O(log⁡n)O(\log n)O(logn), meaning it takes logarithmic tiime as thee input siize growss.

How to Solve Recurrence Relations?

To analyze recursive algorithms, we need to solve the recurrence relation to find the overall
time complexity. Some common methods for solving recurrences are:

1. Substitution Method: In this methodd, we makee a gues about thee solutiion andd
thenn prove it by induction.
2. Masterr Theorem: Thiis method gives a straiightforward way too solvee certain typess
off recurrencess by comparing parameters.
3. Recursion Tree: A visual approach where we break down the recursive calls and
calculatee the totall workk donee at each levell of recursionn.

Example: Merge Sort

Let’s look at merge sort, another recursive algorithmm. Merge sort diivides the problem into
two halves, recursively sorts each half, and then combines the results. Thee recurrence relatiion
forr merge sortt is:

T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n)T(n)=2T(n/2)+O(n)


Where:

 2T(n/2)2T(n/2)2T(n/2) means the algorithm makes twoo recursiive calls to sort the two
halves.
 O(n)O(n)O(n) represents the linear time required too mergee the two halvess.

Usiing methods like the Masteer Theorrem or a recursiion tre, we sollve this recurrence andd
fiind thatt the tiime complexiity of merge sort is O(nlogn)O(n \log n)O(nlogn).

2. State the Master Theorem. Given the recurrence relation T(n) =


9T(n/3) + n^2, apply the Master Theorem to find the asymptotic time
complexity. Explain whiich case off thee Masterr Theoremm you are
using.
ANS:

Master Theorrem

The Masterr Theoremm iis usedd to analyze the tiime complexiity off diviide-and-conquerr
recurrences and appliies too recurrencess off the forrm:

T(n) = aT(n / b) + O(n^d)

Where:

 a = numberr of subproblems,
 b = factorr by whiich the problemm siize is reducedd,
 d = costt off the workk done outsiide the recursiive calls.

Master Theorem Cases

1. Case 1: If a > b^d, then the time complexiity iis: T(n) = O(n^log_b a)
2. Case 2: If a = b^d, then the time complexiity iss: T(n) = O(n^d * log n)
3. Case 3: If a < b^d, then the time complexity is: T(n) = O(n^d)

Given Recurrence

The recurrence to analyze is: T(n) = 9T(n / 3) + n^2

Here:

 a=9
 b=3
 d=2

Applying the Masterr Theorem

We calculate log_b a, which is the logarithm of a to the base b:

 log_3 9

Since 9 = 3^2, we get:

 log_3 9 = 2

Now, we compare log_b a = 2 with d = 2. Since log_b a = d, we are in Case 2 of the Masterr
Theorem.

Conclusion

According to Case 2 of the Masterr Theorem:

 T(n) = O(n^d * log n)

Here, d = 2, so the tiime complexity off the recurrencee is:

 T(n) = O(n^2 * log n)

Thus, the asymptotic time complexity of the recurrence is O(n^2 * log n).

3. Compute the productt off matriices usiing Strassen’s Algoriithm


with Example.
ANS.

Strassen's Formula:

We compute the following intermediate products (P1 through P7):

1. P1=a(e−h)P_1 = a(e - h)P1=a(e−h)


2. P2=(a+b)hP_2 = (a + b)hP2=(a+b)h
3. P3=(c+d)eP_3 = (c + d)eP3=(c+d)e
4. P4=d(g−e)P_4 = d(g - e)P4=d(g−e)
5. P5=(a+d)(e+h)P_5 = (a + d)(e + h)P5=(a+d)(e+h)
6. P6=(b−d)(g+h)P_6 = (b - d)(g + h)P6=(b−d)(g+h)
7. P7=(a−c)(e+f)P_7 = (a - c)(e + f)P7=(a−c)(e+f)
Using these intermediate products, we can compute the result matrix C=ABC = ABC=AB as:

C=[C11C12C21C22]C = \begin{bmatrix} C_{11} & C_{12} \\ C_{21} & C_{22} \end{bmatrix}C=[C11C21C12


C22]

Where:

 C11=P5+P4−P2+P6C_{11} = P_5 + P_4 - P_2 + P_6C11=P5+P4−P2+P6


 C12=P1+P2C_{12} = P_1 + P_2C12=P1+P2
 C21=P3+P4C_{21} = P_3 + P_4C21=P3+P4
 C22=P1+P5−P3−P7C_{22} = P_1 + P_5 - P_3 - P_7C22=P1+P5−P3−P7

Example of Strassen's Algorithm:

Let's multiply two 2×22 \times 22×2 matrices using Strassen’s Algorithm.

Given Matrices:

Let:

A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7


& 8 \end{bmatrix}A=[1324],B=[5768]

We will now compute the intermediate products using Strassen's method.

Step-by-step Calculation:

1. Compute P1P_1P1:

P1=1×(6−8)=1×(−2)=−2P_1 = 1 \times (6 - 8) = 1 \times (-2) = -2P1=1×(6−8)=1×(−2)=−2

2. Compute P2P_2P2:

P2=(1+2)×8=3×8=24P_2 = (1 + 2) \times 8 = 3 \times 8 = 24P2=(1+2)×8=3×8=24

3. Compute P3P_3P3:

P3=(3+4)×5=7×5=35P_3 = (3 + 4) \times 5 = 7 \times 5 = 35P3=(3+4)×5=7×5=35

4. Compute P4P_4P4:

P4=4×(7−5)=4×2=8P_4 = 4 \times (7 - 5) = 4 \times 2 = 8P4=4×(7−5)=4×2=8

5. Compute P5P_5P5:
P5=(1+4)×(5+8)=5×13=65P_5 = (1 + 4) \times (5 + 8) = 5 \times 13 = 65P5=(1+4)×(5+8)=5×13=65

6. Compute P6P_6P6:

P6=(2−4)×(7+8)=(−2)×15=−30P_6 = (2 - 4) \times (7 + 8) = (-2) \times 15 = -30P6


=(2−4)×(7+8)=(−2)×15=−30

7. Compute P7P_7P7:

P7=(1−3)×(5+6)=(−2)×11=−22P_7 = (1 - 3) \times (5 + 6) = (-2) \times 11 = -22P7


=(1−3)×(5+6)=(−2)×11=−22

Now, we compute the final matrix CCC:

1. Compute C11C_{11}C11:

C11=P5+P4−P2+P6=65+8−24−30=19C_{11} = P_5 + P_4 - P_2 + P_6 = 65 + 8 - 24 - 30 = 19C11=P5


+P4−P2+P6=65+8−24−30=19

2. Compute C12C_{12}C12:

C12=P1+P2=−2+24=22C_{12} = P_1 + P_2 = -2 + 24 = 22C12=P1+P2=−2+24=22

3. Compute C21C_{21}C21:

C21=P3+P4=35+8=43C_{21} = P_3 + P_4 = 35 + 8 = 43C21=P3+P4=35+8=43

4. Compute C22C_{22}C22:

C22=P1+P5−P3−P7=−2+65−35−(−22)=50C_{22} = P_1 + P_5 - P_3 - P_7 = -2 + 65 - 35 - (-22) =


50C22=P1+P5−P3−P7=−2+65−35−(−22)=50

Final Result: After multiplying A and B is

C = [ 19 22 ]

[ 43 50 ]

You might also like