CS2420: Lecture 4 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS2420: Lecture 4

Description:

F(N) is the time/space performance of P on instances of size N. Model of Computation ... Plus-equal on line 3 is executed n times and takes 2n time units. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 18
Provided by: Vladimir120
Category:
Tags: cs2420 | lecture | models | plus | size

less

Transcript and Presenter's Notes

Title: CS2420: Lecture 4


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)
  • 1 int rslt 0
  • 2 for(int i 1 i lt n i)
  • 3 rslt i
  • 4 return 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 n1
    times and takes (n1) 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
  • 1 time unit
  • n1 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
    Cgt0 And N0 gt 0 such that F(N) CG(N) for All N
    N0
  • 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(2N) is correct, but too loose to
    be useful.
  • F(N) 5N2 Is O(N4) 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) a0 a1n a2n2 amnm, Then
    f(n) O(nm).
  • Proof f(n) ?aini nm?ain(i-m) nm?ai.
  • Take C ?ai. Take n0 1.
  • f(n) O(nm).

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

17
Big-Oh of Polynomials Example 2
  • Claim F(N) 5N2 7N 3 O(N2).
  • Since m 2, then F(N) O(N2).
Write a Comment
User Comments (0)
About PowerShow.com