-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBig-Oh.py
137 lines (119 loc) · 3.89 KB
/
Big-Oh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Asymptotic Analysis - Find Big Oh
# Made by : Kevin Wijaya
# -------------------------------
# Orders of common functions
# -------------------------------
# O(1) : constant function
# O(log n) : logarithmic function
# O(n) : linear function
# O(n^2) : quadratic function
# O(2^n) : exponential function
# O(n^k) : polynomial function
# O(n!) : factorial function
import matplotlib.pyplot as plt
import math
class big_oh:
def __init__(self, n=0):
self.name = '' # name fucntion (ex: constant function)
self.function = '' # expression (f(n) = ?)
self.size = [] # input (n) ---> [1, 2 , 3, ..., n]
self.time = [] # itertaion (N) ---> [y1, y2, y3, , ..., yn]
self.n = n
def plot(self):
plt.figure(figsize=(5, 5))
plt.plot(self.size, self.time)
plt.xlabel('n - size / input')
plt.ylabel('N - time / iteration')
plt.title(self.name)
plt.xlim(0,100)
plt.ylim(0,100)
plt.show()
def plot_all(self, temp_list):
plt.xlabel('n - size / input')
plt.ylabel('N - time / iteration')
plt.title('All type')
for i in temp_list:
plt.plot(i.size, i.time, label=i.name)
plt.xlim(0,100)
plt.ylim(0,100)
plt.legend(loc='upper right', frameon=False)
plt.show()
def convert_string(self, string):
string = string
string = string.replace('log', ' math.log')
string = string.replace('^', '**')
if '!' in list(string):
i = list(string).index('!')
n = (list(string)[0:i])
n = ''.join(n)
string = string.replace('!', '')
string = string.replace(n, f' math.factorial({n}) ')
return string
def function_to_time(self):
temp_list = []
for i in self.size:
expression = self.function.replace('n', str(i))
try:
result = eval(self.convert_string(expression))
except Exception as e:
result = None
temp_list.append(result)
self.time = temp_list
def constant(self, n):
self.name = 'Constant Function - O(1)'
self.function = '1'
self.size = [*range(n)]
self.time = [i for s in [[int(self.function)]*(len(self.size))] for i in s]
def logarithmic(self, n):
self.name = 'Logarithmic Function - O(log n)'
self.function = 'log(n,2) + 2'
self.size = [*range(n)]
self.function_to_time()
def linear(self, n):
self.name = 'Linear Function - O(n)'
self.function = 'n + 2'
self.size = [*range(n)]
self.function_to_time()
def quadratic(self, n):
self.name = 'Quadratic Function - O(n^2)'
self.function = 'n^2 + 2'
self.size = [*range(n)]
self.function_to_time()
def polynomial(self, n, k=3):
self.name = 'Polynomial Function - O(n^k) {k=3}'
self.function = 'n^k + 2'.replace('k', str(k))
self.size = [*range(n)]
self.function_to_time()
def exponential(self, n):
self.name = 'Exponential Function - O(2^n)'
self.function = '2^n + 2'
self.size = [*range(n)]
self.function_to_time()
def factorial(self, n):
self.name = 'Factorial Function - O(n!)'
self.function = 'n! + 2'
self.size = [*range(n)]
self.function_to_time()
# -------------------------------
# Just to speed up the plot
# -------------------------------
class start:
def __init__(self, option=1):
self.option = option
def main(self):
n = 100
list_all_type = ['constant', 'logarithmic', 'linear', 'quadratic', 'exponential', 'polynomial', 'factorial']
if self.option == 1: # show all type in 1 plot
list_o = []
for i in list_all_type:
o = big_oh(n)
eval(f'o.{i}(n)')
list_o.append(o)
big_oh().plot_all(list_o)
else: # show all type one by one
for i in list_all_type:
o = big_oh(n)
eval(f'o.{i}(n)')
o.plot()
if __name__ == '__main__':
start().main()