CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS
description
Transcript of CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS
![Page 1: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/1.jpg)
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS
![Page 2: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/2.jpg)
Definitions of Algorithm
A mathematical relation between an observed quantity and a variable used in a step-by-step mathematical process to calculate a quantity
Algorithm is any well defined computational procedure that takes some value or set of values as input and produces some value or set of values as output
A procedure for solving a mathematical problem in a finite number of steps that frequently involves repetition of an operation; broadly : a step-by-step procedure for solving a problem or accomplishing some end (Webster’s Dictionary)
![Page 3: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/3.jpg)
Analysis of Algorithms Involves evaluating the following parameters
Memory – Unit generalized as “WORDS” Computer time – Unit generalized as
“CYCLES” Correctness – Producing the desired
output
![Page 4: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/4.jpg)
Sample Algorithm
FINDING LARGEST NUMBERINPUT: unsorted array ‘A[n]’of n numbersOUTPUT: largest number----------------------------------------------------------1 large ← A[j]2 for j ← 2 to length[A]3 if large < A[j]4 large ← A[j]5 end
![Page 5: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/5.jpg)
Space and Time AnalysisSpace and Time Analysis(Largest Number Scan Algorithm)(Largest Number Scan Algorithm)
SPACE S(n): One “word” is required to run the algorithm (step 1…to store variable ‘large’)
TIME T(n): n-1 comparisons are required to find the largest (every comparison takes one cycle)
*Aim is to reduce both T(n) and S(n)
![Page 6: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/6.jpg)
ASYMPTOTICS
Used to formalize that an algorithm has running time or storage requirements that are ``never more than,'' ``always greater than,'' or ``exactly'' some amount
![Page 7: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/7.jpg)
ASYMPTOTICS NOTATIONSO-notation (Big Oh)
Asymptotic Upper Bound For a given function g(n), we denote O(g(n)) as the set of
functions: O(g(n)) = { f(n)| there exists positive constants c and n0 such that 0 ≤ f(n) ≤ c g(n) for all n ≥ n0 }
![Page 8: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/8.jpg)
ASYMPTOTICS NOTATIONSΘ-notation Asymptotic tight bound Θ (g(n)) represents a set of functions such
that: Θ (g(n)) = {f(n): there exist positive
constants c1, c2, and n0 such
that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)
for all n≥ n0}
![Page 9: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/9.jpg)
ASYMPTOTICS NOTATIONSΩ-notation
Asymptotic lower bound Ω (g(n)) represents a set of functions such
that: Ω(g(n)) = {f(n): there exist positive
constants c and n0 such that
0 ≤ c g(n) ≤ f(n) for all n≥ n0}
![Page 10: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/10.jpg)
O-notation ------------------
Θ-notation ------------------
Ω-notation ------------------
Less than equal to (“≤”)
Equal to (“=“)
Greater than equal to
(“≥”)
![Page 11: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/11.jpg)
Mappings for n2
Ω (n2 )
O(n2 )
Θ(n2)
![Page 12: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/12.jpg)
Bounds of a Function
Cntd…
![Page 13: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/13.jpg)
Cntd…
c1 , c2 & n0 -> constants
T(n) exists between c1n & c2n
Below n0 we do not plot T(n)
T(n) becomes significant only above n0
![Page 14: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/14.jpg)
Common plots of O( )
O(2n)O(n3 )
O(n2)O(nlogn)
O(n)
O(√n)
O(logn)
O(1)
![Page 15: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/15.jpg)
Examples of algorithms for sorting techniques and their complexities
Insertion sort : O(n2) Selection sort : O(n2) Quick sort : O(n logn) Merge sort : O(n logn)
![Page 16: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/16.jpg)
RECURRENCE RELATIONS
A Recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs
Special techniques are required to analyze the space and time required
![Page 17: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/17.jpg)
RECURRENCE RELATIONS EXAMPLEEXAMPLE 1: QUICK SORTT(n)= 2T(n/2) + O(n)T(1)= O(1) In the above case the presence of function of T on both
sides of the equation signifies the presence of recurrence relation
(SUBSTITUTION MEATHOD used) The equations are simplified to produce the final result:
……cntd
![Page 18: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/18.jpg)
T(n) = 2T(n/2) + O(n)
= 2(2(n/22) + (n/2)) + n
= 22 T(n/22) + n + n
= 22 (T(n/23)+ (n/22)) + n + n
= 23 T(n/23) + n + n + n
= n log n
Cntd….
![Page 19: CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS](https://reader036.fdocuments.us/reader036/viewer/2022082517/56813a06550346895da1d05b/html5/thumbnails/19.jpg)
Cntd…
EXAMPLE 2: BINARY SEARCH
T(n)=O(1) + T(n/2)
T(1)=1
Above is another example of recurrence relation and the way to solve it is by Substitution.
T(n)=T(n/2) +1
= T(n/22)+1+1
= T(n/23)+1+1+1
= logn
T(n)= O(logn)