SlideShare a Scribd company logo
1 of 33
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Algorithms
What is Algorithm?
• Algorithm is a step by step procedure.
• It defines a set of instructions to be executed in
certain order to get the desired output.
• Algorithms are generally created independent of
underlying languages.
Algorithm Development
• Good, logical programming is developed through
good pre-code planning and organization.
• This is assisted by the use of pseudocode and
program flowcharts.
• Flowcharts are written with program flow from the
top of a page to the bottom.
• Each command is placed in a box of the appropriate
shape, and arrows are used to direct program flow.
Pseudocode
• It is a method of describing computer algorithms using a
combination of natural and programming language.
• The usual Fortran symbols are used for arithmetic
operations (+, -, *, / ).
• Symbolic names are used to indicate the quantities being
processed.
• Certain Fortran keywords can be used, such as PRINT,
WRITE, READ, etc.
• Indentation should be used to indicate branches and loops
of instruction.
Example:- Pseudocode to Add Two Nos.
step 1 − START
step 2 − Declare three integers a, b & c
step 3 − define values of a & b
step 4 − Add values of a & b
step 5 − Store output of step 4 to c
step 6 − Print value of c
step 7 − STOP
Characteristics of an Algorithm
• Unambiguous − Algorithm should be clear and unambiguous.
• Input − An algorithm should have 0 or more well defined inputs.
• Output − An algorithm should have 1 or more well defined
outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of
steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step
directions which should be independent of any programming
code.
Algo Design Techniques
1. Top down
2. Bottom up
3. Incremental
4. Divide and conquer
5. Greedy.
6. Dynamic approach
Top-Down Design
• Divide main program into a main module and its
related module.
• Divide each module in sub module according to s/w
engineering and programming style.
• Division of module continues till elementary process
that cant be divided.
• In this each module has single entry and single exit
point.
• In c language it is done by functions.
Bottom-Up Approach
• Its principle is to start with specific module and
built into more complex structure, ending at top.
• It is widely used for testing because each of
lowest level function is written and tested first.
• This strategy often resembles a "seed" model, by
which the beginnings are small but eventually
grow in complexity and completeness.
Incremental Approach
• This approach start with single module then
adding module to finally build the whole
algorithm.
• It is piecing together of systems to give rise to
more complex systems.
Divide and Conquer
• This approach divide the original
problem into sub problems.
• Solve each problem individually.
• Finally combine solution of top
sub problems into a solution of
whole problem.
Greedy Approach
• It seeks to optimize a function by making choice
which are best locally but not globally.
• Result is good solution but necessarily not best.
Dynamic Approach
• It is a technique for efficiently computing
recurrences by storing partial results.
• It is a method of solving problems exhibiting
properties of overlapping sub problems.
Algorithm Analysis
• A priori analysis − This is theoretical analysis of an
algorithm.
– Efficiency of algorithm is measured by assuming that all
other factors e.g. processor speed, are constant and
have no effect on implementation.
• A posterior analysis − The selected algorithm is
implemented using programming language.
– This is then executed on target computer machine. In this
analysis, actual statistics like running time and space
required, are collected.
Algorithm Complexity
• Time Factor − It is measured by counting the no. of
key operations such as comparisons in sorting
algorithm
• Space Factor − The space is measured by counting
the maximum memory space required by the algo.
• The complexity of an algorithm f(n) gives the
running time and / or storage space required by the
algorithm in terms of n as the size of input data.
Space Complexity
• It is amount of memory space required by the
algorithm in its life cycle.
• Space required by an algo is equal to the sum of the
following two components:-
– A fixed part is a space required to store certain data and
variables, that are independent of the size of the
problem.
– A variable part is a space required by variables, whose
size depends on the size of the problem. For example
dynamic memory allocation, recursion stack space etc.
Time Complexity
• It is amount of time required by the algorithm to run
to completion.
• Time requirements can be defined as a numerical
function T(n), which is measured as the number of
steps, provided each step consumes constant time.
• Ex: Addition of two n-bit integers takes n steps.
Thus, the total computational time is T(n) = c*n,
where c is the time taken for addition of two bits.
– Here, we observe that T(n) grows linearly as input size
increases.
Asymptotic Analysis
• Usually, time required by an
algorithm falls under three types −
• Best Case − Minimum time required
for program execution(Ω).
• Average Case − Average time
required for program execution(θ).
• Worst Case − Maximum time
required for program execution(O).
Omega Notation, Ω
• The Ω(n) is the formal way to express the lower
bound of an algorithm's running time.
• It measures best amount of time an algorithm can
possibly take to complete.
• Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
• The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running
time. It is represented as following −
• θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) =
Ω(f(n)) for all n > n0. }
Big Oh Notation, Ο
• The Ο(n) is the formal way to express the upper
bound of an algorithm's running time.
• It measures the worst case or longest amount of
time an algorithm can possibly take to complete.
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0. }
Common Asymptotic Notations
• Constant−Ο(1)
• Logarithmic−Ο(log n) or O(log(log(n))
• Linear−Ο(n) or O(log(n2))
• Quadratic − Ο(n log n) or Ο(n2)
• Cubic−Ο(n3)
• Polynomial−O(nk), where k>0
• Exponential−O(cn), where c>1. Ex: O(2n)
Common Asymptotic Notations
Common Asymptotic Notations
• Constant − Ο(1) -> When algo takes same amount
of time to compute regardless of input size.
• Ex: 1. If a no. is even or odd
• 2. If an item on an array is null
• 3. Print 1st element from list
• 4. Find a value on map
Common Asymptotic Notations
• Linear − Ο(n) -> As input grows, algo takes
proportionally longer.
• Ex: 1. Get max/ min value in array
• 2. Find/ search an element in a collection
• 3. Print all values in a list
Common Asymptotic Notations
• Quadratic − Ο(n2) -> If the input size of 2, it will do
4 operations. If input is 8, it will do 64 operations.
• Ex: 1. Sorting – Bubble, Insertion, Selection
• 2. Check if array has duplicated values
• 3. Find all possible ordered pairs in array
Common Asymptotic Notations
• Polynomial−O(nc) -> Here, c>1. Here, it takes
huge running times as input grow.
• Ex: Triple nested loop has O(n3)
• Quadratic O(n2) and Cubic O(n3) come under this
category.
Common Asymptotic Notations
• Logarithmic − Ο(log n) -> Here, it uses divide-&-
conquer strategy to divide the problems into half.
– Ex: 1. Find a word in dictionary
– 2. Find a person on phone book
• Linear Arithmetic - O(n log(n)) -> It is slower than
linear, but better than a quadratic algo.
– Ex: Sorting – Merge sort, quick sort, etc.
Common Asymptotic Notations
• Exponential – O(2n) -> Calculations performed by
algo double every time as input grows.
– Ex: 1. Power set – find all subsets on set.
– 2. Fibonacci series or Travelling salesman problem.
• Factorial – O(n!) -> Most calculations are worst
case scenarios.
Examples
• If f(n) = 7(2n2+5), then O(f(n)) is n2
• If f(n) = 5 + 4n, then O(n)
• If f(n) = n3 + 4n2 + 20n + 1, then O(n3)
• If f(n) = n & g(n) = n2, then f(n) < g(n),
Hence f(n) = O(g(n))
Examples
• If f(n) = n, g(n) = n2 & h(n) = n3, then
f(n) = O(g(n)), g(n) = O(h(n)) & thus f(n) = O(h(n))
• If f(n) = n2 log n & g(n) = n(log n)10, then:
Hence, f(n) = Ω(g(n))
f(n)
n2 log n
n.n log n
n
g(n)
n(log n)10
n.log n (log n)9
(log n)9
Examples
• If f(n) = n2 & g(n) = 2n, then:
Hence, f(n) = O(g(n)) for n>=4
f(n)
0 for n=0
1 for n=1
4 for n=2
9 for n=3
16 for n=4
25 for n=5
36 for n=6
g(n)
1 for n=0
2 for n=1
4 for n=2
8 for n=3
16 for n=4
32 for n=5
64 for n=6
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

What's hot

Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slidesrfojdar
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhoneThe Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhoneJames Long
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with PythonSushant Mane
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingSrinivas Narasegouda
 
Python programming
Python programmingPython programming
Python programmingsaroja20
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageDr.YNM
 
Python programming
Python programmingPython programming
Python programmingsaroja20
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming LanguageMansiSuthar3
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming LanguageReham AlBlehid
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Avelin Huo
 
An Introduction to ANTLR
An Introduction to ANTLRAn Introduction to ANTLR
An Introduction to ANTLRMorteza Zakeri
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Introduction to Structure Programming with C++
Introduction to Structure Programming with C++Introduction to Structure Programming with C++
Introduction to Structure Programming with C++Mohamed Essam
 
Functional programming ideas in python
Functional programming ideas in pythonFunctional programming ideas in python
Functional programming ideas in pythonManish Tomar
 

What's hot (20)

Python recursion
Python recursionPython recursion
Python recursion
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhoneThe Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Python programming
Python programmingPython programming
Python programming
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python programming
Python programmingPython programming
Python programming
 
Datatype
DatatypeDatatype
Datatype
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming Language
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
 
An Introduction to ANTLR
An Introduction to ANTLRAn Introduction to ANTLR
An Introduction to ANTLR
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python basics
Python basicsPython basics
Python basics
 
Python libraries
Python librariesPython libraries
Python libraries
 
Scheme language
Scheme languageScheme language
Scheme language
 
Introduction to Structure Programming with C++
Introduction to Structure Programming with C++Introduction to Structure Programming with C++
Introduction to Structure Programming with C++
 
Functional programming ideas in python
Functional programming ideas in pythonFunctional programming ideas in python
Functional programming ideas in python
 

Similar to Algorithms & Complexity Calculation

2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and NotationsAbid Kohistani
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxSyed Zaid Irshad
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introductionssuseraf8b2f
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptHODElex
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 

Similar to Algorithms & Complexity Calculation (20)

2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and Notations
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
DSA
DSADSA
DSA
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 

More from Akhil Kaushik

Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & RecoveryAkhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer ImplementationAkhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in PythonAkhil Kaushik
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in CompilerAkhil Kaushik
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to CompilersAkhil Kaushik
 

More from Akhil Kaushik (17)

Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 

Recently uploaded

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Recently uploaded (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Algorithms & Complexity Calculation

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Algorithms
  • 2. What is Algorithm? • Algorithm is a step by step procedure. • It defines a set of instructions to be executed in certain order to get the desired output. • Algorithms are generally created independent of underlying languages.
  • 3. Algorithm Development • Good, logical programming is developed through good pre-code planning and organization. • This is assisted by the use of pseudocode and program flowcharts. • Flowcharts are written with program flow from the top of a page to the bottom. • Each command is placed in a box of the appropriate shape, and arrows are used to direct program flow.
  • 4.
  • 5. Pseudocode • It is a method of describing computer algorithms using a combination of natural and programming language. • The usual Fortran symbols are used for arithmetic operations (+, -, *, / ). • Symbolic names are used to indicate the quantities being processed. • Certain Fortran keywords can be used, such as PRINT, WRITE, READ, etc. • Indentation should be used to indicate branches and loops of instruction.
  • 6. Example:- Pseudocode to Add Two Nos. step 1 − START step 2 − Declare three integers a, b & c step 3 − define values of a & b step 4 − Add values of a & b step 5 − Store output of step 4 to c step 6 − Print value of c step 7 − STOP
  • 7. Characteristics of an Algorithm • Unambiguous − Algorithm should be clear and unambiguous. • Input − An algorithm should have 0 or more well defined inputs. • Output − An algorithm should have 1 or more well defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources. • Independent − An algorithm should have step-by-step directions which should be independent of any programming code.
  • 8. Algo Design Techniques 1. Top down 2. Bottom up 3. Incremental 4. Divide and conquer 5. Greedy. 6. Dynamic approach
  • 9. Top-Down Design • Divide main program into a main module and its related module. • Divide each module in sub module according to s/w engineering and programming style. • Division of module continues till elementary process that cant be divided. • In this each module has single entry and single exit point. • In c language it is done by functions.
  • 10. Bottom-Up Approach • Its principle is to start with specific module and built into more complex structure, ending at top. • It is widely used for testing because each of lowest level function is written and tested first. • This strategy often resembles a "seed" model, by which the beginnings are small but eventually grow in complexity and completeness.
  • 11. Incremental Approach • This approach start with single module then adding module to finally build the whole algorithm. • It is piecing together of systems to give rise to more complex systems.
  • 12. Divide and Conquer • This approach divide the original problem into sub problems. • Solve each problem individually. • Finally combine solution of top sub problems into a solution of whole problem.
  • 13. Greedy Approach • It seeks to optimize a function by making choice which are best locally but not globally. • Result is good solution but necessarily not best. Dynamic Approach • It is a technique for efficiently computing recurrences by storing partial results. • It is a method of solving problems exhibiting properties of overlapping sub problems.
  • 14. Algorithm Analysis • A priori analysis − This is theoretical analysis of an algorithm. – Efficiency of algorithm is measured by assuming that all other factors e.g. processor speed, are constant and have no effect on implementation. • A posterior analysis − The selected algorithm is implemented using programming language. – This is then executed on target computer machine. In this analysis, actual statistics like running time and space required, are collected.
  • 15. Algorithm Complexity • Time Factor − It is measured by counting the no. of key operations such as comparisons in sorting algorithm • Space Factor − The space is measured by counting the maximum memory space required by the algo. • The complexity of an algorithm f(n) gives the running time and / or storage space required by the algorithm in terms of n as the size of input data.
  • 16. Space Complexity • It is amount of memory space required by the algorithm in its life cycle. • Space required by an algo is equal to the sum of the following two components:- – A fixed part is a space required to store certain data and variables, that are independent of the size of the problem. – A variable part is a space required by variables, whose size depends on the size of the problem. For example dynamic memory allocation, recursion stack space etc.
  • 17. Time Complexity • It is amount of time required by the algorithm to run to completion. • Time requirements can be defined as a numerical function T(n), which is measured as the number of steps, provided each step consumes constant time. • Ex: Addition of two n-bit integers takes n steps. Thus, the total computational time is T(n) = c*n, where c is the time taken for addition of two bits. – Here, we observe that T(n) grows linearly as input size increases.
  • 18. Asymptotic Analysis • Usually, time required by an algorithm falls under three types − • Best Case − Minimum time required for program execution(Ω). • Average Case − Average time required for program execution(θ). • Worst Case − Maximum time required for program execution(O).
  • 19. Omega Notation, Ω • The Ω(n) is the formal way to express the lower bound of an algorithm's running time. • It measures best amount of time an algorithm can possibly take to complete. • Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
  • 20. Theta Notation, θ • The θ(n) is the formal way to express both the lower bound and upper bound of an algorithm's running time. It is represented as following − • θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
  • 21. Big Oh Notation, Ο • The Ο(n) is the formal way to express the upper bound of an algorithm's running time. • It measures the worst case or longest amount of time an algorithm can possibly take to complete. Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
  • 22. Common Asymptotic Notations • Constant−Ο(1) • Logarithmic−Ο(log n) or O(log(log(n)) • Linear−Ο(n) or O(log(n2)) • Quadratic − Ο(n log n) or Ο(n2) • Cubic−Ο(n3) • Polynomial−O(nk), where k>0 • Exponential−O(cn), where c>1. Ex: O(2n)
  • 24. Common Asymptotic Notations • Constant − Ο(1) -> When algo takes same amount of time to compute regardless of input size. • Ex: 1. If a no. is even or odd • 2. If an item on an array is null • 3. Print 1st element from list • 4. Find a value on map
  • 25. Common Asymptotic Notations • Linear − Ο(n) -> As input grows, algo takes proportionally longer. • Ex: 1. Get max/ min value in array • 2. Find/ search an element in a collection • 3. Print all values in a list
  • 26. Common Asymptotic Notations • Quadratic − Ο(n2) -> If the input size of 2, it will do 4 operations. If input is 8, it will do 64 operations. • Ex: 1. Sorting – Bubble, Insertion, Selection • 2. Check if array has duplicated values • 3. Find all possible ordered pairs in array
  • 27. Common Asymptotic Notations • Polynomial−O(nc) -> Here, c>1. Here, it takes huge running times as input grow. • Ex: Triple nested loop has O(n3) • Quadratic O(n2) and Cubic O(n3) come under this category.
  • 28. Common Asymptotic Notations • Logarithmic − Ο(log n) -> Here, it uses divide-&- conquer strategy to divide the problems into half. – Ex: 1. Find a word in dictionary – 2. Find a person on phone book • Linear Arithmetic - O(n log(n)) -> It is slower than linear, but better than a quadratic algo. – Ex: Sorting – Merge sort, quick sort, etc.
  • 29. Common Asymptotic Notations • Exponential – O(2n) -> Calculations performed by algo double every time as input grows. – Ex: 1. Power set – find all subsets on set. – 2. Fibonacci series or Travelling salesman problem. • Factorial – O(n!) -> Most calculations are worst case scenarios.
  • 30. Examples • If f(n) = 7(2n2+5), then O(f(n)) is n2 • If f(n) = 5 + 4n, then O(n) • If f(n) = n3 + 4n2 + 20n + 1, then O(n3) • If f(n) = n & g(n) = n2, then f(n) < g(n), Hence f(n) = O(g(n))
  • 31. Examples • If f(n) = n, g(n) = n2 & h(n) = n3, then f(n) = O(g(n)), g(n) = O(h(n)) & thus f(n) = O(h(n)) • If f(n) = n2 log n & g(n) = n(log n)10, then: Hence, f(n) = Ω(g(n)) f(n) n2 log n n.n log n n g(n) n(log n)10 n.log n (log n)9 (log n)9
  • 32. Examples • If f(n) = n2 & g(n) = 2n, then: Hence, f(n) = O(g(n)) for n>=4 f(n) 0 for n=0 1 for n=1 4 for n=2 9 for n=3 16 for n=4 25 for n=5 36 for n=6 g(n) 1 for n=0 2 for n=1 4 for n=2 8 for n=3 16 for n=4 32 for n=5 64 for n=6
  • 33. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!