SlideShare a Scribd company logo
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
1
S. Hassan Adelyar, Ph.D
Instructor of Computer Science Faculty
Kabul University
December 2020
Lesson III: Algorithm-Analysis Part II
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
2
Learning Outcome
 By the end of this presentation, students will be
able to:
 Count the number of steps for simple operations
 Count the number of steps for loops &
conditional statements.
 Count the number of steps for simple
Algorithms.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
3
Simple Operation
 Two classes of operations:
 Comparison
 Arithmetic.
 Comparison operations
 Include equal, not equal, less than, greater
than, less than or equal, and greater than or
equal.
 The comparison operators are counted as a
single step.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
4
 Arithmetic operators are counted in three
groups:
 Additive
 Multiplicative
 Logarithms and geometric functions
 Additive operators include:
 addition, subtraction, increment, and
decrement.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
5
 Multiplicative operators include:
 multiplication, division, and modulus.
 Additive & multiplicative are counted separately
because multiplications are considered to take
longer than additions.
 Reduce the number of multiplications.
 Logarithms and geometric functions:
 Consume more time than multiplications.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
6
 A special case is integer multiplication or division
by a power of 2.
 This operation can be reduced to a shift operation,
which is considered as fast as an addition.
 Multiplication or division by 2 is commonly found
in divide and conquer algorithms.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
7
 We define a set of primitive operations such as the
following:
 Assigning a value to a variable.
 Calling a method.
 Performing an arithmetic operation (for
example, adding two numbers).
 Comparing two numbers.
 Indexing into an array.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
8
 Instead of trying to determine the specific
execution time of each primitive operation, we will
simply count how many primitive operations are
executed, and use this number as a measure of the
running-time of the algorithm.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
9
 String operation
 Linear, but indexing and length are constant.
 For loops
 The total running time of a statement inside a
nested loops is the running time of the statement
multiplied by the product of the loops.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
10
 If/else
 For the fragment if (condition) S1 else S2, the
running time of an if/else statement is never more
than the running time of the test plus the larger of
the running times of S1 and S2.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
11
Examples
 Examples
 1) x = 0;
 2) for(i=0; i<n; i++)
 3) x++;
 The number of steps can be calculate as follow:
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
12
 Line Time Number
 1 c1 1
 2 c2 n+1
 3 c3 n
 Therefore, T(n) is:
 T(n) = c1+c2(n+1)+c3n
 Now considering c as the larger of c1, c2 & c3:
 T(n)= c(2n+2)
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
13
 Example 2:
 1) x=0;
 2) for(i = 0; i<n; i++)
 3) for(j=0; j< n; j++)
 4) x++;
 The number of steps can be calculate as follow:
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
14
 Line Time Number
 1 c1 1
 2 c2 n+1
 3 c3 n(n+1)
 4 c4 n x n
 Therefore, T(n) is:
 T(n) = c1 + c2(n+1) + c3n(n+1)+c4n2
 T(n) = C(2n2
+2n +2)
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
15
 Example 3:
 1) int Factorial(int n) {
 2) int fact = 1;
 3) for(int i=1; i<n; i++)
 4) fact *=i;
 5) return fact;
 }
 The number of steps can be calculate as follow:
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
16
 Line Time Number
 2 c1 1
 3 c2 n
 4 c3 n-1
 5 c4 1
 T(n) = c1+c2n+c3(n-1)+c4
 T(n) = C(2n+1)
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
17
Summary
 In this presentation we study Algorithm analysis by
counting the number of steps for simple operations.
 In addition to counting the number of steps for
simple operation we studied how to count the
number of steps for loops & conditional statements.
 Furthermore, we studied the ways for counting the
number of steps for simple Algorithms.
p
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
18
:‫نمائید‬ ‫دریافت‬ ‫را‬ ‫ذیل‬ ‫کود‬ ‫زمانی‬ ‫تابع‬ :‫تمرین‬
p
for (i=0; i<10; i++){
a = a + i;
b = b * i;
}
Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
19
Exercises
1) Count the number of steps for the following code (5
score):
int a=0;
for(i = 0; i<n; i++)
for(j=0; j< n; j++)
a++;
2) Why we measure the time of Algorithm by counting the
number of steps instead of measuring the time by
seconds?
p
End of Lesson 3
Questions?

More Related Content

PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PPTX
Lesson 1 - Introduction to Algorithms.pptx
PDF
Performance Analysis,Time complexity, Asymptotic Notations
PPT
Data Structures and Algorithm Analysis
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PPT
data unit notes from department of computer science
Presentation_23953_Content_Document_20240906040454PM.pptx
Lesson 1 - Introduction to Algorithms.pptx
Performance Analysis,Time complexity, Asymptotic Notations
Data Structures and Algorithm Analysis
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
data unit notes from department of computer science

Similar to Lesson no 3 - Algorithm Analysis - II.pptx (20)

