Successfully reported this slideshow.
Upcoming SlideShare
×

# Dynamic programming 101

134 views

Published on

A quick introduction to dynamic programming and graph theory

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Dynamic programming 101

1. 1. Dynamic programming 101 Tobias Nyholm @tobiasnyholm @tobiasnyholm
2. 2. @tobiasnyholm Why?
3. 3. @tobiasnyholm Tobias Nyholm • Full stack unicorn on Happyr.com • Certiﬁed Symfony developer • Symfony core member • PHP-Stockholm • Open source
4. 4. @tobiasnyholm Open source PHP-cache HTTPlug Mailgun LinkedIn API clientSwap Stampie BazingaGeocoderBundle PHP-Geocoder FriendsOfApi/boilerplate Guzzle Buzz CacheBundlePSR7 SymfonyBundleTest NSA SimpleBus integrations PSR HTTP clients Neo4j KNP Github API PHP-Translation Puli Assert
5. 5. @tobiasnyholm You’ve got a problem Problem
6. 6. @tobiasnyholm Divide and Conquer Problem
7. 7. @tobiasnyholm Recursion Iterative function Recursive function <?php function factorial(\$n) { \$result = 1; for (\$i = 1; \$i <= \$n; \$i++) { \$result = \$result * \$i; } return \$result; } <?php function factorial(\$n) { if (\$n <= 1) { return 1; } return \$n * factorial(\$n-1); }
8. 8. @tobiasnyholm Fibonacci
9. 9. @tobiasnyholm Fibonacci 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597+ = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17n: <?php function fib(\$n) { if (\$n <= 1) { return \$n; } return fib(\$n-1) + fib(\$n-2); }
10. 10. @tobiasnyholm Fibonacci 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 f(n) = f(n-2) + f(n-1) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17n: f(1000) = x  f(1001) = y f(1002) = ?
11. 11. @tobiasnyholm ﬁb(2) + ﬁb(1) ﬁb(1) + ﬁb(0) ﬁb(1) + ﬁb(0) Fibonacci ﬁb(5) ﬁb(4) + ﬁb(3) ﬁb(3) + ﬁb(2) ﬁb(2) + ﬁb(1) <?php function fib(\$n) { if (\$n <= 1) { return \$n; } return fib(\$n-1) + fib(\$n-2); }
12. 12. @tobiasnyholm Fibonacci <?php class Calculator { private \$cache = [0 => 0, 1 => 1]; public function fib(\$n) { if (!isset(\$this->cache[\$n])) { \$this->cache[\$n] = fib(\$n - 1) + fib(\$n - 2); } return \$this->cache[\$n]; } }
13. 13. @tobiasnyholm Dynamic programming Divide the problem into overlapping subproblems to reduce the number of calculations
14. 14. @tobiasnyholm ﬁb(2) + ﬁb(1) ﬁb(1) + ﬁb(0) ﬁb(1) + ﬁb(0) Fibonacci ﬁb(5) ﬁb(4) + ﬁb(3) ﬁb(3) + ﬁb(2) ﬁb(2) + ﬁb(1)
15. 15. @tobiasnyholm ﬁb(0) Fibonacci ﬁb(5) ﬁb(4) + ﬁb(3) ﬁb(3) + ﬁb(2) ﬁb(1)
16. 16. The more fun stuff
17. 17. Graph theory
18. 18. @tobiasnyholm Graph theory
19. 19. @tobiasnyholm Graph theory 5
20. 20. @tobiasnyholm Graph theory 5 3 3 2 5 4 6 2 3 2 3 7 1 2 4 4 2 2 A B 6
21. 21. @tobiasnyholm Graph theory 5 3 3 2 5 4 6 2 3 2 3 7 1 2 4 4 2 2 A B 6 B 5 A 3 7 8 8 5 5
22. 22. @tobiasnyholm Graph theory 5 3 3 2 5 4 6 2 3 2 3 7 1 2 4 4 2 2 A B 6 B 5 A 3 7 8 8 5 5 11 7 9 7
23. 23. @tobiasnyholm Graph theory 5 3 3 2 5 4 6 2 3 2 3 7 1 2 4 4 2 2 A 12 6 5 A 3 7 8 8 5 5 11 7 9 7
24. 24. @tobiasnyholm Matrix maze 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j
25. 25. @tobiasnyholm Matrix maze 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j
26. 26. @tobiasnyholm Breadth ﬁrst 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j i, j -> value  3, 0 -> 3  4, 0 -> 4 4, 1 -> 5 3, 1 -> 5
27. 27. @tobiasnyholm Depth ﬁrst 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j i, j -> value  3, 0 -> 3  4, 0 -> 4 4, 1 -> 5 3, 1 -> 5
28. 28. @tobiasnyholm Greedy algorithm 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j
29. 29. @tobiasnyholm Dijkstra’s algorithm 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j 3 4 5 8 5 6 14 7 14 10 11 10 9 11 13 12 12 21 14 22 17 22 18 26 25 23 20 22 30
30. 30. @tobiasnyholm 1 3 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Start Goal Nodei Node j Dijkstra’s algorithm 3 4 5 8
31. 31. @tobiasnyholm 1 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Goal Nodei Node j Dijkstra’s algorithm Start 3 + Start 3 + Start 3 +
32. 32. @tobiasnyholm 5 1 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Goal Nodei Node j Dijkstra’s algorithm Start 4 + Start 3 + Start 3 +
33. 33. @tobiasnyholm 5 1 1 2 2 4 1 1 9 3 2 1 8 9 3 2 9 8 9 4 4 2 8 8 8 5 2 Goal Nodei Node j Dijkstra’s algorithm Start 5 + Start 3 + Start 3 +
34. 34. @tobiasnyholm Dynamic programming Let V(n) = {v1 ,v2 ,…, vn } Optimal solution O(n) = min vn + O(n-1) O(n-1) A B C D E F B C D E F A B C D E F+
35. 35. @tobiasnyholm Dynamic programming Let V(n) = {v1 ,v2 ,…, vn } Optimal solution O(n) = min vn + O(n-1) O(n-1) i j
36. 36. @tobiasnyholm Dynamic programming Let V(n) = {v1 ,v2 ,…, vn } Optimal solution O(n) = min vn + O(n-1) O(n-1) i k j
37. 37. @tobiasnyholm Pause
38. 38. @tobiasnyholm \$1.000.000
39. 39. @tobiasnyholm Time complexity O(|V| + |E|)  O(|V| + |E|)  O(|V| + |E|)  O(|V| log(|V|) + |E|)    Breadth-ﬁrst search   Depth-ﬁrst search  Greedy algorithm  Dijkstra’s algorithm    ~ O(n)  ~ O(n)  ~ O(n)  ~ O(n log n) Bellman-Held-Karp O(2n n2)
40. 40. @tobiasnyholm NP P Time complexity O(log n) O(n) O(n!) O(2n) O(n log n) O(n2) 1000 [Super large]
41. 41. “Why do we spend so much time on TSP?”
42. 42. @tobiasnyholm d H a b c
43. 43. @tobiasnyholm d H a b c
44. 44. @tobiasnyholm Let’s try TSP
45. 45. @tobiasnyholm Traveling salesman d H a b c H b a d c O(n!)
46. 46. @tobiasnyholm Traveling salesman d H a b c
47. 47. @tobiasnyholm Traveling salesman d H a b c
48. 48. @tobiasnyholm Traveling salesman d H a b c
49. 49. @tobiasnyholm Traveling salesman d H a b c
50. 50. @tobiasnyholm Traveling salesman d H a b c
51. 51. @tobiasnyholm Traveling salesman d H a b c
52. 52. @tobiasnyholm Traveling salesman d H a b c O(2n n2)
53. 53. “This is good and all, but I’m building websites” ¯_(ツ)_/¯
54. 54. @tobiasnyholm Questions? https://joind.in/talk/5dc18