Commit 8f48c8b2 authored by Ben Glocker's avatar Ben Glocker
Browse files

added dynamic programming notebook

parent 9474c0be
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CO202 - Software Engineering - Algorithms"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tutorial on Dynamic Programming: Fibonacci"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Naive Divide-and-Conquer Approach"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def naive_fib(n):\n",
" if n == 0:\n",
" return 0\n",
" elif n == 1:\n",
" return 1\n",
" else:\n",
" return naive_fib(n-1) + naive_fib(n-2)\n",
" \n",
"print(naive_fib(10))\n",
"%timeit naive_fib(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bottom-up Dynamic Programming Approach"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dp_fib(n):\n",
" if n == 0:\n",
" return 0\n",
" f = [0]*(n+1)\n",
" f[0] = 0\n",
" f[1] = 1\n",
" for i in range(2,n+1):\n",
" f[i] = f[i-1] + f[i-2]\n",
" return f[n]\n",
"\n",
"print(dp_fib(10))\n",
"%timeit dp_fib(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparison"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Timer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"def time_f(f):\n",
" before = time.clock()\n",
" f()\n",
" after = time.clock()\n",
" return after - before"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Test data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = range(1,20)\n",
"#data = range(1,30)\n",
"#data = range(1,1000,10)\n",
"#data = range(1,10000,100)\n",
"#data = range(1,100000,1000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Running Divide-and-Conquer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tdc = []\n",
"\n",
"for i in data:\n",
" tdc.append(time_f(lambda: naive_fib(i)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Running Dynamic Programming"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tdp = []\n",
"\n",
"for i in data:\n",
" tdp.append(time_f(lambda: dp_fib(i)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Plot DC vs DP"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib import pyplot as plt\n",
"\n",
"plt.scatter(data, tdc, c='red')\n",
"plt.scatter(data, tdp, c='blue')\n",
"plt.xlabel('n')\n",
"plt.ylabel('time (/s)')\n",
"plt.xlim(0)\n",
"plt.ylim(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
%% Cell type:markdown id: tags:
# CO202 - Software Engineering - Algorithms
%% Cell type:markdown id: tags:
## Tutorial on Dynamic Programming: Fibonacci
%% Cell type:markdown id: tags:
### Naive Divide-and-Conquer Approach
%% Cell type:code id: tags:
``` python
def naive_fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return naive_fib(n-1) + naive_fib(n-2)
print(naive_fib(10))
%timeit naive_fib(10)
```
%% Cell type:markdown id: tags:
### Bottom-up Dynamic Programming Approach
%% Cell type:code id: tags:
``` python
def dp_fib(n):
if n == 0:
return 0
f = [0]*(n+1)
f[0] = 0
f[1] = 1
for i in range(2,n+1):
f[i] = f[i-1] + f[i-2]
return f[n]
print(dp_fib(10))
%timeit dp_fib(10)
```
%% Cell type:markdown id: tags:
### Comparison
%% Cell type:markdown id: tags:
#### Timer
%% Cell type:code id: tags:
``` python
import time
def time_f(f):
before = time.clock()
f()
after = time.clock()
return after - before
```
%% Cell type:markdown id: tags:
#### Test data
%% Cell type:code id: tags:
``` python
data = range(1,20)
#data = range(1,30)
#data = range(1,1000,10)
#data = range(1,10000,100)
#data = range(1,100000,1000)
```
%% Cell type:markdown id: tags:
#### Running Divide-and-Conquer
%% Cell type:code id: tags:
``` python
tdc = []
for i in data:
tdc.append(time_f(lambda: naive_fib(i)))
```
%% Cell type:markdown id: tags:
#### Running Dynamic Programming
%% Cell type:code id: tags:
``` python
tdp = []
for i in data:
tdp.append(time_f(lambda: dp_fib(i)))
```
%% Cell type:markdown id: tags:
#### Plot DC vs DP
%% Cell type:code id: tags:
``` python
%matplotlib inline
from matplotlib import pyplot as plt
plt.scatter(data, tdc, c='red')
plt.scatter(data, tdp, c='blue')
plt.xlabel('n')
plt.ylabel('time (/s)')
plt.xlim(0)
plt.ylim(0)
```
%% Cell type:code id: tags:
``` python
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment