SlideShare a Scribd company logo
2
Most read
9
Most read
13
Most read
Case Study
Algorithm Analysis
Case Study
This case study is meant to demonstrate the salient features of algorithm design and
analysis, as discussed previously. A simple example is used to systematically describe the
following steps.:
Algorithm Analysis
• Problem statement
• Algorithm design
• Implementation using pseudo code
• Analysis of best, worst and average running times
• Space complexity
• Correctness of algorithm
• Visualization of Analysis
Case Study
¾ Design an algorithm to find maximum element in an array of size n
¾ Analyze the algorithm to determine :
• Time efficiency
• Space efficiency
• Correctness
Problem Statement
Case Study
The design features are expressed in plain language. The algorithm for the solution of
the problem consists of the following steps:
Step #1: Store the first array element in variable max
Step #2: Scan array to compare max with other elements
Step #3: Replace max with a larger element, when found during the scan
Step #4: Return value held by max
Algorithm Design
Algorithm Analysis
FIND-MAX ( A, n )
1 max ←A[1] ►Store first array element into variable max
2 for j←2 to n do ► Scan remaining elements
3 if ( A[j] > max ) ►Compare an element with max
4 then max ← A[j] ► Replace max with a larger element
5 return max ► Return maximum element
The procedure FIND-MAX returns maximum element in an array. The array A and its
size n, are passed as arguments. The following pseudo code describes the essential steps,
together with comments which are identified by the symbol ►:
Pseudo Code
Running Time
Costs of Basic Operations
Cost of returning maximum element
Cr
return max
5
Cost of accessing A[j]
Cost of storing A[j] into max
Ca
Cs
then max ← A[j]
4
Cost of accessing A[j]
Cost of comparing A[j] with max, and branching
Ca
Cc
if ( A[j] > max )
3
Cost of storing 2 into j
Cost of comparing index j with n, and branching
Cost of incrementing j
Cs
Cc
Ci
for j←2 to n do
2
Cost of accessing A[1]
Cost of storing A[1] into max
Ca
Cs
max ←A[1]
1
Remarks
Unit costs
Statement
#
First, we identify basic operations and their associated costs . The table below lists various
operations and costs. Here the term ‘cost’ refers to the time consumed in executing an
operation.
Table of costs of basic operations
Running Time
Counts of Basic Operations
1
k
k
n-1
n-1
1
n-1
n-1
1
1
Operations
Count
Cr
Cr
return max
5
(Ca + Cs). k, where k depends on
condition in statement 3 . In general, 0≤ k
≤ n -1
Ca
Cs
then max ← A[j]
4
(n-1).Ca + (n-1).Cc
Ca
Cc
if ( A[j] > max )
3
Cs + (n-1).Cc +(n-1).Ci
Cs
Cc
Ci
for j←2 to n do
2
Ca + Cs
Ca
Cs
max ←A[1]
1
Total Cost
Unit Cost
Statement
#
Next we count the number of the basic operations, and total cost of executing each
statement. The frequency of execution of statement 4 depends on the outcome of statement
3; it will be executed when the condition A[j]>max turns out to be true. This condition ,in
turn, depends on the order of data in the array A. For the purpose of analysis, we assume
that statement 4 is executed k times, where 0 ≤ k ≤ n-1.
Table of frequency and total cost of basic operations
Running Time
Aggregate Cost
• The running time T(n) is obtained by adding the costs in the last column of the table
Tn) = Ca + Cs
+ Cs + (n-1).Cc
+ (n-1).Ci+ (n-1).Ca
+ (n-1).Cc + (Ca + Cs). k
+ Cr
• Simplifying and rearranging, we get following expression .
T(n) =A + B.k + C.n,
where A = 2Cs – 2Cc – Ci + Cr,
B = Ca + Cs,
C = 2Cc + Ci + Ca
• The constants A, B, C depend on the computing environment
Running Time Classification
Best, Worst, Average Cases
The running time T(n) of the algorithm for finding the maximum element of array of size n is given by
T(n) = A + B.k + C.n, where 0 ≤ k ≤ n-1
Here k is the number of times the statement max ← A[j] will be executed. The following possibilities
can arise:
Best Case: Best case occurs when the statement is not executed at all. This happens when
the array maximum element occurs in the first cell. In this case k = 0, and best (minimum ) running time
is given by
Tbest (n)=A + C.n
Average Case: In this case, the statement is executed on an average n / 2 times so that k = n / 2. Thus,
average time running time is given by
Taverage(n) = A + (B / 2 + C).n
Worst Case: In this case, the statement is executed n-1 times; so k = n-1. This happens
when the array is sorted in ascending order .Thus, worst (maximum) running time is given by
Tworst(n) = A-B + (B + C).n
Running Time Classification
Plot of Best Worst and Average Cases
A plot of running times for different cases is shown below. The running time in all of
the three cases increases linearly. However, the slope of line (rate of increase) is different.
In the case of worst time, for example, the running time increases at a greater rate.
Algorithm Analysis
FIND-MAX(A)
1 max ← A[1]
2 for j←2 to n do
3 if( A[j] > max)
4 then max ← A[j]
1 return max
ƒ The correctness of FIND-MAX algorithm, listed below, is established by the
loop invariant method.
ƒ First, we define the loop invariant S as the following statement:
Variable max holds the largest value at all stages of loop execution
• Next, we consider the steps of initialization, maintenance, and termination
.
Correctness
Algorithm Analysis
Correctness
• The Initialization condition requires that prior to first iteration the statement S should be
true. This is trivially ( also called vacuously) true, because at this stage max contains the
single element A[1].
• The Maintenance condition requires that if S is true before an iteration of loop, it should
remain true after the iteration It can be easily verified that if max holds the largest of k
elements, after kth iteration, then it holds largest of k+1 elements after the next iteration.
ie.(k+1)st iteration
• The Termination condition requires that post-condition should be true for problem size
i.e, max should return maximum array element. The loop terminates when index j exceeds
n. This implies that just after the last iteration max holds the largest of the first n elements
of the array. Since array has size n, it means that max returns the largest of array elements.
Analysis of Algorithm
The space analysis of algorithm for finding maximum element is simple and
straightforward. It amounts to determining space utilization as function of data structure size.
• The total space requirement consists of memory used by the program statements
and array element. The former is a fixed and does not depend on array size.
• The amount of storage requirement for the array depends on the nature of data
type (integer, floating point, strings). It increases in direct proportion to the array size
• Thus, space efficiency is given by
S(n) = A + B.n
Space Efficiency
Array space
requirement
Program space
requirement
Visualization
Visualization

