SlideShare ist ein Scribd-Unternehmen logo
1 von 19
LEC-2 ALGORITHMS EFFICIENCY &
COMPLEXITY
WHAT IS AN ALGORITHM?
• An algorithm is a set of instructions designed to perform a
specific task
• A step-by-step problem-solving procedure, especially an
established, recursive computational procedure for solving a
problem in a finite number of steps.
• An algorithm is any well-defined computational procedure
that takes some value, or set of values, as input and produces
some value, or set of values, as output.
• An algorithm is thus a sequence of computational steps that
transform the input into the output.
HOW TO WRITE AN ALGORITHMS
• Index started with 1
• No variable declaration
• No use of semicolon
• Assignment statement a3
• Comparison if a=3
• Reputational structures while a1 to N or for a1 to N
• When a statement is continued from one line to another
within structure, indent the continued line(s).
EFFICIENCY(ALGORITHMIC COMPLEXITY)?
• Properties of algorithms
 Correctness
 Deterministic
 Efficiency
• Algorithmic Complexity: how many steps our algorithm will
take on any given input instance by simply executing it on the
given input.
• Algorithmic complexity is concerned about how fast or slow
particular algorithm performs.
Efficiency of an algorithm can be measured in terms of:
− Execution time (time complexity)
− The amount of memory required (space complexity)
EFFICIENCY
Which measure is more important?
time complexity comparisons are more interesting than
space complexity comparisons
Time complexity: A measure of the amount of time
required to execute an algorithm
Factors that should not affect time complexity analysis:
• The programming language chosen to implement the algorithm
• The quality of the compiler
• The speed of the computer on which the algorithm is to be
executed
(TIME) EFFICIENCY OF AN ALGORITHM
worst case efficiency
is the maximum number of steps that an algorithm can take for any
collection of data values.
Best case efficiency
is the minimum number of steps that an algorithm can take any
collection of data values.
Average case efficiency
the efficiency averaged on all possible inputs
Example:
Consider: Search for an element in a list
• Best case search when item at beginning
• Worst case when item at end
• Average case somewhere between
If the input has size n, efficiency will be a function of n
MEASURING EFFICIENCY
Simplified analysis can be based on:
• Number of arithmetic operations performed
• Number of comparisons made
• Number of times through a critical loop
• Number of array elements accessed
• etc
Three algorithms for computing the sum 1 + 2 + . . . + n for an integer n > 0
MEASURING EFFICIENCY
Java code for algorithms
MEASURING EFFICIENCY
The number of basic operations required by the algorithms
ANALYSIS OF SUM (2)
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A
int Sum(int A[], int N)
{
int s=0;
for (int i=0; i< N; i++ )
{
s = s + A[i];
}
return s;
}
1
2
3
4
5
6
7
8
1,2,8: Once time
3,4,5,6,7: Once per each iteration
of for loop, N iteration
Total: 5N + 3
The complexity function of the
algorithm is : f(N) = 5N +3
A Simple Example
Asymptotic Notation
− The notations we use to describe the asymptotic running time of an
algorithm are defined in terms of functions whose domains are the set
of natural numbers N = {0, 1, 2, …}. Such notations are convenient
for describing the worst-case running-time function T (n), which
usually is defined only on integer input sizes.
− We will use asymptotic notation primarily to describe the running
times of algorithms
order of growth
− Running time of an algorithm increases with the size of the input in the
limit as the size of the input increases without bound
Growth of function
− Means if we increase the number of inputs(values of n) the function
growth speedily.
Big “O” Notation
Definition: function f(n) is O(g(n)) if there exist
constants c and n0 such that for all n>=n0: f(n) <= c
(g(n)).
-The notation is often confusing: f = O(g) is read "f is
big-oh of g.“
-Generally, when we see a statement of the form
f(n)=O(g(n)):
-f(n) is the formula that tells us exactly how many
operations the function/algorithm in question will
perform when the problem size is n.
-g(n) is like an upper bound for f(n). Within a constant
factor, the number of operations required by your
function is no worse than g(n).
Big “O” Notation
Why is this useful?
–We want out algorithms to scalable. Often, we write
program and test them on relatively small inputs. Yet, we
expect a user to run our program with larger inputs.
Running-time analysis helps us predict how efficient our
program will be in the `real world'.
Big “O” Notation
Big “O” Notation
Big “O” Notation
Big “O” Notation
Big “Ω” Notation
Definition: function f(n) is Ω (g(n)) if there exist constants c
and n0 such that for all n>=n0: f(n) >= c (g(n)).
-The notation is often confusing: f = Ω (g) is read "f is big-omega
of g.“
-Generally, when we see a statement of the form f(n)= Ω (g(n)):
Big “𝜃” Notation

Weitere ähnliche Inhalte

Was ist angesagt?

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Hassan Ahmed
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
Dhaval Kaneria
 

Was ist angesagt? (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Hashing
HashingHashing
Hashing
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
chapter 1
chapter 1chapter 1
chapter 1
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Data structure
Data structureData structure
Data structure
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Ähnlich wie Lec 2 algorithms efficiency complexity

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
 
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 - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
babuk110
 

Ähnlich wie Lec 2 algorithms efficiency complexity (20)

Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
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
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
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 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
Algorithms
Algorithms Algorithms
Algorithms
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 

Mehr von Anaya Zafar

Mehr von Anaya Zafar (20)

data structures and its importance
 data structures and its importance  data structures and its importance
data structures and its importance
 
heap sort
 heap sort heap sort
heap sort
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Link list part 1
Link list part 1Link list part 1
Link list part 1
 
Dijkstra's algorithm
Dijkstra's algorithm Dijkstra's algorithm
Dijkstra's algorithm
 
The miracle-morning
The miracle-morningThe miracle-morning
The miracle-morning
 
How to write a meeting agenda?
How to write a  meeting agenda?How to write a  meeting agenda?
How to write a meeting agenda?
 
How we achieve our goals
How we achieve our goalsHow we achieve our goals
How we achieve our goals
 
Fiber optics
Fiber opticsFiber optics
Fiber optics
 
3 d technology
3 d technology3 d technology
3 d technology
 
Ch 22 question solution of fundamental of physics 8th edition by HRW
Ch 22  question solution of fundamental of physics 8th edition by HRWCh 22  question solution of fundamental of physics 8th edition by HRW
Ch 22 question solution of fundamental of physics 8th edition by HRW
 
Ch 21 question solution of fundamental of physics 8th edition by HRW
Ch 21 question solution of fundamental of physics 8th edition by HRWCh 21 question solution of fundamental of physics 8th edition by HRW
Ch 21 question solution of fundamental of physics 8th edition by HRW
 
Definition of capacitance
Definition of capacitanceDefinition of capacitance
Definition of capacitance
 
Chapter 24-capacitance
Chapter 24-capacitanceChapter 24-capacitance
Chapter 24-capacitance
 
Capacitance and dielectrics
Capacitance and dielectrics Capacitance and dielectrics
Capacitance and dielectrics
 
20 electric current resistance ohms law
20 electric current resistance ohms law20 electric current resistance ohms law
20 electric current resistance ohms law
 
Voltage, current, resistance, and ohm's law
Voltage, current, resistance, and ohm's lawVoltage, current, resistance, and ohm's law
Voltage, current, resistance, and ohm's law
 
Role of women in development
Role of women in developmentRole of women in development
Role of women in development
 
Application of Gauss's law
Application of  Gauss's lawApplication of  Gauss's law
Application of Gauss's law
 
Why we need Gaussian surface in Gauss's law
Why we need Gaussian surface in Gauss's lawWhy we need Gaussian surface in Gauss's law
Why we need Gaussian surface in Gauss's law
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Kürzlich hochgeladen (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
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
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Lec 2 algorithms efficiency complexity

  • 2. WHAT IS AN ALGORITHM? • An algorithm is a set of instructions designed to perform a specific task • A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps. • An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. • An algorithm is thus a sequence of computational steps that transform the input into the output.
  • 3. HOW TO WRITE AN ALGORITHMS • Index started with 1 • No variable declaration • No use of semicolon • Assignment statement a3 • Comparison if a=3 • Reputational structures while a1 to N or for a1 to N • When a statement is continued from one line to another within structure, indent the continued line(s).
  • 4. EFFICIENCY(ALGORITHMIC COMPLEXITY)? • Properties of algorithms  Correctness  Deterministic  Efficiency • Algorithmic Complexity: how many steps our algorithm will take on any given input instance by simply executing it on the given input. • Algorithmic complexity is concerned about how fast or slow particular algorithm performs. Efficiency of an algorithm can be measured in terms of: − Execution time (time complexity) − The amount of memory required (space complexity)
  • 5. EFFICIENCY Which measure is more important? time complexity comparisons are more interesting than space complexity comparisons Time complexity: A measure of the amount of time required to execute an algorithm Factors that should not affect time complexity analysis: • The programming language chosen to implement the algorithm • The quality of the compiler • The speed of the computer on which the algorithm is to be executed
  • 6. (TIME) EFFICIENCY OF AN ALGORITHM worst case efficiency is the maximum number of steps that an algorithm can take for any collection of data values. Best case efficiency is the minimum number of steps that an algorithm can take any collection of data values. Average case efficiency the efficiency averaged on all possible inputs Example: Consider: Search for an element in a list • Best case search when item at beginning • Worst case when item at end • Average case somewhere between If the input has size n, efficiency will be a function of n
  • 7. MEASURING EFFICIENCY Simplified analysis can be based on: • Number of arithmetic operations performed • Number of comparisons made • Number of times through a critical loop • Number of array elements accessed • etc Three algorithms for computing the sum 1 + 2 + . . . + n for an integer n > 0
  • 9. MEASURING EFFICIENCY The number of basic operations required by the algorithms
  • 10. ANALYSIS OF SUM (2) // Input: int A[N], array of N integers // Output: Sum of all numbers in array A int Sum(int A[], int N) { int s=0; for (int i=0; i< N; i++ ) { s = s + A[i]; } return s; } 1 2 3 4 5 6 7 8 1,2,8: Once time 3,4,5,6,7: Once per each iteration of for loop, N iteration Total: 5N + 3 The complexity function of the algorithm is : f(N) = 5N +3 A Simple Example
  • 11. Asymptotic Notation − The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains are the set of natural numbers N = {0, 1, 2, …}. Such notations are convenient for describing the worst-case running-time function T (n), which usually is defined only on integer input sizes. − We will use asymptotic notation primarily to describe the running times of algorithms order of growth − Running time of an algorithm increases with the size of the input in the limit as the size of the input increases without bound Growth of function − Means if we increase the number of inputs(values of n) the function growth speedily.
  • 12. Big “O” Notation Definition: function f(n) is O(g(n)) if there exist constants c and n0 such that for all n>=n0: f(n) <= c (g(n)). -The notation is often confusing: f = O(g) is read "f is big-oh of g.“ -Generally, when we see a statement of the form f(n)=O(g(n)): -f(n) is the formula that tells us exactly how many operations the function/algorithm in question will perform when the problem size is n. -g(n) is like an upper bound for f(n). Within a constant factor, the number of operations required by your function is no worse than g(n).
  • 13. Big “O” Notation Why is this useful? –We want out algorithms to scalable. Often, we write program and test them on relatively small inputs. Yet, we expect a user to run our program with larger inputs. Running-time analysis helps us predict how efficient our program will be in the `real world'.
  • 18. Big “Ω” Notation Definition: function f(n) is Ω (g(n)) if there exist constants c and n0 such that for all n>=n0: f(n) >= c (g(n)). -The notation is often confusing: f = Ω (g) is read "f is big-omega of g.“ -Generally, when we see a statement of the form f(n)= Ω (g(n)):

Hinweis der Redaktion

  1. computational procedure ? 1 :A method of computing. 2 :The act or process of computing. 3 :The result of computing.