Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dynamic programming 101

111 views

Published on

Dynamic programming 101 for PHP Serbia 2018

Published in: Technology
  • 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 • Certified 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 Backup-manager/symfony php-http/httplug-bundle php-http/multipart-stream php-http/discovery happyr/normal-distribution-bundle nyholm/effective-interest-rate MailgunBundle league/geotools
  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 fib(2) + fib(1) fib(1) + fib(0) fib(1) + fib(0) Fibonacci fib(5) fib(4) + fib(3) fib(3) + fib(2) fib(2) + fib(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 fib(2) + fib(1) fib(1) + fib(0) fib(1) + fib(0) Fibonacci fib(5) fib(4) + fib(3) fib(3) + fib(2) fib(2) + fib(1)
  15. 15. @tobiasnyholm fib(0) Fibonacci fib(5) fib(4) + fib(3) fib(2) fib(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 11 6 5 A 3 7 8 8 5 5 9 7 9 7
  24. 24. @tobiasnyholm
  25. 25. @tobiasnyholm Breadth first C E K H F G J D I L A B
  26. 26. @tobiasnyholm <?php class Graph { private $edges = []; public function addEdge(string $from, string $to) { if (!isset($this->edges[$from])) { $this->edges[$from] = []; } $this->edges[$from][] = $to; } public function bfs(string $startNode, string $goalNode) { $queue = [$startNode]; $visited = [$startNode => true]; while (!empty($queue)) { $current = array_shift($queue); if (!isset($this->edges[$current])) { continue; // If no edges } foreach ($this->edges[$current] as $to) { $queue[] = $to; if (!isset($visited[$to])) { $visited[$to] = $current; // First time }
  27. 27. @tobiasnyholm Breadth first > php bfs.php
 
 B - L - H - C - A - C E K H F G J D I L A B
  28. 28. @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
  29. 29. @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
  30. 30. @tobiasnyholm Breadth first 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
  31. 31. @tobiasnyholm Depth first 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
  32. 32. @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
  33. 33. @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
  34. 34. @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
  35. 35. @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 +
  36. 36. @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 +
  37. 37. @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 +
  38. 38. @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+
  39. 39. @tobiasnyholm Dynamic programming Let V(n) = {v1 ,v2 ,…, vn } Optimal solution O(n) = min vn + O(n-1) O(n-1) i j
  40. 40. @tobiasnyholm Dynamic programming Let V(n) = {v1 ,v2 ,…, vn } Optimal solution O(n) = min vn + O(n-1) O(n-1) i k j
  41. 41. @tobiasnyholm Pause
  42. 42. @tobiasnyholm $1.000.000
  43. 43. @tobiasnyholm Time complexity O(|V| + |E|)
 O(|V| + |E|)
 O(|V| + |E|)
 O(|V| log(|V|) + |E|)
 
 Breadth-first search 
 Depth-first search
 Greedy algorithm
 Dijkstra’s algorithm
 
 ~ O(n)
 ~ O(n)
 ~ O(n)
 ~ O(n log n) Bellman-Held-Karp O(2n n2)
  44. 44. @tobiasnyholm NP P Time complexity O(log n) O(n) O(n!) O(2n) O(n log n) O(n2) 1000 [Super large]
  45. 45. @tobiasnyholm Traveling salesman • Isn’t traveling salesman just a edge case of Dijkstra’s algorithm for shortest path? • Could we use Dijkstra’s algoritm and require all length of the solution? • If we do not allow duplicates? i k j
  46. 46. @tobiasnyholm d H a b c
  47. 47. @tobiasnyholm d H a b c
  48. 48. “Why do we spend so much time on TSP?”
  49. 49. @tobiasnyholm Let’s try TSP
  50. 50. @tobiasnyholm Traveling salesman d H a b c H b a d c O(n!)
  51. 51. @tobiasnyholm Traveling salesman d H a b c
  52. 52. @tobiasnyholm Traveling salesman d H a b c
  53. 53. @tobiasnyholm Traveling salesman d H a b c
  54. 54. @tobiasnyholm Traveling salesman d H a b c
  55. 55. @tobiasnyholm Traveling salesman d H a b c
  56. 56. @tobiasnyholm Traveling salesman d H a b c
  57. 57. @tobiasnyholm Traveling salesman d H a b c O(2n n2)
  58. 58. “This is good and all, but I’m building websites” ¯_(ツ)_/¯
  59. 59. @tobiasnyholm Questions? https://joind.in/talk/566f7

×