KEMBAR78
Design and Analysis of Algorithms UNIT 1 .pptx
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
(2025-26)
Design and Analysis of Algorithms (BCO037A)
B. Tech CSE V Sem
UNIT-1
Rajneesh Pareek
Assistant Professor-II
CSE
Contents
• Introduction,
• algorithms specification,
• time and space complexity,
• performance analysis,
• recurrence relations.
• Divide and Conquer – finding max min.
Introduction
An algorithm is a finite sequence of well-defined,
unambiguous instructions designed to solve a specific problem
or perform a computation. It outlines a step-by-step procedure
to transform input data into a desired output.
Algorithms are just like a technology. We all use latest and
greatest processors but we need to run implementations of
good algorithms on that computer in order to properly take
benefits of our money that we spent to have the latest
processor.
Algorithm Specification
An algorithm is a sequence of unambiguous instructions for
solving a problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
• Algorithms are definite
• Algorithms accpets Input
• Algorithms produces Output
Time and Space Complexity
ASYMPTOTIC NOTATION: Formal way notation to speak
about functions and classify them
• Big-O,
Time and Space Complexity
• Omega Ω,
Time and Space Complexity
• Theta Θ
Performance Analysis
Worst case:- Defines the input for which the algorithm takes
more time or input for which algorithm runs the slowest.
Best case:- Defines the input for which the algorithm takes less
time or input for which algorithm runs the fastest.
Performance Analysis
Average case:- Here we run the algorithm for any number of
iterations then compute running time for each trial sum it and
divide by the number of trials
= (total running time)/(number of trials)
Assumes input is random.
Note:- lower bound <= average time <= upper bound
Recurrence Relations
Why they arise in recursive algorithms
General form: T(n) = aT(n/b) + f(n)
Example: Merge Sort → T(n) = 2T(n/2) + Θ(n)
Solving Recurrences
Methods:
1. Substitution method
2. Iterative method
3. Recursion tree method
4. Master method
Substitution method
The substitution method comprises of 3 steps
• Guess the form of the solution
• Verify by induction
• Solve for constants
Substitution method
Example : T(n)=4T(n/2)+n
Step 1:T(n)=O(n3
)
Step 2: verify the induction
Assume T(k)<=ck3
T(n)=4T(n/2)+n
<=4c(n/2)3
+n <=cn3
/2+n <=cn3
-(cn3
/2-n)
T(n)<=cn3
as (cn3
/2 –n) is always positive So what we assumed
was true. T(n)=O(n3
)
Step 3: solve for constants Cn3
/2-n>=0 for n>=1 c>=2
Iterative method
T(n)=2T(n/2)+n
=> 2[2T(n/4) + n/2 ]+n
=>2.2 T(n/4)+n+n
=> 2.2 [2T(n/8)+ n/4]+2n
=>23
T(n/23
) +3n
After k iterations ,T(n)=2k T(n/2k )+kn--------------(1)
Iterative method
Sub problem size is 1 after n/2k =1 => k=logn So,afterlogn
iterations ,the sub-problem size will be 1.
So,when k=logn is put in equation 1
T(n)=nT(1)+nlogn
=nc+nlogn ( say c=T(1))
=O(nlogn)
Recursion tree method
T(n)=3T(n/4)+cn2
Recursion tree method
Master Method
Case 1: f(n) = O(n(logba – ε)
) → T(n) = Θ(n(logba – ε)
)
Case 2: f(n) = Θ(n(logba – ε)
) → T(n) = Θ(n(logba – ε)
log n)
Case 3: f(n) = Ω(n(logba + ε)
), regularity condition → T(n) =
Θ(f(n))
Merge Sort → T(n) = 2T(n/2) + Θ(n) → Case 2 → Θ(n log n)
Binary Search → T(n) = T(n/2) + Θ(1) → Case 1 → Θ(log n)
Divide & Conquer
What is Divide & Conquer?
Steps: Divide → Conquer → Combine
Algorithm D and C (P) {
if small(P) then return S(P)
else {
divide P into smaller instances P1 ,P2 .....Pk
Apply D and C to each sub problem Return
combine (D and C(P1)+ D and C(P2)+.......+D and C(Pk)) }
Divide & Conquer – Max & Min
Find max & min in an array
Problem Statement : Given an array X[] of size n, write a
program to find the maximum and minimum element in the
array.
Goal: solve this problem using minimum number of
comparisons
Naïve O(n) vs Divide & Conquer O(n) with fewer comparisons
Divide & Conquer – Max & Min
Recursive splitting into halves Combine results for max & min
Algorithm steps
Step 1: Suppose the function call minMax(X[], l, r) returns the maximum
and minimum of the array, where l and r are the left and right endpoints.
Step 2: Divide array by calculating mid index i.e. mid = l + (r — l)/2
Step 3: Recursively find the maximum and minimum of left part by calling
the same function i.e. leftMinMax[2] = minMax(X, l, mid)
Step 4: Recursively find the maximum and minimum of right part by
calling the same function i.e. rightMinMax[2] = minMax(X, mid + 1, r)
Step 5: Finally, get the overall maximum and minimum by comparing the
min and max of both halves.

Design and Analysis of Algorithms UNIT 1 .pptx

  • 1.
    DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING (2025-26) Design and Analysis of Algorithms (BCO037A) B. Tech CSE V Sem UNIT-1 Rajneesh Pareek Assistant Professor-II CSE
  • 2.
    Contents • Introduction, • algorithmsspecification, • time and space complexity, • performance analysis, • recurrence relations. • Divide and Conquer – finding max min.
  • 3.
    Introduction An algorithm isa finite sequence of well-defined, unambiguous instructions designed to solve a specific problem or perform a computation. It outlines a step-by-step procedure to transform input data into a desired output. Algorithms are just like a technology. We all use latest and greatest processors but we need to run implementations of good algorithms on that computer in order to properly take benefits of our money that we spent to have the latest processor.
  • 4.
    Algorithm Specification An algorithmis a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. • Algorithms are definite • Algorithms accpets Input • Algorithms produces Output
  • 5.
    Time and SpaceComplexity ASYMPTOTIC NOTATION: Formal way notation to speak about functions and classify them • Big-O,
  • 6.
    Time and SpaceComplexity • Omega Ω,
  • 7.
    Time and SpaceComplexity • Theta Θ
  • 8.
    Performance Analysis Worst case:-Defines the input for which the algorithm takes more time or input for which algorithm runs the slowest. Best case:- Defines the input for which the algorithm takes less time or input for which algorithm runs the fastest.
  • 9.
    Performance Analysis Average case:-Here we run the algorithm for any number of iterations then compute running time for each trial sum it and divide by the number of trials = (total running time)/(number of trials) Assumes input is random. Note:- lower bound <= average time <= upper bound
  • 10.
    Recurrence Relations Why theyarise in recursive algorithms General form: T(n) = aT(n/b) + f(n) Example: Merge Sort → T(n) = 2T(n/2) + Θ(n)
  • 11.
    Solving Recurrences Methods: 1. Substitutionmethod 2. Iterative method 3. Recursion tree method 4. Master method
  • 12.
    Substitution method The substitutionmethod comprises of 3 steps • Guess the form of the solution • Verify by induction • Solve for constants
  • 13.
    Substitution method Example :T(n)=4T(n/2)+n Step 1:T(n)=O(n3 ) Step 2: verify the induction Assume T(k)<=ck3 T(n)=4T(n/2)+n <=4c(n/2)3 +n <=cn3 /2+n <=cn3 -(cn3 /2-n) T(n)<=cn3 as (cn3 /2 –n) is always positive So what we assumed was true. T(n)=O(n3 ) Step 3: solve for constants Cn3 /2-n>=0 for n>=1 c>=2
  • 14.
    Iterative method T(n)=2T(n/2)+n => 2[2T(n/4)+ n/2 ]+n =>2.2 T(n/4)+n+n => 2.2 [2T(n/8)+ n/4]+2n =>23 T(n/23 ) +3n After k iterations ,T(n)=2k T(n/2k )+kn--------------(1)
  • 15.
    Iterative method Sub problemsize is 1 after n/2k =1 => k=logn So,afterlogn iterations ,the sub-problem size will be 1. So,when k=logn is put in equation 1 T(n)=nT(1)+nlogn =nc+nlogn ( say c=T(1)) =O(nlogn)
  • 16.
  • 17.
  • 18.
    Master Method Case 1:f(n) = O(n(logba – ε) ) → T(n) = Θ(n(logba – ε) ) Case 2: f(n) = Θ(n(logba – ε) ) → T(n) = Θ(n(logba – ε) log n) Case 3: f(n) = Ω(n(logba + ε) ), regularity condition → T(n) = Θ(f(n)) Merge Sort → T(n) = 2T(n/2) + Θ(n) → Case 2 → Θ(n log n) Binary Search → T(n) = T(n/2) + Θ(1) → Case 1 → Θ(log n)
  • 19.
    Divide & Conquer Whatis Divide & Conquer? Steps: Divide → Conquer → Combine Algorithm D and C (P) { if small(P) then return S(P) else { divide P into smaller instances P1 ,P2 .....Pk Apply D and C to each sub problem Return combine (D and C(P1)+ D and C(P2)+.......+D and C(Pk)) }
  • 20.
    Divide & Conquer– Max & Min Find max & min in an array Problem Statement : Given an array X[] of size n, write a program to find the maximum and minimum element in the array. Goal: solve this problem using minimum number of comparisons Naïve O(n) vs Divide & Conquer O(n) with fewer comparisons
  • 21.
    Divide & Conquer– Max & Min Recursive splitting into halves Combine results for max & min Algorithm steps Step 1: Suppose the function call minMax(X[], l, r) returns the maximum and minimum of the array, where l and r are the left and right endpoints. Step 2: Divide array by calculating mid index i.e. mid = l + (r — l)/2 Step 3: Recursively find the maximum and minimum of left part by calling the same function i.e. leftMinMax[2] = minMax(X, l, mid) Step 4: Recursively find the maximum and minimum of right part by calling the same function i.e. rightMinMax[2] = minMax(X, mid + 1, r) Step 5: Finally, get the overall maximum and minimum by comparing the min and max of both halves.