Big-O Notation and Intro to Divide and Conquer

advertisement
Big-O
Notation and
Intro to Divide
and Conquer
MODULE 1

Algorithm: “a process or set of rules to be followed
in calculations or other problem-solving
operations, especially by a computer.”
(Wikipedia)

How do we measure the performance of
algorithms?
Big-O Notation
Examples:

O(1)

O(N)
int x = myarray[5];
for(int i = 0; i<N; i++)
myarray[i] = N-i;

O(N)2
for(int i = 0; i<N; i++)
for(int j = 0; j<N; j++)
myarray[i][j] = i+j;
Has 2^N Running Time

O (2N)
Generating all subsets of an N length
array, myarray = {1, 2, 3}
{} 3C0=1
1 3C1=3
2
3
1, 2
3C2=3
1, 3
2, 3
1, 2, 3 3 C 3 = 1

Aside: Why are there 2n subsets?
Why are there 2^N
Subsets?

𝑥+𝑎
𝑛
𝑛
=
𝑘=0


Binomial Theorem
𝑥 𝑘 𝑎𝑛−𝑘
The sum of the binomial coefficients is equal to 2n
𝑥+1
𝑛
𝑛
=
𝑘=0

𝑛
𝑘
Substitute x=1,
𝑛
𝑘
𝑥𝑘
2𝑛 =
𝑛
𝑘=0
𝑛
𝑘
What about O (logN)
running time?

O (log N)
Performing a Binary Search through a
Sorted Array


1, 2, 4, 6, 7, 12, 15, 18,

Does 5 exist?

7>5, so 5 must be before 7

4<5, so 5 must be after 4 but before 7

5! =6, there’s nowhere else to go, so return
“search failed”


This process took 3 steps, why?
Measuring Algorithms:
Selection Sort
Selection Sort: 4, 3, 2, 1

1, 4, 3, 2 (4 steps to find 1)

1, 2, 4, 3 (3 steps to find 2)

1, 2, 3, 4 (2 steps to find 3)

1 step to find 4

10 steps, for a list of length 4.
What is the Running Time?

Merge Sort:
O(n log n)

How is Merge Sort different from Quick Sort?

Task: Write a program that calculates the nth Fibonacci
number and measure its running time.

1, 1, 2, 3, 5, 8, 13, 21…

“Perhaps the most important principle of all, for the good
algorithm designer is to refuse to be content.” Aho, Hopcroft,
and Ullman


Tn = Tn-1 + Tn-2, n>1
Problem Set: CCC 2005 #5 Pinball (Hint:
It can be solved
with a modification of merge sort or with binary
search)
Another example of divide and conquer: Karatsuba
Multiplication
FatalEagle is
god
Download