PPTX
Data structures algorithms basics
PPTX
Lesson num 2 - Algorithm Analysis - I.pptx
PPTX
Analysis Framework, Asymptotic Notations
PPT
UnitI (1).ppt
PPT
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
PPT
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
PDF
Design and Analysis of Algorthim(CSE) Study Material
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Algorithms.pdf
PPT
Analysis of algo
PPT
Daa presentation 97
PDF
Design & Analysis Of Algorithm
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
PDF
PPTX
Understanding Basic C++ Program for Arithmetic Operations.pptx
PPTX
algorithmanalysis and effciency.pptx
PPTX
Lesson 4 - Algorithm Analysis - III.pptx
Data structures algorithms basics
Lesson num 2 - Algorithm Analysis - I.pptx
Analysis Framework, Asymptotic Notations
UnitI (1).ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
Design and Analysis of Algorthim(CSE) Study Material
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Algorithms.pdf
Analysis of algo
Daa presentation 97
Design & Analysis Of Algorithm
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
Fundamentals of the Analysis of Algorithm Efficiency
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Understanding Basic C++ Program for Arithmetic Operations.pptx
algorithmanalysis and effciency.pptx
Lesson 4 - Algorithm Analysis - III.pptx
Ad

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Pre independence Education in Inndia.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
master seminar digital applications in india
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
human mycosis Human fungal infections are called human mycosis..pptx
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial diseases, their pathogenesis and prophylaxis
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
Module 4: Burden of Disease Tutorial Slides S2 2025
Pre independence Education in Inndia.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
master seminar digital applications in india
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
Ad

Lesson no 3 - Algorithm Analysis - II.pptx

  • 1. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 1 S. Hassan Adelyar, Ph.D Instructor of Computer Science Faculty Kabul University December 2020 Lesson III: Algorithm-Analysis Part II
  • 2. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 2 Learning Outcome  By the end of this presentation, students will be able to:  Count the number of steps for simple operations  Count the number of steps for loops & conditional statements.  Count the number of steps for simple Algorithms. p
  • 3. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 3 Simple Operation  Two classes of operations:  Comparison  Arithmetic.  Comparison operations  Include equal, not equal, less than, greater than, less than or equal, and greater than or equal.  The comparison operators are counted as a single step. p
  • 4. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 4  Arithmetic operators are counted in three groups:  Additive  Multiplicative  Logarithms and geometric functions  Additive operators include:  addition, subtraction, increment, and decrement. p
  • 5. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 5  Multiplicative operators include:  multiplication, division, and modulus.  Additive & multiplicative are counted separately because multiplications are considered to take longer than additions.  Reduce the number of multiplications.  Logarithms and geometric functions:  Consume more time than multiplications. p
  • 6. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 6  A special case is integer multiplication or division by a power of 2.  This operation can be reduced to a shift operation, which is considered as fast as an addition.  Multiplication or division by 2 is commonly found in divide and conquer algorithms. p
  • 7. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 7  We define a set of primitive operations such as the following:  Assigning a value to a variable.  Calling a method.  Performing an arithmetic operation (for example, adding two numbers).  Comparing two numbers.  Indexing into an array. p
  • 8. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 8  Instead of trying to determine the specific execution time of each primitive operation, we will simply count how many primitive operations are executed, and use this number as a measure of the running-time of the algorithm. p
  • 9. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 9  String operation  Linear, but indexing and length are constant.  For loops  The total running time of a statement inside a nested loops is the running time of the statement multiplied by the product of the loops. p
  • 10. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 10  If/else  For the fragment if (condition) S1 else S2, the running time of an if/else statement is never more than the running time of the test plus the larger of the running times of S1 and S2. p
  • 11. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 11 Examples  Examples  1) x = 0;  2) for(i=0; i<n; i++)  3) x++;  The number of steps can be calculate as follow: p
  • 12. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 12  Line Time Number  1 c1 1  2 c2 n+1  3 c3 n  Therefore, T(n) is:  T(n) = c1+c2(n+1)+c3n  Now considering c as the larger of c1, c2 & c3:  T(n)= c(2n+2) p
  • 13. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 13  Example 2:  1) x=0;  2) for(i = 0; i<n; i++)  3) for(j=0; j< n; j++)  4) x++;  The number of steps can be calculate as follow: p
  • 14. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 14  Line Time Number  1 c1 1  2 c2 n+1  3 c3 n(n+1)  4 c4 n x n  Therefore, T(n) is:  T(n) = c1 + c2(n+1) + c3n(n+1)+c4n2  T(n) = C(2n2 +2n +2) p
  • 15. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 15  Example 3:  1) int Factorial(int n) {  2) int fact = 1;  3) for(int i=1; i<n; i++)  4) fact *=i;  5) return fact;  }  The number of steps can be calculate as follow: p
  • 16. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 16  Line Time Number  2 c1 1  3 c2 n  4 c3 n-1  5 c4 1  T(n) = c1+c2n+c3(n-1)+c4  T(n) = C(2n+1) p
  • 17. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 17 Summary  In this presentation we study Algorithm analysis by counting the number of steps for simple operations.  In addition to counting the number of steps for simple operation we studied how to count the number of steps for loops & conditional statements.  Furthermore, we studied the ways for counting the number of steps for simple Algorithms. p
  • 18. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 18 :‫نمائید‬ ‫دریافت‬ ‫را‬ ‫ذیل‬ ‫کود‬ ‫زمانی‬ ‫تابع‬ :‫تمرین‬ p for (i=0; i<10; i++){ a = a + i; b = b * i; }
  • 19. Algorithm Analysis – Part II Algorithm Analysis & Design April 2020 19 Exercises 1) Count the number of steps for the following code (5 score): int a=0; for(i = 0; i<n; i++) for(j=0; j< n; j++) a++; 2) Why we measure the time of Algorithm by counting the number of steps instead of measuring the time by seconds? p
  • 20. End of Lesson 3 Questions?