Analysis and Design of Algorithms
Analysis of Algorithms I
Analysis and Design of Algorithms
Analysis of Algorithms
Time complexity
Asymptotic Notations
Big O Notation
Growth Orders
Problems
Analysis and Design of Algorithms
Analysis of Algorithms is the determination of the
amount of time, storage and/or other resources necessary
to execute them.
Analyzing algorithms is called Asymptotic Analysis
Asymptotic Analysis evaluate the performance of an
algorithm
Analysis and Design of Algorithms
Time complexity
Analysis and Design of Algorithms
 time complexity of an algorithm quantifies the amount of time taken
by an algorithm
 We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Analysis and Design of Algorithms
 Assume the below algorithm using Python code:
Analysis and Design of Algorithms
 Worst Case Analysis: In the worst case analysis, we calculate upper
bound on running time of an algorithm.
Analysis and Design of Algorithms
 Worst Case Analysis: the case that causes maximum number of
operations to be executed.
 For Linear Search, the worst case happens when the element to be
searched is not present in the array. (example : search for number
8)
2 3 5 4 1 7 6
Analysis and Design of Algorithms
 Worst Case Analysis: When x is not present, the search() functions
compares it with all the elements of arr one by one.
Analysis and Design of Algorithms
 The worst case time complexity of linear search would be O(n).
Analysis and Design of Algorithms
 Average Case Analysis: we take all possible inputs and calculate
computing time for all of the inputs.
Analysis and Design of Algorithms
 Best Case Analysis: calculate lower bound on running time of an
algorithm.
Analysis and Design of Algorithms
 The best case time complexity of linear search would be O(1).
Analysis and Design of Algorithms
 Best Case Analysis: the case that causes minimum number of
operations to be executed.
 For Linear Search, the best case occurs when x is present at the first
location. (example : search for number 2)
 So time complexity in the best case would be Θ(1)
2 3 5 4 1 7 6
Analysis and Design of Algorithms
 Most of the times, we do worst case analysis to analyze algorithms.
 The average case analysis is not easy to do in most of the practical
cases and it is rarely done.
 The Best case analysis is bogus. Guaranteeing a lower bound on an
algorithm doesn’t provide any information.
Analysis and Design of Algorithms
1) Big O Notation: is an Asymptotic Notation for the worst case.
2) Ω Notation (omega notation): is an Asymptotic Notation for the
best case.
3) Θ Notation (theta notation) : is an Asymptotic Notation for the
worst case and the best case.
Analysis and Design of Algorithms
Big O Notation
Analysis and Design of Algorithms
1) O(1)
 Time complexity of a function (or set of statements) is considered as
O(1) if it doesn’t contain loop, recursion and call to any other non-
constant time function. For example swap() function has O(1) time
complexity.
Analysis and Design of Algorithms
 A loop or recursion that runs a constant number of times is also
considered as O(1). For example the following loop is O(1).
Analysis and Design of Algorithms
2) O(n)
 Time Complexity of a loop is considered as O(n) if the loop
variables is incremented / decremented by a constant amount. For
example the following loop statements have O(n) time complexity.
Analysis and Design of Algorithms
2) O(n)
Analysis and Design of Algorithms
2) O(n)
 Another Example:
Analysis and Design of Algorithms
3) O(nc)
 Time complexity of nested loops is equal to the number of times the
innermost statement is executed. For example the following loop
statements have O(n2) time complexity
Analysis and Design of Algorithms
3) O(n2)
Analysis and Design of Algorithms
 Another Example
Analysis and Design of Algorithms
4) O(Logn)
 Time Complexity of a loop is considered as O(Logn) if the loop
variables is divided / multiplied by a constant amount.
Analysis and Design of Algorithms
4) O(Logn)
 Another Example
Analysis and Design of Algorithms
5) O(LogLogn)
 Time Complexity of a loop is considered as O(LogLogn) if the loop
variables is reduced / increased exponentially by a constant.
Analysis and Design of Algorithms
5) O(LogLogn)
 Another Example
Analysis and Design of Algorithms
 How to combine time complexities of consecutive loops?
 Time complexity of above code is O(n) + O(m) which is O(n+m)
Analysis and Design of Algorithms
n O(1) O(log(n)) O(n) O(nlog(n)) O(N2) O(2n) O(n!)
1 1 0 1 1 1 2 1
8 1 3 8 24 64 256 40xx103
30 1 5 30 150 900 10x109 210x1032
500 1 9 500 4500 25x104 3x10150 1x101134
1000 1 10 1000 10x103 1x106 1x10301 4x102567
16x103 1 14 16x103 224x103 256x106 - -
1x105 1 17 1x105 17x105 10x109 - -
Analysis and Design of Algorithms
Analysis and Design of Algorithms
Length of Input (N) Worst Accepted Algorithm
≤10 O(N!),O(N6)
≤15 O(2N∗N2)
≤20 O(2N∗N)
≤100 O(N4)
≤400 O(N3)
≤2K O(N2∗logN)
≤10K O(N2)
≤1M O(N∗logN)
≤100M O(N),O(logN),O(1)
Analysis and Design of Algorithms
 Find the complexity
