Data Structures and Algorithms
Algorithm Analysis
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Outline
What is an Algorithm? Algorithm Formulation Algorithm Analysis Frequency Count Operation Count
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
What is an Algorithm?
An organized sequence or list of clear steps or operations needed to solve a given programming problem 3 components:
Inputs Steps or instructions Output
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
What is an Algorithm?
Any well-defined computational procedure that takes some value(s) as input and produces some value(s) as output Sequence of computational steps that transform the input to output
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
What is an Algorithm?
A means to solve a problem A problem can be solved in many different ways:
Many algorithms can be used to solve the same problem
We choose between different algorithms based on their efficiency
A good metric of efficiency is the computational complexity of an algorithm
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Algorithms and Computer Science
Can a particular task be accomplished by a computing device? What is the minimum number of steps or operations for any algorithm to perform a certain function? Concerns: effectiveness, efficiency
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Algorithm Formulation
1.
Steps: Understand the problem
Determine the given inputs, the desired output, and the steps to reach the output
2.
Design the algorithm
Write the algorithm that will solve the problem given the inputs
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Algorithm Formulation
3.
Analyze the algorithm
Determine the resources that the algorithm requires memory, bandwidth, hardware, computational time
4. 5.
Refine the algorithm Verification
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Algorithm Analysis
Study the behavior of the algorithm to determine its pattern and performance
Measured in terms of execution time (speed) and amount of memory (space) consumed Designing efficient algorithms
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
How to analyze programs?
1.
2 phases: Priori estimates
Obtain a function which bounds the algorithms complexity time The amount of time a single execution will take, or the number of times a statement is executed Space consumed may also be estimated
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
How to analyze programs
It is impossible to know the exact amount of time to execute any command unless the following are known:
The machine for execution machine instruction set time required by each machine instruction translation of the compiler from source to machine language
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
How to analyze programs?
2.
Posteriori estimates
Study of exact time & space required for execution
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count
Number of statements or steps needed by the algorithm to finish Simple statements:
a 10 ba*2
Count: 1 Count: 1
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count
Conditional Statements
if (<condition>) <S1>; else <S2>; Sum of the following: 1 + Max{ Count(<S1>), Count(<S2>) }
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Example
if x > 1
y 10; else { y 20; z 30;
2 1 2
}
Total = 3
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count
Loop Statements
for i <lb> to <ub> <S1> ub - lb + 2 ub - lb + 1
Example for i 1 to n xx+1
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
n - 1 + 2 = n+1 n
Total = 2n+1
Frequency Count - Example
if x < 1 1 y 10 else if x < 2 2 {y 20; z 30 } else { for i 1 to x x+1 print(i) x } 1
1
2x+1
Total = 2x+3
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
1. k 500; for i 1 to k-1 zz+1
2. for k 0 to n { print (k); print(n-k);
}
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count
Nested Statements
n - 1 + 2 = n+1 for i 1 to n for j 1 to n (n+1)(n) (n) = <S1> xx+1 (n) (n)
Total = n+1 + n2+n + n2 = 2n2 + 2n + 1
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Example
for i 2 to n-1 n-1 - 2 + 2 = n-1 for j 1 to n (n+1) (n-2) <S1> xx+1 = (n-2) (n) (n-2)
Total = n-1 + n2-2n+n-2 + n2-2n = 2n2 - 2n - 3
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Example
<S1> = (n)
for i 1 to n n+1 { xx+1 n for j 3 to n+1 n(n) { y y + 1 (n-1) (n) <S2> z z + 1 (n-1) (n) = (n-1) } }
Total = n+1 + n + n2 + (n-1)n + (n-1)n = 2n+1 + n2 + n2-n + n2-n College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines = 3n2 + 1
Frequency Count - Exercise
for i 1 to n for j 1 to n for k 1 to n zz+1
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count
Loop Statements
do <S1> while <condition>
Ex.
x1 do xx+1 while x < n
What if while x <= n? What if x = 0?
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
n-x=n-1 n-x=n-1 Total = 2n-1
Frequency Count
Loop Statements
Ex.
while <condition> <S1> x1 while x < n xx+1 1 n-x+1=n n-1 Total = 2n
What if while x <= n? What if x = 0?
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
1. x1 while x <= n xx+1
x1 do yy+1 xx+1 while x <> n-1
1 n-x+2=n+1 n Total = 2n + 2 1 n-1-x=n-2 n-1-x=n-2 n-1-x=n-2 Total = 3n - 5
2.
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
1. for i 1 to n for j 1 to 2n xx+1 for k 2 to n+1 for j 3 to n-3 xx+1
2.
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
3. for i 2 to n+1 for j 3 to n-3 for k 4 to n-4 xx+1 for i 1 to n { j2 while j<=n+3 { print(A[i],A[j-1]); j++; } }
4.
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
5. for i 1 to n for j 1 to i xx+1
6.
for i 1 to n for j 2i downto 1 xx+1 for i 1 to n for j i to n xx+1
7.
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
8. for i 1 to n-1 for j 1 to i xx+1 for i 4 to n for j 1 to i xx+1
9.
10. for i 1 to n for j 1 to i for k 1 to j xx+1
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines
Frequency Count - Exercise
11. while (i < n) { k = k+1; i = i+1; } 12. while (i>=n) { k= k+1; x = x+1; i = i-1; } 13. while (i > n) { k = k+1; i = i-1; } while (b != n-10) b= b+1; do { h = h-1; } while (h >= n);
14. 15.
College of Computer Studies, De La Salle University Manila 2401 Taft Avenue, Metro Manila 1004 Philippines