More Related Content

PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PPT
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
PPT
how to calclute time complexity of algortihm
PPTX
Analysis of algorithm
PPT
Introduction to Design Algorithm And Analysis.ppt
PPTX
Notion of an algorithm
PPTX
Algorithm Complexity and Main Concepts
PPT
Design and Analysis of Algorithms
Algorithm And analysis Lecture 03& 04-time complexity.
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
how to calclute time complexity of algortihm
Analysis of algorithm
Introduction to Design Algorithm And Analysis.ppt
Notion of an algorithm
Algorithm Complexity and Main Concepts
Design and Analysis of Algorithms

What's hot (20)

PDF
Algorithms Lecture 2: Analysis of Algorithms I
DOC
Ch 6 final
PPTX
ID3 ALGORITHM
PPTX
Stressen's matrix multiplication
PPT
finding Min and max element from given array using divide & conquer
PPTX
Strassen's matrix multiplication
PPTX
implementation of travelling salesman problem with complexity ppt
PDF
Little o and little omega
PPTX
Predicate logic
PPTX
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
PDF
Coin Change Problem
PPT
Randomized algorithms ver 1.0
PPTX
Asymptotic Notations
PDF
Target language in compiler design
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Mining single dimensional boolean association rules from transactional
PPT
decison tree and rules in data mining techniques
PPTX
Mathematical Analysis of Recursive Algorithm.
PDF
R Programming language model test paper
PPTX
Knowledge Representation, Inference and Reasoning
Algorithms Lecture 2: Analysis of Algorithms I
Ch 6 final
ID3 ALGORITHM
Stressen's matrix multiplication
finding Min and max element from given array using divide & conquer
Strassen's matrix multiplication
implementation of travelling salesman problem with complexity ppt
Little o and little omega
Predicate logic
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Coin Change Problem
Randomized algorithms ver 1.0
Asymptotic Notations
Target language in compiler design
Greedy Algorithm - Knapsack Problem
Mining single dimensional boolean association rules from transactional
decison tree and rules in data mining techniques
Mathematical Analysis of Recursive Algorithm.
R Programming language model test paper
Knowledge Representation, Inference and Reasoning
Ad

