|
| 1 | +# Extra Code |
| 2 | + |
| 3 | +""" |
| 4 | +
|
| 5 | +Big-O notation image generator. Code which generated the Big-O |
| 6 | +growth graph (order) in the book. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import math |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +from functools import reduce |
| 15 | + |
| 16 | +def f(t): |
| 17 | + return np.exp(-t) * np.cos(2*np.pi*t) |
| 18 | + |
| 19 | +def k(t): |
| 20 | + return [0] |
| 21 | + |
| 22 | +def l(t): |
| 23 | + return list(map(math.log, t)) |
| 24 | + |
| 25 | +def x(t): |
| 26 | + return list(map(lambda x: x, t)) |
| 27 | + |
| 28 | +def xl(t): |
| 29 | + return list(map(lambda x: math.log(x)*x, t)) |
| 30 | + |
| 31 | +def sq(t): |
| 32 | + return list(map(lambda x: x*x, t)) |
| 33 | + |
| 34 | +def p2(t): |
| 35 | + return list(map(lambda x: pow(2,x), t)) |
| 36 | + |
| 37 | +def fact(n): |
| 38 | + return reduce(lambda x,y : x*y, range(1, n+1)) |
| 39 | + |
| 40 | +def facn(t): |
| 41 | + return list(map(lambda x: fact(x), t)) |
| 42 | + |
| 43 | +t = range(1, 101) |
| 44 | + |
| 45 | +plt.axis([0, 50, 0, 50]) |
| 46 | +line1=plt.plot(t, [1]*100, '#00ee00') |
| 47 | +print(line1) |
| 48 | +plt.text(45, 1.5, r'O(1)') |
| 49 | + |
| 50 | +line2=plt.plot(t, l(t), color='#00bb00') |
| 51 | +plt.text(40, 4.8, r'O(log(n))') |
| 52 | + |
| 53 | +line3=plt.plot(t, x(t), color='#008800') |
| 54 | +plt.text(42, 40, r'O((n))') |
| 55 | + |
| 56 | +line4=plt.plot(t, xl(t), color='#eeee00') |
| 57 | +plt.text(18, 45, r'O(nlog(n))') |
| 58 | + |
| 59 | +line5=plt.plot(t, sq(t), color='#440000') |
| 60 | +plt.text(8, 44, r'O(n^2)') |
| 61 | + |
| 62 | +line6=plt.plot(t, p2(t), color='#bb0000') |
| 63 | +plt.text(2, 35, r'O(2^n)') |
| 64 | + |
| 65 | +line7=plt.plot(t, facn(t), color='#ff0000') |
| 66 | +plt.text(0, 45, r'O(n!)') |
| 67 | + |
| 68 | +plt.show() |
0 commit comments