monday 9/13 - cs.plu.edu

13
Monday 9/13 Announcements: CS Summer Programming Recap Program 1 is posted – due Friday, 9/17 at 11:55 PM Homework 1 is posted – due today at 11:55 PM Problem 3-1 do part (a) not (c) -- Equations and drawings / “take a picture” Office Hours Next time: Sorting Insertion Sort (Section 2.1) Merge Sort (Read section 2.3) Today: Questions: Homework 1 / Program 1 In-class exercise from Friday – (quiz problems ??) 4 Solutions to problem 2 Formal Definition of O(n), Θ(n), and Ω(n) Useful Mathematical Formulas In-class exercise (if time permits)

Upload: others

Post on 16-Oct-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Monday 9/13Announcements:

CS Summer Programming Recap

Program 1 is posted – due Friday, 9/17 at 11:55 PM

Homework 1 is posted – due today at 11:55 PM

Problem 3-1 do part (a) not (c) -- Equations and drawings / “take a picture”

Office Hours

Next time:

Sorting

Insertion Sort (Section 2.1)

Merge Sort (Read section 2.3)

Today:

Questions: Homework 1 / Program 1

In-class exercise from Friday – (quiz problems ??)

4 Solutions to problem 2

Formal Definition of O(n), Θ(n), and Ω(n)

Useful Mathematical Formulas

In-class exercise (if time permits)

First In-class Exercise Suppose we are comparing implementations of insertion sort and merge

sort on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64n lg n steps (lg is log base 2). For what values of n does insertion sort beat merge sort?

Not a quiz question.

Describe (high level language or pseudocode) an algorithm that given a set S of n integers and another integer x, determines whether or not there exists two elements in S whose sum is exactly x.

What is “set” S?

Several Students: Brute-force solution

Cade: Using sets (HashSet or TreeSet)

Several Students: Sorting and Binary search

Avery and Kieran: Sorting and linear search

Asymptotic Order of Growth

A way of comparing functions that ignores constant factors and lower order terms

Informally what are O(g(n)), Ω(g(n)), Θ(g(n)) ?

O(g(n)) = { f(n) | f(n) grows no faster than g(n) }

Ω(g(n)) = { f(n) | f(n) grows at least as fast as g(n) }

Θ(g(n)) = {f(n) | f(n) grows at same rate as g(n)}

Θ(g(n)) O(g(n)) Ω(g(n)),

Formal Definition Big-O

Definition: f(n) is in O(g(n))

if there exist positive constants c and n0

such that

f(n) ≤ c g(n) for every n ≥ n0

Examples:

10n + 100 is O(n2)

5n2+20n +15 is O(n2)

Formal Definition Big-Ω

Definition: f(n) is in Ω(g(n))

if there exist positive constants c and n0

such that

c g(n) ≤ f(n) for every n ≥ n0

Examples:

10n - 100 is Ω(n)

5n2-200n +15 is Ω(n2)

Formal Definition Big- Θ

Definition: f(n) is in Θ(g(n))

if there exist positive constants c1, c2, and n0 such that

c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0

Examples:

10n - 100 is Θ(n)

5n2-200n +15 is Θ(n2)

Mathematical Properties

Theorem : f(n) Θ(g(n)) if and only if

f(n) O(g(n)) and f(n) Ω(g(n))

Transitivity Property: For O(g(n)) Ω(g(n)) Θ(g(n))

f(n) is Θ(g(n)) and g(n) is Θ(h(n)) ⇒ f(n) is Θ(h(n))

Theorem : If f1(n) O(g1(n)) and f2(n) O(g2(n)) , then

f1(n) + f2(n) O(max{g1(n), g2(n)})

Useful Facts Stirling’s Approximation (page 57)

n! (2n)1/2 (n/e)n

All logarithmic functions logb n belong to the same class

(log n) for all base b > 1

Polynomials of the same degree, k, belong to the same class:

cknk + ck-1nk-1 + … + c0 (nk) (ck>0)

Exponential functions an have different orders of growth fordifferent a’s

log n < n < an < n! < nn

where: >0 and a>1

Useful Mathematical Rules

Rules for Exponents (page 55) Rules and Notation for logarithmic functions

(page 56) Summation rules and tips (Appendix A)

In general we can rank functions Constant functionsComposition of logarithmic functionsPolylogarithmic functionsPolynomial functionsExponential functionsFactorial

14

Basic asymptotic efficiency classes

1 constant

log n logarithmic

n linear

n log n n-log-n

n2 quadratic

n3 cubic

2n exponential

n! factorial

Mathematical Tools

lim T(n)/g(n) =

0 order of growth of T(n) < order of growth of g(n)

c > 0 order of growth of T(n) = order of growth of g(n)

∞ order of growth of T(n) > order of growth of g(n) n→∞

L’Hôpital’s rule: If limn f(n) = limn g(n) = And the derivatives f´, g´ exist, then

f(n)g(n)

limn

= f ´(n)g ´(n)

limn

In-class Exercise

If we have time