of the below
program:
Analysis and Design of Algorithms
 Solution: Time
Complexity O(n).
Even though the
inner loop is
bounded by n, but
due to break
statement it is
executing only once.
Analysis and Design of Algorithms
 Find the complexity of the below program:
Analysis and Design of Algorithms
 Solution:
Time
O(n2logn)
Analysis and Design of Algorithms
 Find the complexity of the below program:
Analysis and Design of Algorithms
 Solution:
Time
O(n log2n)
Analysis and Design of Algorithms
 Find the
complexity
of the
below
program:
Analysis and Design of Algorithms
 Solution:
Time O(n5)
Analysis and Design of Algorithms
facebook.com/mloey
mohamedloey@gmail.com
twitter.com/mloey
linkedin.com/in/mloey
mloey@fci.bu.edu.eg
mloey.github.io
Analysis and Design of Algorithms
www.YourCompany.com
© 2020 Companyname PowerPoint Business Theme. All Rights Reserved.
THANKS FOR
YOUR TIME

More Related Content

PDF
Algorithms Lecture 1: Introduction to Algorithms
PPT
Algorithm analysis
PDF
Algorithms Lecture 4: Sorting Algorithms I
PDF
Algorithms Lecture 5: Sorting Algorithms II
PDF
Algorithms Lecture 3: Analysis of Algorithms II
PDF
Algorithms Lecture 6: Searching Algorithms
PPTX
Advertising & Sales Management
PDF
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Algorithm analysis
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 6: Searching Algorithms
Advertising & Sales Management
Algorithms Lecture 7: Graph Algorithms

What's hot (20)

PPTX
daa-unit-3-greedy method
PPTX
Asymptotic Notations
PPTX
heap Sort Algorithm
PPTX
Parsing LL(1), SLR, LR(1)
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Strassen's matrix multiplication
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PDF
Time and Space Complexity
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPT
Asymptotic notations
PPTX
Algorithm analysis (All in one)
PPT
Asymptotic notation
PPT
Randomized algorithms ver 1.0
PPTX
Greedy algorithms
PPTX
Merge sort and quick sort
DOC
Unit 2 in daa
PPTX
Analysis of algorithm
PPT
Dinive conquer algorithm
PPTX
Priority Queue in Data Structure
PDF
Binary Search - Design & Analysis of Algorithms
daa-unit-3-greedy method
Asymptotic Notations
heap Sort Algorithm
Parsing LL(1), SLR, LR(1)
Greedy Algorithm - Knapsack Problem
Strassen's matrix multiplication
Fundamentals of the Analysis of Algorithm Efficiency
Time and Space Complexity
Unit 1 chapter 1 Design and Analysis of Algorithms
Asymptotic notations
Algorithm analysis (All in one)
Asymptotic notation
Randomized algorithms ver 1.0
Greedy algorithms
Merge sort and quick sort
Unit 2 in daa
Analysis of algorithm
Dinive conquer algorithm
Priority Queue in Data Structure
Binary Search - Design & Analysis of Algorithms
Ad

Viewers also liked (6)

PDF
Convolutional Neural Network Models - Deep Learning
PDF
Deep Learning - Overview of my work II
PDF
Computer Security Lecture 5: Simplified Advanced Encryption Standard
PDF
PMP Lecture 1: Introduction to Project Management
PDF
Computer Security Lecture 7: RSA
PPSX
C++ Programming Language
Convolutional Neural Network Models - Deep Learning
Deep Learning - Overview of my work II
Computer Security Lecture 5: Simplified Advanced Encryption Standard
PMP Lecture 1: Introduction to Project Management
Computer Security Lecture 7: RSA
C++ Programming Language
Ad

Similar to Algorithms Lecture 2: Analysis of Algorithms I (20)

