SlideShare a Scribd company logo
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.
Algorithms & Complexity Calculation
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 !!!
Ad

Recommended

Intro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
Akhil Kaushik
 
Decision Making & Loops
Decision Making & Loops
Akhil Kaushik
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Python Tutorial Part 1
Python Tutorial Part 1
Haitham El-Ghareeb
 
Python Data Types
Python Data Types
athithanvijay
 
Logic programming in python
Logic programming in python
Pierre Carbonnelle
 
Python Data-Types
Python Data-Types
Akhil Kaushik
 
Python 3 Programming Language
Python 3 Programming Language
Tahani Al-Manie
 
Python recursion
Python recursion
Prof. Dr. K. Adisesha
 
Full Python in 20 slides
Full Python in 20 slides
rfojdar
 
Python algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
James Long
 
Introduction To Programming with Python
Introduction To Programming with Python
Sushant Mane
 
Introduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python programming
Python programming
saroja20
 
Python Programming Language
Python Programming Language
Dr.YNM
 
Python programming
Python programming
saroja20
 
Datatype
Datatype
baran19901990
 
Introduction to Python programming Language
Introduction to Python programming Language
MansiSuthar3
 
Scheme Programming Language
Scheme Programming Language
Reham AlBlehid
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
Avelin Huo
 
An Introduction to ANTLR
An Introduction to ANTLR
Morteza Zakeri
 
Python-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Python basics
Python basics
TIB Academy
 
Python libraries
Python libraries
Prof. Dr. K. Adisesha
 
Scheme language
Scheme language
JITENDRA LENKA
 
Introduction to Structure Programming with C++
Introduction to Structure Programming with C++
Mohamed Essam
 
Functional programming ideas in python
Functional programming ideas in python
Manish Tomar
 
DSA
DSA
rrupa2
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
HODElex
 

More Related Content

What's hot (20)

Python recursion
Python recursion
Prof. Dr. K. Adisesha
 
Full Python in 20 slides
Full Python in 20 slides
rfojdar
 
Python algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
James Long
 
Introduction To Programming with Python
Introduction To Programming with Python
Sushant Mane
 
Introduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python programming
Python programming
saroja20
 
Python Programming Language
Python Programming Language
Dr.YNM
 
Python programming
Python programming
saroja20
 
Datatype
Datatype
baran19901990
 
Introduction to Python programming Language
Introduction to Python programming Language
MansiSuthar3
 
Scheme Programming Language
Scheme Programming Language
Reham AlBlehid
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
Avelin Huo
 
An Introduction to ANTLR
An Introduction to ANTLR
Morteza Zakeri
 
Python-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Python basics
Python basics
TIB Academy
 
Python libraries
Python libraries
Prof. Dr. K. Adisesha
 
Scheme language
Scheme language
JITENDRA LENKA
 
Introduction to Structure Programming with C++
Introduction to Structure Programming with C++
Mohamed Essam
 
Functional programming ideas in python
Functional programming ideas in python
Manish Tomar
 
Full Python in 20 slides
Full Python in 20 slides
rfojdar
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
James Long
 
Introduction To Programming with Python
Introduction To Programming with Python
Sushant Mane
 
Introduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python programming
Python programming
saroja20
 
Python Programming Language
Python Programming Language
Dr.YNM
 
Python programming
Python programming
saroja20
 
Introduction to Python programming Language
Introduction to Python programming Language
MansiSuthar3
 
Scheme Programming Language
Scheme Programming Language
Reham AlBlehid
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
Avelin Huo
 
An Introduction to ANTLR
An Introduction to ANTLR
Morteza Zakeri
 
Python-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Introduction to Structure Programming with C++
Introduction to Structure Programming with C++
Mohamed Essam
 
Functional programming ideas in python
Functional programming ideas in python
Manish Tomar
 

Similar to Algorithms & Complexity Calculation (20)

DSA
DSA
rrupa2
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
HODElex
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
DevaKumari Vijay
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
RahikAhmed1
 
DAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
Algorithm for the DAA agscsnak javausmagagah
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
design analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
Algorithms overview
Algorithms overview
Deborah Akuoko
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Lec1
Lec1
Nikhil Chilwant
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
Algorithm.pptx
Algorithm.pptx
Koteswari Kasireddy
 
Algorithm.pptx
Algorithm.pptx
Koteswari Kasireddy
 
Algorithm analysis and design
Algorithm analysis and design
Megha V
 
Unit 1.pptx
Unit 1.pptx
DeepakYadav656387
 
Annotations.pdf
Annotations.pdf
GauravKumar295392
 
Analysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
Design Analysis and Algorithm Module1.pdf
Design Analysis and Algorithm Module1.pdf
Shana799280
 
Aad introduction
Aad introduction
Mr SMAK
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
HODElex
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
DevaKumari Vijay
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
RahikAhmed1
 
DAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
Algorithm for the DAA agscsnak javausmagagah
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
design analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
Algorithm analysis and design
Algorithm analysis and design
Megha V
 
Analysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
Design Analysis and Algorithm Module1.pdf
Design Analysis and Algorithm Module1.pdf
Shana799280
 
Aad introduction
Aad introduction
Mr SMAK
 
Ad

More from Akhil Kaushik (17)

Code Optimization
Code Optimization
Akhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Context Free Grammar
Context Free Grammar
Akhil Kaushik
 
Error Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Symbol Table
Symbol Table
Akhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer Implementation
Akhil Kaushik
 
NFA & DFA
NFA & DFA
Akhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
File Handling Python
File Handling Python
Akhil Kaushik
 
Regular Expressions
Regular Expressions
Akhil Kaushik
 
Basic programs in Python
Basic programs in Python
Akhil Kaushik
 
Introduction to Python Programming
Introduction to Python Programming
Akhil Kaushik
 
Compiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in Compiler
Akhil Kaushik
 
Compiler construction tools
Compiler construction tools
Akhil Kaushik
 
Phases of compiler
Phases of compiler
Akhil Kaushik
 
Introduction to Compilers
Introduction to Compilers
Akhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Context Free Grammar
Context Free Grammar
Akhil Kaushik
 
Error Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer Implementation
Akhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
File Handling Python
File Handling Python
Akhil Kaushik
 
Basic programs in Python
Basic programs in Python
Akhil Kaushik
 
Introduction to Python Programming
Introduction to Python Programming
Akhil Kaushik
 
Compiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in Compiler
Akhil Kaushik
 
Compiler construction tools
Compiler construction tools
Akhil Kaushik
 
Introduction to Compilers
Introduction to Compilers
Akhil Kaushik
 
Ad

Recently uploaded (20)

Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 

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.
  • 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