ANALYSIS OF ALGORITHMS AND BIG-O
CS16: Introduction to Algorithms & Data Structures
Tuesday, January 31, 2017
1
ANALYSIS OF ALGORITHMS AND BIG-O CS16: Introduction to Algorithms - - PowerPoint PPT Presentation
1 Tuesday, January 31, 2017 ANALYSIS OF ALGORITHMS AND BIG-O CS16: Introduction to Algorithms & Data Structures Tuesday, January 31, 2017 2 Outline 1) Running time and theoretical analysis 2) Big-O notation 3) Big- and Big- 4)
Tuesday, January 31, 2017
1
2
Tuesday, January 31, 2017
3
Tuesday, January 31, 2017
4
1000 2000 3000 4000 5000 6000 7000 8000 9000 50 100
Input Size Time (ms)
Tuesday, January 31, 2017
5
Tuesday, January 31, 2017
Friday, January 28, 2014
6
7
Tuesday, January 31, 2017
Friday, January 28, 2014
8
9
Tuesday, January 31, 2017
10
Tuesday, January 31, 2017
11
Tuesday, January 31, 2017
function possible_products(array): // Input: an array // Output: a list of all possible products // between any two elements in the list products = [] // make an empty list, 1 op for i in [0, array.length): // 1 op per loop for j in [0, array.length): // 1 op per loop per loop products.append(array[i] * array[j]) // 4 ops per loop per loop return products // 1 op
12
Tuesday, January 31, 2017
13 1E+1 1E+4 1E+7 1E+10 1E+13 1E+16 1E+19
1E+1 1E+3 1E+5 1E+7
Quadratic Quadratic Linear Linear
105n2 + 108n n2 10n + 105 n In this graph (log scale on both axes), the slope of a line corresponds to the growth rate of its respective function n T(n)
Tuesday, January 31, 2017
14
1 10 100 1,000 10,000 1 10 100 1,000
n 3n 2n+10 n
Tuesday, January 31, 2017
15
Tuesday, January 31, 2017
16
Tuesday, January 31, 2017
17
Tuesday, January 31, 2017
18
Tuesday, January 31, 2017
19
Tuesday, January 31, 2017
20 Big-Omega
Tuesday, January 31, 2017
21 Big-Theta
Tuesday, January 31, 2017
22
Tuesday, January 31, 2017
23
Tuesday, January 31, 2017
24
Tuesday, January 31, 2017
25
Tuesday, January 31, 2017
26
Tuesday, January 31, 2017
Friday, January 28, 2014
27
The recursive function for calculating the Fibonacci Sequence can be illustrated using a tree, where each row is a level of recursion. This diagram illustrates the calls made for fib(4).
28
fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) fib(4) fib(1) alone gets recomputed 3 times!
Tuesday, January 31, 2017
29
function dynamicFib(n): fibs = [] // make an array of size n fibs[0] = 0 fibs[1] = 1 for i from 2 to n: fibs[i] = fibs[i-1] + fibs[i-2] return fibs[n]
Tuesday, January 31, 2017
30
Tuesday, January 31, 2017
31
Tuesday, January 31, 2017
Tuesday, January 31, 2017
32