PDF
Time Complexity of Algorithm (Analysis).pdf
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
PPTX
Analysis of algorithms
PPTX
complexity big oh notation notation.pptx
PPT
Lec7.ppt
PPT
analysis of algorithms
PPT
Lec7.ppt
PDF
Asymptotic notations
PPTX
Measuring algorithm performance
PPTX
Data Structure Algorithm -Algorithm Complexity
PDF
Data Structure - Lecture 1 - Introduction.pdf
PPT
18103010 algorithm complexity (iterative)
PPTX
Algorithm for the DAA agscsnak javausmagagah
PPT
Analysis.ppt
PPTX
complex computer systems and its applications.pptx
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PPT
Analysis of algo
PPT
Algorithm in Computer, Sorting and Notations
Time Complexity of Algorithm (Analysis).pdf
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
Analysis of algorithms
complexity big oh notation notation.pptx
Lec7.ppt
analysis of algorithms
Lec7.ppt
Asymptotic notations
Measuring algorithm performance
Data Structure Algorithm -Algorithm Complexity
Data Structure - Lecture 1 - Introduction.pdf
18103010 algorithm complexity (iterative)
Algorithm for the DAA agscsnak javausmagagah
Analysis.ppt
complex computer systems and its applications.pptx
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Analysis of algo
Algorithm in Computer, Sorting and Notations

More from Mohamed Loey (19)

PDF
Lecture 6: Deep Learning Applications
PDF
Lecture 5: Convolutional Neural Network Models
PDF
Lecture 4: Deep Learning Frameworks
PDF
Lecture 4: How it Works: Convolutional Neural Networks
PPTX
Lecture 3: Convolutional Neural Networks
PDF
Lecture 2: Artificial Neural Network
PDF
Lecture 1: Deep Learning for Computer Vision
PDF
Design of an Intelligent System for Improving Classification of Cancer Diseases
PDF
Computer Security - CCNA Security - Lecture 2
PDF
Computer Security - CCNA Security - Lecture 1
PDF
Algorithms Lecture 8: Pattern Algorithms
PDF
Computer Security Lecture 4.1: DES Supplementary Material
PDF
PMP Lecture 4: Project Integration Management
PDF
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
PDF
Computer Security Lecture 3: Classical Encryption Techniques 2
PDF
Computer Security Lecture 2: Classical Encryption Techniques 1
PDF
Computer Security Lecture 1: Overview
PDF
PMP Lecture 3: Project Management Processes
PDF
PMP Lecture 2: Project Management Framework
Lecture 6: Deep Learning Applications
Lecture 5: Convolutional Neural Network Models
Lecture 4: Deep Learning Frameworks
Lecture 4: How it Works: Convolutional Neural Networks
Lecture 3: Convolutional Neural Networks
Lecture 2: Artificial Neural Network
Lecture 1: Deep Learning for Computer Vision
Design of an Intelligent System for Improving Classification of Cancer Diseases
Computer Security - CCNA Security - Lecture 2
Computer Security - CCNA Security - Lecture 1
Algorithms Lecture 8: Pattern Algorithms
Computer Security Lecture 4.1: DES Supplementary Material
PMP Lecture 4: Project Integration Management
Computer Security Lecture 4: Block Ciphers and the Data Encryption Standard
Computer Security Lecture 3: Classical Encryption Techniques 2
Computer Security Lecture 2: Classical Encryption Techniques 1
Computer Security Lecture 1: Overview
PMP Lecture 3: Project Management Processes
PMP Lecture 2: Project Management Framework

Recently uploaded (20)

PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
semiconductor packaging in vlsi design fab
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
Education and Perspectives of Education.pptx
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
Complications of Minimal Access-Surgery.pdf
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Hazard Identification & Risk Assessment .pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
semiconductor packaging in vlsi design fab
Environmental Education MCQ BD2EE - Share Source.pdf
What if we spent less time fighting change, and more time building what’s rig...
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Education and Perspectives of Education.pptx
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Complications of Minimal Access-Surgery.pdf
Race Reva University – Shaping Future Leaders in Artificial Intelligence
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
FORM 1 BIOLOGY MIND MAPS and their schemes
A powerpoint presentation on the Revised K-10 Science Shaping Paper
AI-driven educational solutions for real-life interventions in the Philippine...
Hazard Identification & Risk Assessment .pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf

