Skip to content

Commit

Permalink
Fixed a few typos
Browse files Browse the repository at this point in the history
  • Loading branch information
jdwittenauer committed Apr 27, 2016
1 parent d96862c commit b032e24
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions notebooks/misc/DynamicProgramming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The Fibonacci problem is a good starter example but doesn't really capture the challenge of representing problems in terms of optimal sub-problems because for Fibonacci numbers the answer is pretty obvious. Let's move up one step in difficulty to a problem known as the [longest increasing subsequence](https://en.wikipedia.org/wiki/Longest_increasing_subsequence) problem. The objective is to find the longest subsequence of a given sequence such that all elements in the subsequence are sorted in increasing order. Note that the elements do not need to be contiguous; that is, they are not required to appear next to each other. For example, in the sequence [ 10, 22, 9, 33, 21, 50, 41, 60, 80 ] the longest common subsequence (LIS) is [10, 22, 33, 50, 60, 80].\n",
"The Fibonacci problem is a good starter example but doesn't really capture the challenge of representing problems in terms of optimal sub-problems because for Fibonacci numbers the answer is pretty obvious. Let's move up one step in difficulty to a problem known as the [longest increasing subsequence](https://en.wikipedia.org/wiki/Longest_increasing_subsequence) problem. The objective is to find the longest subsequence of a given sequence such that all elements in the subsequence are sorted in increasing order. Note that the elements do not need to be contiguous; that is, they are not required to appear next to each other. For example, in the sequence [ 10, 22, 9, 33, 21, 50, 41, 60, 80 ] the longest increasing subsequence (LIS) is [10, 22, 33, 50, 60, 80].\n",
"\n",
"It turns out that it's fairly difficult to do a \"brute-force\" solution to this problem. The dynamic programming solution is much more concise and a natural fit for the problem definition, so we'll skip creating an unnecessarily complicated naive solution and jump straight to the DP solution."
]
Expand Down Expand Up @@ -296,7 +296,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The intuition here is that for a given index $i$, we can compute the length of the longest common subsequence $length(i)$ by looking at all indices $j < i$ and if $length(j) + 1 > i$ and $seq[j] < seq[i]$ (meaning there's a number at position $j$ that increases the longest subsequence at that index such that it is now longer than the longest recorded subsequence at $i$) then we increase $length(i)$ by 1. It's a bit confusing at first glance but step through it carefully and convince yourself that this solution finds the optimal subsequence. The \"prev\" list holds the indices of the elements that form the actual values in the subsequence.\n",
"The intuition here is that for a given index $i$, we can compute the length of the longest increasing subsequence $length(i)$ by looking at all indices $j < i$ and if $length(j) + 1 > i$ and $seq[j] < seq[i]$ (meaning there's a number at position $j$ that increases the longest subsequence at that index such that it is now longer than the longest recorded subsequence at $i$) then we increase $length(i)$ by 1. It's a bit confusing at first glance but step through it carefully and convince yourself that this solution finds the optimal subsequence. The \"prev\" list holds the indices of the elements that form the actual values in the subsequence.\n",
"\n",
"Let's generate some test data and try it out."
]
Expand Down Expand Up @@ -393,7 +393,7 @@
"source": [
"The [knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem) is another classic dynamic programming exercise. The generalization of this problem is very old and comes in many variations, and there are actually multiple ways to tackle this problem aside from dynamic programming. Still, it's a common example for DP exercises.\n",
"\n",
"The problem at its core is one of combinatorial optimization. Given a set of items, each with a mass and a value, determine the collection of items that results in the highest possible value while not exceeding some limit on the total weight. The variation we'll look at is commonly referred to as the 0-1 knapsack problem, which restricts the number of copies of each kind of item to 0 or 1. More formally, given a set of $n$ items each with weight $w_i$ and value $w_i$ along with a maximum total weight $W$, our objective is:\n",
"The problem at its core is one of combinatorial optimization. Given a set of items, each with a mass and a value, determine the collection of items that results in the highest possible value while not exceeding some limit on the total weight. The variation we'll look at is commonly referred to as the 0-1 knapsack problem, which restricts the number of copies of each kind of item to 0 or 1. More formally, given a set of $n$ items each with weight $w_i$ and value $v_i$ along with a maximum total weight $W$, our objective is:\n",
"\n",
"$\\Large max \\Sigma v_i x_i$, where $\\Large \\Sigma w_i x_i \\leq W$\n",
"\n",
Expand All @@ -420,8 +420,8 @@
" # as the optimal solution with k-1 items for the same max weight\n",
" S[k][x] = S[k-1][x]\n",
" \n",
" # if the current item weighs less than the max weight and the optimal solution from including this item\n",
" # is better than the current optimum, the new optimum is the one resulting from including the current item\n",
" # if the current item weighs less than the max weight and the optimal solution including this item is \n",
" # better than the current optimum, the new optimum is the one resulting from including the current item\n",
" if w[k] < x and S[k-1][x-w[k]] + v[k] > S[k][x]:\n",
" S[k][x] = S[k-1][x-w[k]] + v[k]\n",
" \n",
Expand All @@ -432,7 +432,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The intuition behind this algorithm is that once you've solved for the optimal combination of items at some weight $x < W$ and with some number of items $k < n$, then it's easy to solve the problem with one more item or one higher max weight because you can just check to see if the solution obtained by incorporating that items is better than the best solution you've already found. So how do you get the initial solution? Keep going down the rabbit hole until to reach 0 (in which case the answer is 0). At first glance it's very hard to grasp, but that's part of the magic of dynamic programming. Let's run an example to see what it looks like."
"The intuition behind this algorithm is that once you've solved for the optimal combination of items at some weight $x < W$ and with some number of items $k < n$, then it's easy to solve the problem with one more item or one higher max weight because you can just check to see if the solution obtained by incorporating that item is better than the best solution you've already found. So how do you get the initial solution? Keep going down the rabbit hole until to reach 0 (in which case the answer is 0). At first glance it's very hard to grasp, but that's part of the magic of dynamic programming. Let's run an example to see what it looks like."
]
},
{
Expand Down Expand Up @@ -509,7 +509,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
"version": "2.7.11"
}
},
"nbformat": 4,
Expand Down

0 comments on commit b032e24

Please sign in to comment.