Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
387 views

Case Study (Analysis of Algorithm

The document summarizes an algorithm for finding the maximum element in an array and analyzes its running time complexity. It describes the problem statement, algorithm design using pseudocode, and analysis of the best, worst, and average cases. The running time is shown to be linear in the size of the array n. Space complexity is also linear due to storage of the array. Correctness is established using a loop invariant.

Uploaded by

nimrashafiq604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
387 views

Case Study (Analysis of Algorithm

The document summarizes an algorithm for finding the maximum element in an array and analyzes its running time complexity. It describes the problem statement, algorithm design using pseudocode, and analysis of the best, worst, and average cases. The running time is shown to be linear in the size of the array n. Space complexity is also linear due to storage of the array. Correctness is established using a loop invariant.

Uploaded by

nimrashafiq604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Case Study

Algorithm Analysis
Case Study
Algorithm Analysis
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.:

• 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
Problem Statement
¾ Design an algorithm to find maximum element in an array of size n

¾ Analyze the algorithm to determine :

• Time efficiency

• Space efficiency

• Correctness
Case Study
Algorithm Design
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 Analysis
Pseudo Code
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 ►:

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


Running Time
Costs of Basic Operations
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.

# Statement Unit costs Remarks

1 max ←A[1] Ca Cost of accessing A[1]


Cs Cost of storing A[1] into max
2 for j←2 to n do Cs Cost of storing 2 into j
Cc Cost of comparing index j with n, and branching
Ci Cost of incrementing j
3 if ( A[j] > max ) Ca Cost of accessing A[j]
Cc Cost of comparing A[j] with max, and branching
4 then max ← A[j] Ca Cost of accessing A[j]
Cs Cost of storing A[j] into max
5 return max Cr Cost of returning maximum element

Table of costs of basic operations


Running Time
Counts of Basic Operations
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.

# Statement Unit Cost Operations Total Cost


Count
1 max ←A[1] Ca 1 Ca + Cs
Cs 1
2 for j←2 to n do Cs 1 Cs + (n-1).Cc +(n-1).Ci
Cc n-1
Ci n-1
3 if ( A[j] > max ) Ca n-1 (n-1).Ca + (n-1).Cc
Cc n-1
4 then max ← A[j] Ca k (Ca + Cs). k, where k depends on
Cs k condition in statement 3 . In general, 0≤ k
≤ n -1
5 return max Cr 1 Cr

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
Correctness
ƒ The correctness of FIND-MAX algorithm, listed below, is established by the
loop invariant method.

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

ƒ 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


.
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
Space Efficiency
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

Program space Array space


requirement requirement

Visualization
Visualization

You might also like