Title: List ADTs
1Algorithm Analysis
Capt Balazs Michelson 366
2Recall
- We are typically concerned with worst case
running time of algorithms as input grows - Run time is function of input
- We count basic steps
- When we count steps we put in terms of n
- We can eliminate lower order terms
3Recall
- Looked growth rates for alg_1 and alg_2
- We determined that alg_1 took k2 6k 19 steps
and alg_2 took 5k 13 steps - We are able to ignore constants and lower order
terms - alg_2 has a linear growth rate
- alg_1 grows like k2 (quadratic growth)
4Technically Speaking
- Notation used to describe algorithms running
time called Asymptotic Notation - It is mathematic notation for functions that
disregards constant factors and lower order terms - Describes growth rate of a function in terms of
input size
5? - Notation
- Function f(n) that has a quadratic growth rate is
said to grow like ?(n2) - Written as f(n) ?(n2)
- In other words
- f(n) has the same shape as n2
- f(n) grows at a rate proportional to n2
6More Generally
- f(n) ?(g(n))
- g(n) is n2 in our previous (specific) example
- alg_1 alg_2 example
- alg_1 f(n) ?(g(n)) g(n) is n2
- alg_2 f(n) ?(g(n)) g(n) is n
7f(n) ?(g(n))
After some cutoff point, n0 -where n0 is an
integer constant - f(n) is bound between constant
multiples of g(n). Therefore, f(n) grows like
g(n).
8Formal Definition
- ?(g(n)) f(n) ? (c1, c2, n0 gt0)
- where 0 ? c1g(n) ? f(n) ? c2g(n)
- ? n ? n0
- Less Formal
- A function (f(n)) belongs to the set ?(g(n)) if
there exists c1 c2 gt 0 such that f(n) can be
bound between c1g(n) and c2g(n) for all values
of n greater than a sufficiently large value (n ?
n0)
9Note!
- f(n) ?(g(n)) and f(n) ? ?(g(n)) are equivalent
- If f(n) ?(g(n)) then g(n) is an asymptotically
tight bound for f(n) - Key points
- ?(g(n)) is a tight bound
- ?(g(n)) is a set of functions
10Big-O Notation
- O(g(n)) f(n) ? (c, n0 gt0)
- where 0 ? f(n) ? c g(n)
- ? n ? n0
11Big-O
O(g(n)) describes a worst case upper bound. It
is an asymptotic upper bound for f(n). No
matter what, f(n) will be no worse than g(n).
12Big-O Example
- Use definition of big-O to show
- 2n2 22n -18 O(n2)
2n2 22n -18 ? 2n2 22n for all n gt 0 Want
n2 on right 22n ? n2 22 ? n 2n2 22n -18 ?
2n2 n2 for all n ? 22 2n2 22n -18 ? 3n2 for
all n ? 22 Thus, with c 3 and n0 22 we have
shown that 2n2 22n -18 O(n2)
13Example of Asymptotic Upper Bound
4g(n)4n2
- 4 g(n) 4n2
- 3n2 n2
- ? 3n2 9 for all n ? 3
- gt 3n2 5
- f(n)
- Thus, f(n)O(g(n))
f(n)3n25
g(n)n2
3
14Use Definition of Big-O to show that 6n3 is not
O(n2)
15? - Notation
- ?(g(n)) f(n) ? (c, n0 gt0)
- where 0 ? cg(n) ? f(n)
- ? n ? n0
Best case running time or lower bound
16Proving Tight Bound
- Can be difficult
- f(n) ?(g(n))
- iff f(n) O(g(n)) f(n) ?(g(n))
17f(n) ?(g(n)) iff f(n) O(g(n)) f(n) ?(g(n))
- Implies
- if f(n)O(g(n)) and g(n)O(f(n)) then f(n)?(g(n))
18f(n) ?(g(n)) iff f(n) O(g(n)) f(n) ?(g(n))
- Proof
- g(n) O(f(n)) implies that g(n) ? cf(n)
- 1/c g(n) ? f(n)
- let c 1/c
- cg(n) ? f(n)
- f(n) ? cg(n)
- ?f(n) ?(g(n))
- Since f(n) ?(g(n)) and g(n) O(f(n)), it
follows that f(n) ?(g(n))
19Useful Properties of Asymptotic Notation
- if a is a constant, O(af(n)) O(f(n))
- Ex O(32n3) O(n3)
- if f(n) grows as fast as or faster than g(n)
(remember the growth rate hierarchy), then O(f(n)
g(n)) O(f(n)) - Ex O(5n4 3n2) ?(n4)
- O(f(n)) O(g(n)) O(f(n) g(n))
- Ex ?(n) ?(log n) ?(n log n)
- O(f(n)) O(g(n)) O(f(n) g(n))
20Show 8n3 - 5n2 10 O(n3)