SlideServe
Menu
Préférences
© SlideServe · v2026

Dynamic Programming (continued)

hilda-rivera avatar
132 diapositives · 1.7K Views · Nov 14, 2014

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)

Dynamic Programming (continued) - slide 1
01 / 132Dynamic Programming (continued)
1 / 17

Télécharger la présentation

Dynamic Programming (continued)
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Dynamic Programming (continued) Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009

  2. TCO 09 Algorithms (Feb.) Today

  3. TCO 09 Algorithms (March)

  4. Lecture Outline • Order Notation (for real this time) • Dynamic Programming w/ Iteration • Fibonacci numbers (revisited) • Text Segmentation (revisited) • ACM ICPC

  5. Algorithmic Complexity • We would like to be able to describe how long a program takes to run

  6. 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

  7. Algorithmic Complexity • Absolute time measurements are not helpful, as computers get faster all the time

  8. 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)

  9. Order Notation • We define O(*) as follows

  10. Order Notation • Basically, we just want to count the number of operations (without being too precise)

  11. Examples • What’s the complexity of the following code? int c = a + b;

  12. Examples • What’s the complexity of the following code? int c = a + b; • Answer: O(1)

  13. Examples • What’s the complexity of the following code? int c = a + b; • Answer: O(1) • (it actually depends on how you count)

  14. Example 2 for(int i = 0; i < n; ++i) { //do something }

  15. Example 2 for(int i = 0; i < n; ++i) { //do something } • Answer: O(n)

  16. 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

  17. Example 2 for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //do something } }

  18. Example 2 for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //do something } } • Answer: O(n2)

  19. 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

  20. Example 3 for(int i = 0; i < n; ++i) { for(int j = i; j < n; ++j) { //do something } }

  21. 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)

  22. 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)

  23. 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)…

  24. 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)

  25. Example 3 • n + (n–1) + … + 1 = ?

  26. Example 3 • n + (n–1) + … + 1 = ? • Call this sum N

  27. Example 3 • n + (n–1) + … + 1 = N • Call this sum N

  28. Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1

  29. Example 3 • n + (n–1) + … + 1 = N • Call this sum N N = n + (n-1) + … + 2 + 1 N = 1 + 2 + … + (n-1) + n

  30. 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)

  31. 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)

  32. 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

  33. 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)

  34. 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)

  35. Fibonacci Numbers • F0 = 0 • F1 = 1 • Fn = Fn-1 + Fn-2 for n > 1 • 0, 1, 1, 2, 3, 5, 8, 13, …

  36. Fibonacci Numbers int fibonacci(int n) { if(n == 0 || n == 1) return n; else return fibonacci(n-1) + fibonacci(n-2); }

  37. Fibonacci Numbers: Computing F(5)

  38. Fibonacci Numbers: Computing F(5) F(5) = active

  39. Fibonacci Numbers: Computing F(5) F(5) F(4)

  40. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3)

  41. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2)

  42. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1)

  43. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1)

  44. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)

  45. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)

  46. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(0)

  47. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)

  48. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)

  49. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(1) F(1) F(0)

  50. Fibonacci Numbers: Computing F(5) F(5) F(4) F(3) F(2) F(2) F(1) F(1) F(0)