Commit ca9835cd authored by Ben Glocker's avatar Ben Glocker
Browse files

cleared notebooks 00 and 02

parent 8c6adbca
This diff is collapsed.
This diff is collapsed.
......@@ -23,18 +23,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n",
"54.9 µs ± 10.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"outputs": [],
"source": [
"def naive_fib(n):\n",
" if n == 0:\n",
......@@ -57,18 +48,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n",
"4.13 µs ± 285 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"outputs": [],
"source": [
"def dp_fib(n):\n",
" if n == 0:\n",
......@@ -100,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"collapsed": true
},
......@@ -124,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {
"collapsed": true
},
......@@ -146,7 +128,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {
"collapsed": true
},
......@@ -167,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {
"collapsed": true
},
......@@ -188,30 +170,9 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0, 0.6281491025812429)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFQNJREFUeJzt3X2QXXd93/H3R7Jdsi4txFYIta1du1GGcaih8dakjUvp\nNG4EZUal8RDJ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ODN3bv83jVsh1jmSzIB\n/G3gzxih7TZvvWAEtlmSzUkeAJ4E/riqVmWbDVMojLLrq+qlwCuBN3UPU4yk7mnIw3HMsj/vpnMn\n4JcCXwb+62DLWbkkfxX4APArVfXt3nnDvN0WWK+R2GZV9f3u98blwHVJXjxv/oq22TCFQl+3xBhG\nVXW8+/ok8CE6h8pGyVe7x3fPHOd9csD1rJqq+mr3H+dp4D0M6bbrHpf+ADBbVR/sNg/9dltovUZl\nm51RVd8EPgVsZxW22TCFQj+3zRg6SS7uDoKR5GLgH7PoDf+G0l7gDd33bwA+MsBaVtWZf4Bdr2EI\nt1130PL3gUer6p09s4Z6u51rvUZkm21J8rzu+x+icwLO/2UVttnQnH0E0D117Hd49rYZQ//opiRX\n0dk7gM4V5n84zOuV5L8Dr6Bzx8avAv8e+DBwD7AVOAy8tqqGbsD2HOv2CjqHIQo4BPzLYbt/V5Lr\ngfuAh4DT3eZ/R+f4+9But0XWaxfDv82uoTOQvJnOf+7vqar/mOQSznObDVUoSJLaNUyHjyRJLTMU\nJEkNQ0GS1DAUJEkNQ0GS1DAUJEkNQ0GS1DAUpPOUZCLJo0ne0723/ce7V5lKQ8dQkFbHNuCOqvoJ\n4JvAzw24HmlFDAVpdTxRVQ903x8AJgZYi7RihoK0Or7b8/77dO5jJQ0dQ0GS1DAUJEkN75IqSWq4\npyBJahgKkqSGoSBJahgKkqSGoSBJahgKkqSGoSBJahgKkqTG/wfcadOMYLc2RwAAAABJRU5ErkJg\ngg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x1e64afc2eb8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib import pyplot as plt\n",
......
%% Cell type:markdown id: tags:
# CO202 - Algorithms 2
%% 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)
```
%%%% Output: stream
55
54.9 µs ± 10.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%% 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)
```
%%%% Output: stream
55
4.13 µs ± 285 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%% 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)
```
%%%% Output: execute_result
(0, 0.6281491025812429)
%%%% Output: display_data
%% 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