Slides9 8
Slides9 8
CSOR W4246
Eleni Drinea
Computer Science Department
Columbia University
Tuesday, September 8, 2015
Outline
1 Overview
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
Today
1 Overview
2 A first algorithm: insertion sort
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
Algorithms
Efficient Algorithms
Running time
Today
1 Overview
2 A first algorithm: insertion sort
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
Sorting
I
Sorting
I
Example
I
Input: n = 6, A = {9, 3, 2, 6, 8, 5}
Output: A = {2, 3, 5, 6, 8, 9}
Sorting
I
Example
I
Input: n = 6, A = {9, 3, 2, 6, 8, 5}
Output: A = {2, 3, 5, 6, 8, 9}
unsorted
sorted
key
key
i-1
unsorted
sorted
key
key
i-1
unsorted
sorted
key
key
i-1
unsorted
sorted
9
key
unsorted
sorted
3
6
key
unsorted
sorted
2
3
9
8
key
unsorted
sorted
2
3
9
key
unsorted
sorted
2
3
9
3
9
sorted
5
Pseudocode
Today
1 Overview
2 A first algorithm: insertion sort
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
Analysis of algorithms
Correctness
Running time
(Space)
Analysis of algorithms
Analysis of insertion-sort
Example of induction
Fact 1.
For all n 1,
Pn
i=1 i
n(n+1)
.
2
Example of induction
Fact 1.
For all n 1,
Pn
i=1 i
n(n+1)
.
2
Proof.
I
Base case: n = 1
Inductive hypothesis:
PAssume that the statement is
true for n 1, that is, ni=1 i = n(n+1)
.
2
Inductive step:
We show that the statement is true for
Pn+1
n + 1. That is, i=1 i = (n+1)(n+2)
. (Show this!)
2
Correctness of insertion-sort
Notation: A[i, j] is the subarray of A that starts at position i
and ends at position j.
Minor change in the pseudocode: in line 1, start from i = 1
rather than i = 2. How does this change affect the algorithm?
Claim 1.
Let n 1 be a positive integer. For all 1 i n, after the i-th
loop, the subarray A[1, i] is sorted.
Correctness of insertion-sort follows if we show Claim 1
(why?).
Proof of Claim 1
By induction on i.
I
i=2
i=2
3n2
2
7n
2
Worst-case analysis
Definition 2.
Worst-case running time: largest possible running time of the
algorithm over all inputs of a given size n.
Why worst-case analysis?
I
Today
1 Overview
2 A first algorithm: insertion sort
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
Efficient algorithms
Definition 4.
An algorithm is efficient if it has a polynomial running time.
Caveat
I
However
I
Remark 1.
Todays big data: even low degree polynomials might be too slow!
1. Can we do better?
2. And what is better?
I
Aymptotic analysis
Today
1 Overview
2 A first algorithm: insertion sort
3 Analysis of algorithms
4 Efficient algorithms
5 Asymptotic notation
T(n) = O(f(n))
c f(n)
T(n)
n
n0
Definition 6 (O).
We say that T (n) = O(f (n)) if there exist constants c > 0 and
n0 0 s.t. for all n n0 , we have T (n) c f (n) .
Examples:
I
T(n) = (f(n))
T(n)
c f(n)
n0
Definition 8 ().
We say that T (n) = (f (n)) if there exist constants c > 0 and
n0 0 s.t. for all n n0 , we have T (n) c f (n).
Examples:
I
T(n)
c1 f(n)
n
n0
Definition 10 ().
We say that T (n) = (f (n)) if there exist constants c1 , c2 > 0
and n0 0 s.t. for all n n0 , we have
c1 f (n) T (n) c2 f (n).
Equivalent definition
T (n) = (f (n)) if T (n) = O(f (n)) and T (n) = (f (n))
Examples:
I
T (n)
n f (n)
T (n)
n f (n)
Examples:
I
Definition 12 ().
We say that T (n) = (f (n)) if for any constant c > 0 there
exists n0 0 s.t. for all n n0 , we have T (n) > c f (n).
Definition 12 ().
We say that T (n) = (f (n)) if for any constant c > 0 there
exists n0 0 s.t. for all n n0 , we have T (n) > c f (n).
I
T (n)
n f (n)
= if the limit
Definition 12 ().
We say that T (n) = (f (n)) if for any constant c > 0 there
exists n0 0 s.t. for all n n0 , we have T (n) > c f (n).
I
T (n)
n f (n)
= if the limit