Similar to Case Study(Analysis of Algorithm.pdf (20)

PPTX
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
PDF
Analysis of algorithm. big-oh notation.omega notation theta notation.performa...
PPTX
AoA Lec Design of algorithm spresentation
PPT
introegthnhhdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhppt
PDF
Algorithms Analysis.pdf
PDF
Analysis Framework for Analysis of Algorithms.pdf
PPTX
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
PDF
ADA_2_Analysis of Algorithms
PPTX
Analysis of Algorithm full version 2024.pptx
PPTX
Design Analysis of Alogorithm 1 ppt 2024.pptx
PDF
01 CS316_Introduction.pdf5959695559655565
PDF
Asymptotic Analysis
PPT
Data structure lecture 2
PPTX
daa unit 1.pptx
PDF
Anlysis and design of algorithms part 1
PDF
Bt0080 fundamentals of algorithms1
PPT
Introduction to Algorithms
PPTX
Design and analysis of algorithms - Abstract View
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
Analysis of algorithm. big-oh notation.omega notation theta notation.performa...
AoA Lec Design of algorithm spresentation
introegthnhhdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhppt
Algorithms Analysis.pdf
Analysis Framework for Analysis of Algorithms.pdf
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
ADA_2_Analysis of Algorithms
Analysis of Algorithm full version 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
01 CS316_Introduction.pdf5959695559655565
Asymptotic Analysis
Data structure lecture 2
daa unit 1.pptx
Anlysis and design of algorithms part 1
Bt0080 fundamentals of algorithms1
Introduction to Algorithms
Design and analysis of algorithms - Abstract View
Ad

More from ShaistaRiaz4 (20)

PDF
Lecture3(b).pdf
PPT
02_Computer-Evolution(1).ppt
PPT
01_Introduction.ppt
PPTX
Algo_Lecture01.pptx
PPT
02_Computer-Evolution(1).ppt
PPT
01_Introduction.ppt
PDF
Bisma Zahid (1)-1.pdf
PPTX
MNS Lecture 1.pptx
PPTX
Plan (2).pptx
PDF
Lecture+9+-+Dynamic+Programming+I.pdf
PDF
Lecture 3(a) Asymptotic-analysis.pdf
PPTX
oppositional-defiant-disorder495.pptx
PPTX
Development Education.pptx
PPT
WISC-IV Introduction Handout.ppt
PPTX
Summary and Evaluation of the Book.pptx
PPT
MH&PSS for L&NFBED 7-8 April 2020.ppt
PPT
Intro_to_Literature_2012-2013-1.ppt
PPT
Coping strategies-Farzana Razi.ppt
PPT
Intellectual_development.ppt
PPTX
Integrating Technology Models.pptx
Lecture3(b).pdf
02_Computer-Evolution(1).ppt
01_Introduction.ppt
Algo_Lecture01.pptx
02_Computer-Evolution(1).ppt
01_Introduction.ppt
Bisma Zahid (1)-1.pdf
MNS Lecture 1.pptx
Plan (2).pptx
Lecture+9+-+Dynamic+Programming+I.pdf
Lecture 3(a) Asymptotic-analysis.pdf
oppositional-defiant-disorder495.pptx
Development Education.pptx
WISC-IV Introduction Handout.ppt
Summary and Evaluation of the Book.pptx
MH&PSS for L&NFBED 7-8 April 2020.ppt
Intro_to_Literature_2012-2013-1.ppt
Coping strategies-Farzana Razi.ppt
Intellectual_development.ppt
Integrating Technology Models.pptx

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
01-Introduction-to-Information-Management.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 Đ...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Business Ethics Teaching Materials for college
Insiders guide to clinical Medicine.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india
TR - Agricultural Crops Production NC III.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
VCE English Exam - Section C Student Revision Booklet
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
01-Introduction-to-Information-Management.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 Đ...
Microbial disease of the cardiovascular and lymphatic systems
O7-L3 Supply Chain Operations - ICLT Program
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Business Ethics Teaching Materials for college

Case Study(Analysis of Algorithm.pdf

  • 2. Case Study This case study is meant to demonstrate the salient features of algorithm design and analysis, as discussed previously. A simple example is used to systematically describe the following steps.: Algorithm Analysis • Problem statement • Algorithm design • Implementation using pseudo code • Analysis of best, worst and average running times • Space complexity • Correctness of algorithm • Visualization of Analysis
  • 3. Case Study ¾ Design an algorithm to find maximum element in an array of size n ¾ Analyze the algorithm to determine : • Time efficiency • Space efficiency • Correctness Problem Statement
  • 4. Case Study The design features are expressed in plain language. The algorithm for the solution of the problem consists of the following steps: Step #1: Store the first array element in variable max Step #2: Scan array to compare max with other elements Step #3: Replace max with a larger element, when found during the scan Step #4: Return value held by max Algorithm Design
  • 5. Algorithm Analysis FIND-MAX ( A, n ) 1 max ←A[1] ►Store first array element into variable max 2 for j←2 to n do ► Scan remaining elements 3 if ( A[j] > max ) ►Compare an element with max 4 then max ← A[j] ► Replace max with a larger element 5 return max ► Return maximum element The procedure FIND-MAX returns maximum element in an array. The array A and its size n, are passed as arguments. The following pseudo code describes the essential steps, together with comments which are identified by the symbol ►: Pseudo Code
  • 6. Running Time Costs of Basic Operations Cost of returning maximum element Cr return max 5 Cost of accessing A[j] Cost of storing A[j] into max Ca Cs then max ← A[j] 4 Cost of accessing A[j] Cost of comparing A[j] with max, and branching Ca Cc if ( A[j] > max ) 3 Cost of storing 2 into j Cost of comparing index j with n, and branching Cost of incrementing j Cs Cc Ci for j←2 to n do 2 Cost of accessing A[1] Cost of storing A[1] into max Ca Cs max ←A[1] 1 Remarks Unit costs Statement # First, we identify basic operations and their associated costs . The table below lists various operations and costs. Here the term ‘cost’ refers to the time consumed in executing an operation. Table of costs of basic operations
  • 7. Running Time Counts of Basic Operations 1 k k n-1 n-1 1 n-1 n-1 1 1 Operations Count Cr Cr return max 5 (Ca + Cs). k, where k depends on condition in statement 3 . In general, 0≤ k ≤ n -1 Ca Cs then max ← A[j] 4 (n-1).Ca + (n-1).Cc Ca Cc if ( A[j] > max ) 3 Cs + (n-1).Cc +(n-1).Ci Cs Cc Ci for j←2 to n do 2 Ca + Cs Ca Cs max ←A[1] 1 Total Cost Unit Cost Statement # Next we count the number of the basic operations, and total cost of executing each statement. The frequency of execution of statement 4 depends on the outcome of statement 3; it will be executed when the condition A[j]>max turns out to be true. This condition ,in turn, depends on the order of data in the array A. For the purpose of analysis, we assume that statement 4 is executed k times, where 0 ≤ k ≤ n-1. Table of frequency and total cost of basic operations
  • 8. Running Time Aggregate Cost • The running time T(n) is obtained by adding the costs in the last column of the table Tn) = Ca + Cs + Cs + (n-1).Cc + (n-1).Ci+ (n-1).Ca + (n-1).Cc + (Ca + Cs). k + Cr • Simplifying and rearranging, we get following expression . T(n) =A + B.k + C.n, where A = 2Cs – 2Cc – Ci + Cr, B = Ca + Cs, C = 2Cc + Ci + Ca • The constants A, B, C depend on the computing environment
  • 9. Running Time Classification Best, Worst, Average Cases The running time T(n) of the algorithm for finding the maximum element of array of size n is given by T(n) = A + B.k + C.n, where 0 ≤ k ≤ n-1 Here k is the number of times the statement max ← A[j] will be executed. The following possibilities can arise: Best Case: Best case occurs when the statement is not executed at all. This happens when the array maximum element occurs in the first cell. In this case k = 0, and best (minimum ) running time is given by Tbest (n)=A + C.n Average Case: In this case, the statement is executed on an average n / 2 times so that k = n / 2. Thus, average time running time is given by Taverage(n) = A + (B / 2 + C).n Worst Case: In this case, the statement is executed n-1 times; so k = n-1. This happens when the array is sorted in ascending order .Thus, worst (maximum) running time is given by Tworst(n) = A-B + (B + C).n
  • 10. Running Time Classification Plot of Best Worst and Average Cases A plot of running times for different cases is shown below. The running time in all of the three cases increases linearly. However, the slope of line (rate of increase) is different. In the case of worst time, for example, the running time increases at a greater rate.
  • 11. Algorithm Analysis FIND-MAX(A) 1 max ← A[1] 2 for j←2 to n do 3 if( A[j] > max) 4 then max ← A[j] 1 return max ƒ The correctness of FIND-MAX algorithm, listed below, is established by the loop invariant method. ƒ First, we define the loop invariant S as the following statement: Variable max holds the largest value at all stages of loop execution • Next, we consider the steps of initialization, maintenance, and termination . Correctness
  • 12. Algorithm Analysis Correctness • The Initialization condition requires that prior to first iteration the statement S should be true. This is trivially ( also called vacuously) true, because at this stage max contains the single element A[1]. • The Maintenance condition requires that if S is true before an iteration of loop, it should remain true after the iteration It can be easily verified that if max holds the largest of k elements, after kth iteration, then it holds largest of k+1 elements after the next iteration. ie.(k+1)st iteration • The Termination condition requires that post-condition should be true for problem size i.e, max should return maximum array element. The loop terminates when index j exceeds n. This implies that just after the last iteration max holds the largest of the first n elements of the array. Since array has size n, it means that max returns the largest of array elements.
  • 13. Analysis of Algorithm The space analysis of algorithm for finding maximum element is simple and straightforward. It amounts to determining space utilization as function of data structure size. • The total space requirement consists of memory used by the program statements and array element. The former is a fixed and does not depend on array size. • The amount of storage requirement for the array depends on the nature of data type (integer, floating point, strings). It increases in direct proportion to the array size • Thus, space efficiency is given by S(n) = A + B.n Space Efficiency Array space requirement Program space requirement Visualization