SlideShare a Scribd company logo
1 of 17
Download to read offline
Introduction to algorithms
ANANTHARAMU MP
What is an Algorithm
An algorithm is a set of well-defined instructions in sequence to solve a problem
Algorithm must have :-
1. Precise inputs & outputs
2. Clear and unambiguous steps & instructions
3. Well documented assumptions & constraints
What is an Data Structure
• A data structure is a particular way of organizing data.
• Structure helps to use and process data efficiently
Runtime Performance - Efficiency - Complexity
Efficiency of the
algorithm
Run the algorithm
Memory Consumed
Time Taken
Runtime Performance – Efficiency – Complexity
• How much time algorithm takes to run?
• How much memory algorithm take to run?
• Can we compute Efficiency of algorithm
without running?
• Can we mathematically represent
efficiency of the algorithm?
What is Asymptotic Notation
Mathematical Notation to represent efficiency of an algorithm
Defines the correlation between input value(s) and efficiency
of the algorithm.
Asymptotic notation is machine independent. So, it does not
consider CPU and memory of the system where algorithm
executes.
Why is asymptotic analysis required?
Mathematically represent runtime performance of the
algorithm
Show the correlation between input value(s) and runtime
Compare two algorithms
Coding Interviews. Interviews expect candidate to
compute the efficiency of the logic written.
Types of Asymptotic Analysis. Big Oh – Worst Case
Notation: O(n) where n is the input
Shows only the upper bound or worst time or space taken.
Pros:
Easy to derive
Most Commonly used
Cons:
Not Realistic
1 5 10 15
Search the array 15
100
Types of Asymptotic Analysis. Big Omega – Best Case
Notation: Ω(n). where n is the input
Shows only the lower bound or most optimal efficiency
of the algorithm.
Pros:
Helps represent most
efficient execution
Cons:
Does not cover all the
cases of efficiency
1 5 10 15
Search the array
1
5
Types of Asymptotic Analysis. Big Theta – Average Case
Notation: θ(n). where n is the input
Shows Average or realistic efficiency. (best + worst)/2.
Pros:
Helps calculate exact
or realistic efficiency
Cons:
Harder to compute
1 5 10 15
Search the array 5 15
Big-Oh Examples
O(1): Constant time.
• Input size does not affect(increase/decrease) efficiency
of the algorithm.
• Typically, it is a logic statement or operation.
• Example: Find a number is even or odd.
algorithm(x) {
return (x * 500 + 2);
O(n): Linear Time.
• Efficiency of the algorithm linearly
increases/decreases with input size.
• Typically, it is a loop statement which iterates n times.
• Example: Search for an element in an array
algorithm(x) {
for(int i=0;i<x;i++) {
//Assume O(1) Logic
algorithm (x [ ]) {
for(int
i=0;i<x.length;i++) {
//Assume O(1) Logic
O(logn): Log Time.
• Efficiency of the algorithm linearly increases/decreases with
input size.
• Typically, it is a loop statement which iterates less than n
times.
• This happens when loop iterator is multiplied or divided.
• Find number in first half of the array.
algorithm(x) {
for(int i=0;i<x;i=i*c) {
//Assume O(1) Logic
O(Nc): Polymorphic
• Efficiency of the algorithm polymorphically
increases/decreases with input size.
• Typically, it is a multiple loops which iterate (n pow c) times
where c is > 1
• Example: Matrix multiplication. Bubble sort.
algorithm(x) {
for(int i=0;i<x;i++) {
for(int j=0;j<x;j++) {
//Assume O(1) Logic
Big-Oh Examples
O(2n): Exponential.
• If input is 2 then number of operations is 2 * 2 = 4. If
input is 4 then number of operations is 2 * 2 * 2 * 2 =
16.
• Example: Find subsets of a & b ➔ ‘’, ‘a’, ‘b’, ‘ab’
O(n!): Factorial.
• If input is 3 then number of operations is 3 * 2 * 1 = 6.
If input is 4 then number of operations is 4 * 3 * 2 * 1 =
24.
• Example: find permutations of a string. abc ➔ abc,
acb, bca, bac, cab, cba
O(N2): Quadrilateral
• If executes n * n times
algorithm(x) {
for(int i=0;i<x;i++) {
for(int j=0;j<x;j=j * c) {
//Assume O(1) Logic
O(nlogn): N Log n.
• First loop iterates n times. Second loop iterated log n times.
algorithm(x) {
for(int i=0;i<x;i++) {
for(int j=0;j<x;j=j * c) {
//Assume O(1) Logic
Order of growth
Comparing How increase in input size affect the efficiency.
O(1) < O(logn) < O(n) < O(n logn) < O(n2) < O(n3) < O(2n) < O(n!)
Lowest Highest
Compute Big Oh
Rule#1: Add/multiply Complexity of each logical block.
• O(n) + O(n2) ➔ O (n + n2) //two loops in sequence
• O(2) + O(n) ➔ O(n+2) //two statements followed by loop
• O(n) * O(3) ➔ O(3n) //Loop with three statements
• O(n) * O(n) ➔ O(n2) //Loop inside loop
Rule#2: Ignore constants. Constants do NOT matter.
• O(3n) ➔ O(n),
• O(n + 5) ➔O(n)
• O(n(n/2)) ➔O(nn) ➔ O(n2)
• O(n/2) ➔ O(n)
Rule#3: Ignore lower order terms. As values get large, lower
order terms do NOT matter.
• O(n + n2) ➔ O(n2). O(n) is ignored.
• If block has O(logn) and else block has O(n). Then overall
complexity is O(n). O(logn) is ignored.
Rule#4: Alogithm with 2 or more inputs must be represented
by O(m,n)
Big-Oh for recursion
O(1)
O(1)
Method #1:
• Each function call will have complexity of O(1)
• So for n calls there will be n operations. So efficiency is
O(n)
Method #2:
• Draw recursion graph for some sample values.
• Formula: (complexity of each call) * (Number of branches/calls for each call) ^ (height of the call stack)
factorial (1)
factorial (2)
factorial (3)
factorial (4)
factorial (5)
O(1) * 1 ^ n ➔ O(n)
Big-Oh for Recursion
Method #1:
• Each function call will have complexity of O(1)
• Each call will create 2 more calls. 2 calls will create 4 more
calls.
• So for n calls efficiency will be O(2n)
Method #2:
• Formula: (complexity of each call) * (Number of branches/calls for each call) ^ (height of the call stack)
O(1) * 2 ^ n ➔ O(2n)
O(1)
O(1)
5
4
3
2
1 0
1
2
1 0
3
2
1 0
1
3
2
1 0
1
Fibonacci of 5
Fibonacci of 3
Big-Oh for Recursion
Method #2:
• Assume each call takes n operations (8 here).
• (complexity of each call) * (Number of branches/calls for
each call) ^ (height of the call stack)
• O(n) * 2 ^ (n/2) ➔ n logn
O(1) * 2 ^ n ➔ O(n)
n 1 2 4 8 10
O(n) 2(1/2) = 1.4 2(2/2) = 2 2(4/2) =8 2(8/2)=16 2(10/2)=32
THANK YOU
https://ananthas-scribblepad.blogspot.com

More Related Content

What's hot

please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...
hwbloom27
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
malasumathi
 
Categories for the Working C++ Programmer
Categories for the Working C++ ProgrammerCategories for the Working C++ Programmer
Categories for the Working C++ Programmer
Platonov Sergey
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
chidabdu
 

What's hot (20)

Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Permute
PermutePermute
Permute
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
01 intro to algorithm--updated 2015
01 intro to algorithm--updated 201501 intro to algorithm--updated 2015
01 intro to algorithm--updated 2015
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loops
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Categories for the Working C++ Programmer
Categories for the Working C++ ProgrammerCategories for the Working C++ Programmer
Categories for the Working C++ Programmer
 
Ch9c
Ch9cCh9c
Ch9c
 
Introducction to Algorithm
Introducction to AlgorithmIntroducction to Algorithm
Introducction to Algorithm
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Introduction to python programming [part 1]
Introduction to python programming [part 1]Introduction to python programming [part 1]
Introduction to python programming [part 1]
 
operators and expressions in c++
 operators and expressions in c++ operators and expressions in c++
operators and expressions in c++
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
 

Similar to Brief introduction to Algorithm analysis

how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
Abbas Ali
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
rajatmay1992
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 

Similar to Brief introduction to Algorithm analysis (20)

Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Lec1
Lec1Lec1
Lec1
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Brief introduction to Algorithm analysis

  • 2. What is an Algorithm An algorithm is a set of well-defined instructions in sequence to solve a problem Algorithm must have :- 1. Precise inputs & outputs 2. Clear and unambiguous steps & instructions 3. Well documented assumptions & constraints
  • 3. What is an Data Structure • A data structure is a particular way of organizing data. • Structure helps to use and process data efficiently
  • 4. Runtime Performance - Efficiency - Complexity Efficiency of the algorithm Run the algorithm Memory Consumed Time Taken Runtime Performance – Efficiency – Complexity • How much time algorithm takes to run? • How much memory algorithm take to run? • Can we compute Efficiency of algorithm without running? • Can we mathematically represent efficiency of the algorithm?
  • 5. What is Asymptotic Notation Mathematical Notation to represent efficiency of an algorithm Defines the correlation between input value(s) and efficiency of the algorithm. Asymptotic notation is machine independent. So, it does not consider CPU and memory of the system where algorithm executes.
  • 6. Why is asymptotic analysis required? Mathematically represent runtime performance of the algorithm Show the correlation between input value(s) and runtime Compare two algorithms Coding Interviews. Interviews expect candidate to compute the efficiency of the logic written.
  • 7. Types of Asymptotic Analysis. Big Oh – Worst Case Notation: O(n) where n is the input Shows only the upper bound or worst time or space taken. Pros: Easy to derive Most Commonly used Cons: Not Realistic 1 5 10 15 Search the array 15 100
  • 8. Types of Asymptotic Analysis. Big Omega – Best Case Notation: Ω(n). where n is the input Shows only the lower bound or most optimal efficiency of the algorithm. Pros: Helps represent most efficient execution Cons: Does not cover all the cases of efficiency 1 5 10 15 Search the array 1 5
  • 9. Types of Asymptotic Analysis. Big Theta – Average Case Notation: θ(n). where n is the input Shows Average or realistic efficiency. (best + worst)/2. Pros: Helps calculate exact or realistic efficiency Cons: Harder to compute 1 5 10 15 Search the array 5 15
  • 10. Big-Oh Examples O(1): Constant time. • Input size does not affect(increase/decrease) efficiency of the algorithm. • Typically, it is a logic statement or operation. • Example: Find a number is even or odd. algorithm(x) { return (x * 500 + 2); O(n): Linear Time. • Efficiency of the algorithm linearly increases/decreases with input size. • Typically, it is a loop statement which iterates n times. • Example: Search for an element in an array algorithm(x) { for(int i=0;i<x;i++) { //Assume O(1) Logic algorithm (x [ ]) { for(int i=0;i<x.length;i++) { //Assume O(1) Logic O(logn): Log Time. • Efficiency of the algorithm linearly increases/decreases with input size. • Typically, it is a loop statement which iterates less than n times. • This happens when loop iterator is multiplied or divided. • Find number in first half of the array. algorithm(x) { for(int i=0;i<x;i=i*c) { //Assume O(1) Logic O(Nc): Polymorphic • Efficiency of the algorithm polymorphically increases/decreases with input size. • Typically, it is a multiple loops which iterate (n pow c) times where c is > 1 • Example: Matrix multiplication. Bubble sort. algorithm(x) { for(int i=0;i<x;i++) { for(int j=0;j<x;j++) { //Assume O(1) Logic
  • 11. Big-Oh Examples O(2n): Exponential. • If input is 2 then number of operations is 2 * 2 = 4. If input is 4 then number of operations is 2 * 2 * 2 * 2 = 16. • Example: Find subsets of a & b ➔ ‘’, ‘a’, ‘b’, ‘ab’ O(n!): Factorial. • If input is 3 then number of operations is 3 * 2 * 1 = 6. If input is 4 then number of operations is 4 * 3 * 2 * 1 = 24. • Example: find permutations of a string. abc ➔ abc, acb, bca, bac, cab, cba O(N2): Quadrilateral • If executes n * n times algorithm(x) { for(int i=0;i<x;i++) { for(int j=0;j<x;j=j * c) { //Assume O(1) Logic O(nlogn): N Log n. • First loop iterates n times. Second loop iterated log n times. algorithm(x) { for(int i=0;i<x;i++) { for(int j=0;j<x;j=j * c) { //Assume O(1) Logic
  • 12. Order of growth Comparing How increase in input size affect the efficiency. O(1) < O(logn) < O(n) < O(n logn) < O(n2) < O(n3) < O(2n) < O(n!) Lowest Highest
  • 13. Compute Big Oh Rule#1: Add/multiply Complexity of each logical block. • O(n) + O(n2) ➔ O (n + n2) //two loops in sequence • O(2) + O(n) ➔ O(n+2) //two statements followed by loop • O(n) * O(3) ➔ O(3n) //Loop with three statements • O(n) * O(n) ➔ O(n2) //Loop inside loop Rule#2: Ignore constants. Constants do NOT matter. • O(3n) ➔ O(n), • O(n + 5) ➔O(n) • O(n(n/2)) ➔O(nn) ➔ O(n2) • O(n/2) ➔ O(n) Rule#3: Ignore lower order terms. As values get large, lower order terms do NOT matter. • O(n + n2) ➔ O(n2). O(n) is ignored. • If block has O(logn) and else block has O(n). Then overall complexity is O(n). O(logn) is ignored. Rule#4: Alogithm with 2 or more inputs must be represented by O(m,n)
  • 14. Big-Oh for recursion O(1) O(1) Method #1: • Each function call will have complexity of O(1) • So for n calls there will be n operations. So efficiency is O(n) Method #2: • Draw recursion graph for some sample values. • Formula: (complexity of each call) * (Number of branches/calls for each call) ^ (height of the call stack) factorial (1) factorial (2) factorial (3) factorial (4) factorial (5) O(1) * 1 ^ n ➔ O(n)
  • 15. Big-Oh for Recursion Method #1: • Each function call will have complexity of O(1) • Each call will create 2 more calls. 2 calls will create 4 more calls. • So for n calls efficiency will be O(2n) Method #2: • Formula: (complexity of each call) * (Number of branches/calls for each call) ^ (height of the call stack) O(1) * 2 ^ n ➔ O(2n) O(1) O(1) 5 4 3 2 1 0 1 2 1 0 3 2 1 0 1 3 2 1 0 1 Fibonacci of 5 Fibonacci of 3
  • 16. Big-Oh for Recursion Method #2: • Assume each call takes n operations (8 here). • (complexity of each call) * (Number of branches/calls for each call) ^ (height of the call stack) • O(n) * 2 ^ (n/2) ➔ n logn O(1) * 2 ^ n ➔ O(n) n 1 2 4 8 10 O(n) 2(1/2) = 1.4 2(2/2) = 2 2(4/2) =8 2(8/2)=16 2(10/2)=32