Download Algorithm Analysis - Fundamentals of Computer Science - Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!
Algorithm Analysis
"bit twiddling: 1. (pejorative) An exercise in tuning (see
tune ) in which incredible amounts of time and effort go to produce little noticeable improvement, often with the result that the code becomes incomprehensible."
- The Hackers Dictionary, version 4.4.
Is This Algorithm Fast?
- Problem: given a problem, how fast does this code solve that problem?
- Could try to measure the time it takes, but that is subject to lots of errors - multitasking operating system - speed of computer - language solution is written in
Grading Algorithms
- What we need is some way to grade algorithms and their representation via computer programs for efficiency - both time and space efficiency are concerns - are examples simply deal with time, not space
- The grades used to characterize the algorithm and code should be independent of platform, language, and compiler - We will look at Java examples as opposed to pseudocode algorithms
Big O
- The most common method and notation for discussing the execution time of algorithms is "Big O"
- Big O is the asymptotic execution time of the algorithm
- Big O is an upper bounds
- It is a mathematical tool
- Hide a lot of unimportant details by assigning a simple grade (function) to algorithms
Big O Functions
- N is the size of the data set.
- The functions do not include less dominant terms and do not include any coefficients.
- 4N 2 + 10N – 100 is not a valid F(N).
- It would simply be O(N^2 )
- It is possible to have two independent variables in the Big O function. - example O(M + log N) - M and N are sizes of two different, but interacting data sets
Actual vs. Big O
8
Amount of data
Time for algorithm to complete
Actual
Simplified
What it Means
- T(N) is the actual growth rate of the algorithm
- can be equated to the number of executable statements in a program or chunk of code
- F(N) is the function that bounds the growth rate - may be upper or lower bound
- T(N) may not necessarily equal F(N)
- constants and lesser terms ignored because it is a bounding function
Yuck
- How do you apply the definition?
- Hard to measure time without running programs and that is full of inaccuracies
- Amount of time to complete should be directly proportional to the number of statements executed for a given amount of data
- Count up statements in a program or method or algorithm as a function of the amount of data - This is one technique
- Traditionally the amount of data is signified by the variable N (^) Docsity.com 11
Assumptions in For Counting Statements
- Once found accessing the value of a primitive is constant time. This is one statement: x = y; //one statement
- mathematical operations and comparisons in boolean expressions are all constant time. x = y * 5 + z % 3; // one statement
- if statement constant time if test and maximum time for each alternative are constants if( iMySuit == DIAMONDS || iMySuit == HEARTS ) return RED; else return BLACK; // 2 statements (boolean expression + 1 return)
Counting Statements in Loops
Attendenance Question 2
- Counting statements in loops often requires a bit of informal mathematical induction
- What is output by the following code? int total = 0; for(int i = 0; i < 2; i++) total += 5; System.out.println( total );
A. 2 B. 5 C. 10 D. 15 E. 20
Counting Statements in Nested Loops Attendance Question 4
- What is output by the following code? int total = 0; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) total += 5; System.out.println( total ); A. 0 B. 10 C. 20 D. 30 E. 40
• What is output by the following code?Attendance Question 5
int total = 0; // assume limit is an int >= 0 for(int i = 0; i < limit; i++) for(int j = 0; j < limit; j++) total += 5; System.out.println( total );
A. 5
B. limit * limit
C. limit * limit * 5
D. 0
E. limit 5
Counting Up Statements
- int result = 0; 1 time
- int i = 0; 1 time
- i < values.length; N + 1 times
- i++ N times
- result += values[i]; N times
- return total; 1 time
- T(N) = 3N + 4
- F(N) = N
- Big O = O(N)
Showing O(N) is Correct
- Recall the formal definition of Big O
- T(N) is O( F(N) ) if there are positive constants c and N 0 such that T(N) < cF(N) when N > N 0
- In our case given T(N) = 3N + 4, prove the method is O(N). - F(N) is N
- We need to choose constants c and N (^0)
- how about c = 4, N 0 = 5?