1.36k likes | 1.63k Vues
Dynamic Programming (continued). Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009. TCO 09 Algorithms (Feb.). Today. TCO 09 Algorithms (March). Lecture Outline. Order Notation (for real this time) Dynamic Programming w/ Iteration Fibonacci numbers (revisited)
E N D
Dynamic Programming (continued) Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009
TCO 09 Algorithms (Feb.) Today
Lecture Outline • Order Notation (for real this time) • Dynamic Programming w/ Iteration • Fibonacci numbers (revisited) • Text Segmentation (revisited) • ACM ICPC
Algorithmic Complexity • We would like to be able to describe how long a program takes to run
Algorithmic Complexity • We would like to be able to describe how long a program takes to run • We should express the runtime in terms of the input size
Algorithmic Complexity • Absolute time measurements are not helpful, as computers get faster all the time
Algorithmic Complexity • Absolute time measurements are not helpful, as computers get faster all the time • Also, runtime is clearly dependent on the input (and input size)
Order Notation • We define O(*) as follows
Order Notation • Basically, we just want to count the number of operations (without being too precise)
Examples • What’s the complexity of the following code? int c = a + b;
Examples • What’s the complexity of the following code? int c = a + b; • Answer: O(1)
Examples • What’s the complexity of the following code? int c = a + b; • Answer: O(1) • (it actually depends on how you count)
Example 2 for(int i = 0; i < n; ++i) { //do something }
Example 2 for(int i = 0; i < n; ++i) { //do something } • Answer: O(n)
Example 2 for(int i = 0; i < n; ++i) { //do something } • Answer: O(n) • O(c) operations for each i O(cn) == O(n) total operations
Example 2 for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //do something } }
Example 2 for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //do something } } • Answer: O(n2)
Example 2 for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //do something } } • Answer: O(n2) • O(n) operations for each i == n * n total operations == O(n2) total operations
Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } }
Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } } • The first iteration of the outer loop takes O(n)
Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } } • The second iteration of the outer loop takes O(n-1)
Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } } • The third iteration of the outer loop takes O(n-2)…
Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } } • The last iteration of the outer loop takes O(1)
Example 3 • n + (n–1) + … + 1 = ?
Example 3 • n + (n–1) + … + 1 = ? • Call this sum N
Example 3 • n + (n–1) + … + 1 = N • Call this sum N
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n • Now, add these two together: 2N = (n + 1) + ((n-1) + 2) + … + (1 + n)
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n • Now, add these two together: 2N = (n + 1) + ((n-1) + 2) + … + (1 + n) = n * (n + 1)
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n • Now, add these two together: 2N = (n + 1) + ((n-1) + 2) + … + (1 + n) = n * (n + 1) N = n * (n + 1) / 2
Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n • Now, add these two together: 2N = (n + 1) + ((n-1) + 2) + … + (1 + n) = n * (n + 1) N = n * (n + 1) / 2 = O(n2)
Examples • This is also the number of ways 2 items can be chosen from a set of (n + 1) items, disregarding order and without replacement (also called “(n + 1) choose 2”, binomial coefficient)
Fibonacci Numbers • F0 = 0 • F1 = 1 • Fn = Fn-1 + Fn-2 for n > 1 • 0, 1, 1, 2, 3, 5, 8, 13, …
Fibonacci Numbers int fibonacci(int n) { if(n == 0 || n == 1) return n; else return fibonacci(n-1) + fibonacci(n-2); }
Fibonacci Numbers: Computing F(5) F(5) = active
Fibonacci Numbers: Computing F(5) F(5) F(4)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)
Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(2) F(1) F(1) F(0)