Download presentation

Presentation is loading. Please wait.

1
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University

2
Outline Algorithm Analysis (Chapter 2)

3
Recall Big Question 1 I have designed an algorithm. How fast does it run?

4
Asymptotic Analysis We would like to determine time/space performance of programs regardless of constant factors (differences in compilers and operating systems and differences in programming languages). In asymptotic analysis, N is an instance characteristic, typically the size of the input to a program P that implements some algorithm A. F(N) is the time/space performance of P on instances of size N.

5
Model of Computation We have a standard digital computer. One instruction is executed at a time. There is a small finite set of primitive instructions that can represented an arbitrary complex instruction in any programming language. Every primitive instruction can be executed in constant time. We have sufficient memory to execute complex programs.

6
Computing The Arithmetic Sum int sum(int n) { 1int rslt = 0; 2for(int i = 1; i <= n; i++) 3rslt += i; 4return rslt; }

7
Computing The Arithmetic Sum Assignment on line 1 is executed once and takes 1 time unit. Assignment inside the for-loop is executed once and takes 1 time unit. Comparison inside the for-loop is executed n+1 times and takes (n+1) time units. Increment inside the for-loop is executed n times and takes n time units. Plus-equal on line 3 is executed n times and takes 2n time units. Return on line 4 is executed once and takes 1 time unit.

8
Computing The Arithmetic Sum We need to compute the sum total of the following time units: –1 time unit –n+1 time units –n time units –2n time units –1 time unit The sum total = 4 + 4n.

9
Computing The Arithmetic Sum What is the complexity of computing the arithmetic sum? T(N) = 4N + 4.

10
Computing The Arithmetic Sum int sum2(int n) { return n * (n + 1) / 2; }

11
Computing The Arithmetic Sum What is the complexity of sum? T(N) = 4.

12
Big-Oh Notation F(N) = O(G(N)) IFF there exist positive constants C>0 And N 0 > 0 such that F(N) ≤ CG(N) for All N ≥ N 0 The Objective is to find the Least (Smallest) Upper Bound

13
Big-Oh: Tight Bounds The objective is to find as tight an upper bound as possible. Loose bounds are easy but not that Useful F(N) = 4N is O(2 N ) is correct, but too loose to be useful. F(N) = 5N 2 Is O(N 4 ) is correct, but too loose to be useful.

14
Worst Case vs. Average Case Worst-Case Analysis (Big-Oh) gives an upper performance bound over all inputs of size N. This is the most useful and most frequent measure of performance. Average-Case Analysis gives an average time bound over all inputs of size N. Average case analysis is often hard to do, because it is unclear what is “average.”

15
Big-Oh of Polynomials Theorem: f(n) = a 0 + a 1 n + a 2 n 2 + … + a m n m, Then f(n) = O(n m ). Proof: f(n) ≤ ∑|a i |n i ≤ n m ∑|a i |n (i-m) ≤ n m ∑|a i |. Take C = ∑|a i |. Take n 0 = 1. f(n) = O(n m ).

16
Big-Oh of Polynomials: Example 1 Claim: F(N) = 3N + 2 = O(N). Since m = 1, then F(N) = O(N 1 ) = O(N).

17
Big-Oh of Polynomials: Example 2 Claim: F(N) = 5N 2 + 7N + 3 = O(N 2 ). Since m = 2, then F(N) = O(N 2 ).

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google