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,
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
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
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)
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.