Skip to content

Commit ab98b4f

Browse files
author
Hamid Gasmi
committed
DP: descriptions + related problems are added
1 parent 6329ece commit ab98b4f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@
354354
<details>
355355
<summary>Dynamic Programming</summary>
356356

357-
- It's a general algorithmic design technique: Approach can be used to solve many kinds of problems
358357
- It's Frequently used for optimization problems: finding best way to do something
359358
- It's typically used when brute-force solution is to enumerate all possibilities:
360359
- May not know which subproblems to solve, so we solve many or all!
@@ -444,7 +443,7 @@
444443
- ***D(i,j) = MIN(D(i,j-1) + 1, D(i-1,j) + 1, D(i-1,j-1)) if A[i] = B[j]***
445444
- **Reconstructing an **Optimal Alignment**:
446445
- 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**:
448447
- N items with total weight Wi (Kg) and total value Vi ($)
449448
- A Backpack with a capacity W
450449
- Each item is either taken or not
@@ -469,7 +468,28 @@
469468
- Value per Unit: Item 1: $5; Item2: $4.66; Item3: $4; Item4: $4.5
470469
- 6 ($30) + 3 ($14) = 9 items ($44)
471470
- 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 log⁡W, 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 log⁡m.
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
472488
- 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)
473493
- [Money Change II](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/13)
474494
- [Money Change III](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/168)
475495
- [Primitive Calculator](https://github.com/hamidgasmi/training.computerscience.algorithms-datastructures/issues/14)
@@ -489,6 +509,12 @@
489509
- Geeksforgeeks [Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/)
490510
- [Dynamic Programming](https://www.radford.edu/~nokie/classes/360/dynprog.html)
491511
- 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)
492518

493519
</details>
494520

0 commit comments

Comments
 (0)