Algorithm Analysis - Fundamentals of Computer Science - Lecture Slides, Slides of Computer Fundamentals

Algorithm Analysis, Algorithm Fast, Attendance Question, Grading Algorithms, Asymptotic Execution, Common Name, Data Set, Formal Definition, Actual Growth Rate, Apply the Definition are the important key points of lecture slides of Fundamentals of Computer Science.

Typology: Slides

2012/2013

Uploaded on 01/02/2013

umar
umar 🇮🇳

4.5

(4)

39 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.7
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

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?