Title: Mathematical Analysis of Non Recursive Algorithms
1Mathematical Analysis of Non Recursive
Algorithms (Section 2.3)
2Non Recursive Algorithms
Complexity of Algorithm X
Algorithm X (n) i ? 5 j ?
while ( i lt ) k ?
Algorithm Y() for ()
while ()
return
3MaxElement
MaxElement(A1..n) maxval ? A1 for i ? 2
to n do if Ai gt maxval
maxval ? Ai return maxval
4UniqueElement
UniqueElement(A1..n) for i ? 1 to n-1 do
for j ? i1 to n do if Ai
Aj return false return true
5MatrixMultiplication
UniqueElement(A1..n,1..n, B1..n,1..n) for i
? 1 to n do for j ? 1 to n do
Ci,j ?0 for k ? 1 to n do
Ci,j ? Ci,j Ai,k
Bk,j
return C
6Recursive Algorithms
Algorithm X (n, ) if (n 0) return value
while ( i lt )
return Algorithm X(n1) // where n1 lt n
Complexity of Algorithm X T(n)
//n is the size of the input
T(n) f(n) T(n1) T(0) 1
7Solving a Recursive Equation
- Make a few evaluations of T(n) for a few values
n1, n2, n3 according to the recursive call - Deduce a pattern for T(n)
- Compute the number of times, k, the recursive
call is made until the termination condition
(e.g.,. T(0)) - Use 1 and 3 for obtaining a final equation T(n)
- Solve the equation T(n)
8SumElement
- SumElement(A1..n, i)
- If (i n) then return An
- Else
- return Ai sumElement(A,i1)
-