Algorithms Lecture 2: Analysis of Algorithms I

  • 1. Analysis and Design of Algorithms Analysis of Algorithms I
  • 2. Analysis and Design of Algorithms Analysis of Algorithms Time complexity Asymptotic Notations Big O Notation Growth Orders Problems
  • 3. Analysis and Design of Algorithms Analysis of Algorithms is the determination of the amount of time, storage and/or other resources necessary to execute them. Analyzing algorithms is called Asymptotic Analysis Asymptotic Analysis evaluate the performance of an algorithm
  • 4. Analysis and Design of Algorithms Time complexity
  • 5. Analysis and Design of Algorithms  time complexity of an algorithm quantifies the amount of time taken by an algorithm  We can have three cases to analyze an algorithm: 1) Worst Case 2) Average Case 3) Best Case
  • 6. Analysis and Design of Algorithms  Assume the below algorithm using Python code:
  • 7. Analysis and Design of Algorithms  Worst Case Analysis: In the worst case analysis, we calculate upper bound on running time of an algorithm.
  • 8. Analysis and Design of Algorithms  Worst Case Analysis: the case that causes maximum number of operations to be executed.  For Linear Search, the worst case happens when the element to be searched is not present in the array. (example : search for number 8) 2 3 5 4 1 7 6
  • 9. Analysis and Design of Algorithms  Worst Case Analysis: When x is not present, the search() functions compares it with all the elements of arr one by one.
  • 10. Analysis and Design of Algorithms  The worst case time complexity of linear search would be O(n).
  • 11. Analysis and Design of Algorithms  Average Case Analysis: we take all possible inputs and calculate computing time for all of the inputs.
  • 12. Analysis and Design of Algorithms  Best Case Analysis: calculate lower bound on running time of an algorithm.
  • 13. Analysis and Design of Algorithms  The best case time complexity of linear search would be O(1).
  • 14. Analysis and Design of Algorithms  Best Case Analysis: the case that causes minimum number of operations to be executed.  For Linear Search, the best case occurs when x is present at the first location. (example : search for number 2)  So time complexity in the best case would be Θ(1) 2 3 5 4 1 7 6
  • 15. Analysis and Design of Algorithms  Most of the times, we do worst case analysis to analyze algorithms.  The average case analysis is not easy to do in most of the practical cases and it is rarely done.  The Best case analysis is bogus. Guaranteeing a lower bound on an algorithm doesn’t provide any information.
  • 16. Analysis and Design of Algorithms 1) Big O Notation: is an Asymptotic Notation for the worst case. 2) Ω Notation (omega notation): is an Asymptotic Notation for the best case. 3) Θ Notation (theta notation) : is an Asymptotic Notation for the worst case and the best case.
  • 17. Analysis and Design of Algorithms Big O Notation
  • 18. Analysis and Design of Algorithms 1) O(1)  Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non- constant time function. For example swap() function has O(1) time complexity.
  • 19. Analysis and Design of Algorithms  A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1).
  • 20. Analysis and Design of Algorithms 2) O(n)  Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example the following loop statements have O(n) time complexity.
  • 21. Analysis and Design of Algorithms 2) O(n)
  • 22. Analysis and Design of Algorithms 2) O(n)  Another Example:
  • 23. Analysis and Design of Algorithms 3) O(nc)  Time complexity of nested loops is equal to the number of times the innermost statement is executed. For example the following loop statements have O(n2) time complexity
  • 24. Analysis and Design of Algorithms 3) O(n2)
  • 25. Analysis and Design of Algorithms  Another Example
  • 26. Analysis and Design of Algorithms 4) O(Logn)  Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount.
  • 27. Analysis and Design of Algorithms 4) O(Logn)  Another Example
  • 28. Analysis and Design of Algorithms 5) O(LogLogn)  Time Complexity of a loop is considered as O(LogLogn) if the loop variables is reduced / increased exponentially by a constant.
  • 29. Analysis and Design of Algorithms 5) O(LogLogn)  Another Example
  • 30. Analysis and Design of Algorithms  How to combine time complexities of consecutive loops?  Time complexity of above code is O(n) + O(m) which is O(n+m)
  • 31. Analysis and Design of Algorithms n O(1) O(log(n)) O(n) O(nlog(n)) O(N2) O(2n) O(n!) 1 1 0 1 1 1 2 1 8 1 3 8 24 64 256 40xx103 30 1 5 30 150 900 10x109 210x1032 500 1 9 500 4500 25x104 3x10150 1x101134 1000 1 10 1000 10x103 1x106 1x10301 4x102567 16x103 1 14 16x103 224x103 256x106 - - 1x105 1 17 1x105 17x105 10x109 - -
  • 32. Analysis and Design of Algorithms
  • 33. Analysis and Design of Algorithms Length of Input (N) Worst Accepted Algorithm ≤10 O(N!),O(N6) ≤15 O(2N∗N2) ≤20 O(2N∗N) ≤100 O(N4) ≤400 O(N3) ≤2K O(N2∗logN) ≤10K O(N2) ≤1M O(N∗logN) ≤100M O(N),O(logN),O(1)
  • 34. Analysis and Design of Algorithms  Find the complexity of the below program:
  • 35. Analysis and Design of Algorithms  Solution: Time Complexity O(n). Even though the inner loop is bounded by n, but due to break statement it is executing only once.
  • 36. Analysis and Design of Algorithms  Find the complexity of the below program:
  • 37. Analysis and Design of Algorithms  Solution: Time O(n2logn)
  • 38. Analysis and Design of Algorithms  Find the complexity of the below program:
  • 39. Analysis and Design of Algorithms  Solution: Time O(n log2n)
  • 40. Analysis and Design of Algorithms  Find the complexity of the below program:
  • 41. Analysis and Design of Algorithms  Solution: Time O(n5)
  • 42. Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io
  • 43. Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME