step count method examples
step count method examples
The cost of the header Case <expr> of is given a cost equal to that
assignable to <expr>. The cost of each following condition-statement
pair is the cost of this condition plus that of all preceding conditions plus
that of this statement.
If-Then-Else statements: the if-then-else statement consists of three
parts:
If <expr>
Then <statement 1>
Else <statement 2>
Each part is assigned the number of steps corresponding to <expr>,
<statement 1>, and <statement 2> respectively, note that if the else
clause is absent, then no cost is assigned to it.
Procedure and Function invocation: all invocations of procedures and
functions count as one step unless the invocation involves value
parameters whose size depends on the instance characteristics. In this
latter case, the count is the sum of the size of these value parameters. In
case the procedure/ function being invoked is recursive, then we must
also consider the local variable in the procedure or function being
invoked. The sizes of local variables that are characteristic dependent
need to be added into the step count.
Begin, End, With, and Repeat statements: each With statement counts
as one step. Each Begin, End, and Repeat statement counts as zero
steps.
Procedure and function statements: these count as zero steps as their
cost has already been assigned to the invoking statements.
Goto statement: this has a step count of one.
Example 1: Find the space and the time complexities for the following function
using step count method.
Function Abc(a, b, c : real) : real;
Begin
Abc := a + b + b * c + (a + b – c) / (a + b) + 4;
End;
Solution:
Space complexity: One word of type real is a adequate to store the values of
each of a, b, c, 4, and Abc. We see that the space needed by function Abc is
independent of the instance characteristics. Consequently,
SAbc(a, b, c) = 0
Time complexity:
TAbc(a, b, c) = 1
Example 2: Find the space and the time complexities for the following function
using step count method.
Function sum1(n : integer) : integer;
var k, s : integer;
Begin
s := 0;
For k := 1 to n do
s := s + k;
sum1 := s;
End;
Solution:
Space complexity: The function sum1 requires six words of type integer to
store the values of each ( n, k, s, sum1, and constants 0, 1). We see that the
space needed by function sum1 (12 Bytes) is independent of the instance
characteristics. Consequently,
Ssum1(n) = 0
Time complexity:
Tsum1(n) = 1 + (n+1) + n + 1 = 2n + 3
Example 3: Find the space and the time complexities for the following function
using step count method.
Function sum2( a: ElemList ; n : integer) : real;
var k : integer;
s: real;
Begin
s := 0;
For k := 1 to n do
s := s + a[k];
sum1 := s;
End;
Solution:
Space complexity: The function sum2 requires six spaces (four of type integer
and two of type real) to store the values of each ( n, k, s, sum2,and constants
0, 1). The space needed by a is the space needed by variables of type
ElemList. This is equal to MaxSize. We see that the space needed by function
sum2 is
Ssum2(n) = 16 Bytes + Maxsize
Or Ssum2(n) ≥ n
Note: if we change the formal parameter a from value to reference ( or Var),
only the address of the actual parameter gets transferred to the function and
the space needed by the function is independent of instance characteristics
(n), in this case Ssum2(n) = 0.
Time complexity:
Tsum2(n) = 1 + (n+1) + n + 1 = 2n + 3