|
354 | 354 | <details>
|
355 | 355 | <summary>Dynamic Programming</summary>
|
356 | 356 |
|
357 |
| -- It's a general algorithmic design technique: Approach can be used to solve many kinds of problems |
358 | 357 | - It's Frequently used for optimization problems: finding best way to do something
|
359 | 358 | - It's typically used when brute-force solution is to enumerate all possibilities:
|
360 | 359 | - May not know which subproblems to solve, so we solve many or all!
|
|
444 | 443 | - ***D(i,j) = MIN(D(i,j-1) + 1, D(i-1,j) + 1, D(i-1,j-1)) if A[i] = B[j]***
|
445 | 444 | - **Reconstructing an **Optimal Alignment**:
|
446 | 445 | - It could be done by backtracking pointers that are stored in the edit distance computation matrix
|
447 |
| -- E.g., Discrete Knapsack problem |
| 446 | +- **Discrete Knapsack problem**: |
448 | 447 | - N items with total weight Wi (Kg) and total value Vi ($)
|
449 | 448 | - A Backpack with a capacity W
|
450 | 449 | - Each item is either taken or not
|
|
469 | 468 | - Value per Unit: Item 1: $5; Item2: $4.66; Item3: $4; Item4: $4.5
|
470 | 469 | - 6 ($30) + 3 ($14) = 9 items ($44)
|
471 | 470 | - taking an element of maximum value per unit of weight is not safe!
|
| 471 | + - Running time: O(nW) |
| 472 | + - It's called pseudo polynomial, but not just polynomial |
| 473 | + - The catch is that the input size is proportional to logW, rather than W |
| 474 | + - To further illustrate this, consider the following two scenarios: |
| 475 | + - 1. The input consists of m objects (say, integers) |
| 476 | + 2. The input is an integer m |
| 477 | + |
| 478 | + They look similar, but there is a dramatic difference. |
| 479 | + Assume that we have an algorithm that loops for m iterations. Then, in the 1st. case it is a polynomial time algorithm (in fact, even linear time), whereas in the 2nd. case it's an exponential time algorithm. |
| 480 | + This is because we always measure the running time in terms of the input size. In the 1st. case the input size is proportional to m, but in the 2nd. case it's proportional to logm. |
| 481 | + Indeed, a file containing just a number “100000” occupies about 7 bytes on your disc while a file containing a sequence of 100000 zeroes (separated by spaces) occupies about 200000 bytes (or 200 KB). Hence, in the 1st. case the running time of the algorithm is O(size), whereas in the 2nd. case the running time is O(2size). |
| 482 | + -Let’s also consider the same issue from a slightly different angle: |
| 483 | + - Assume that we have a file containing a single integer 74145970345617824751. |
| 484 | + If we treat it as a sequence of m=20 digits, then an algorithm working in time O(m) will be extremely fast in practice. |
| 485 | + If, on the other hand, we treat it as an integer m=74145970345617824751, then an algorithm making m iterations will work for: |
| 486 | + 74145970345617824751109/(10^9 * 60 * 60 * 24 * 365) ≈ 2351 years |
| 487 | + assuming that the underlying machine performs 10^9 operations per second |
472 | 488 | - Related Problems:
|
| 489 | + - Interactive Puzzle: [Number of Paths](http://dm.compsciclub.ru/app/quiz-number-of-paths) |
| 490 | + - Interactive Puzzle: [Two Rocks Game](http://dm.compsciclub.ru/app/quiz-take-the-last-stone) |
| 491 | + - Interactive Puzzle: [Three Rocks Game](http://dm.compsciclub.ru/app/quiz-three-rocks-game) |
| 492 | + - Interactive Puzzle: [Primitive Calculator](http://dm.compsciclub.ru/app/quiz-three-rocks-game) |
473 | 493 | - [Money Change II](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/13)
|
474 | 494 | - [Money Change III](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/168)
|
475 | 495 | - [Primitive Calculator](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/14)
|
|
489 | 509 | - Geeksforgeeks [Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/)
|
490 | 510 | - [Dynamic Programming](https://www.radford.edu/~nokie/classes/360/dynprog.html)
|
491 | 511 | - Leetcode Post: [Dynamic Programming Patterns](https://en.wikipedia.org/wiki/Counting_sort)
|
| 512 | +- For more details: |
| 513 | + - Visualization: [DP Making Change](https://www.cs.usfca.edu/~galles/visualization/DPChange.html) |
| 514 | + - Visualization: [Edit Distance calculator](http://www.let.rug.nl/kleiweg/lev/) |
| 515 | + - Visualization: [DP Longest Common Subsequence](https://www.cs.usfca.edu/~galles/visualization/DPLCS.html) |
| 516 | + - [DP for competitive Programmer's Core Skills](https://www.dropbox.com/s/qxzh146jd72188d/dynprog.pdf?dl=0) |
| 517 | + - [Why is the knapsack problem pseudo-polynomial?](https://stackoverflow.com/questions/4538581/why-is-the-knapsack-problem-pseudo-polynomial#answer-4538668) |
492 | 518 |
|
493 | 519 | </details>
|
494 | 520 |
|
|
0 commit comments