Skip to content

Instantly share code, notes, and snippets.

@enkerewpo
Last active February 28, 2025 02:45
Show Gist options
  • Save enkerewpo/9d492a2acc7224fd0718845984d118e8 to your computer and use it in GitHub Desktop.
Save enkerewpo/9d492a2acc7224fd0718845984d118e8 to your computer and use it in GitHub Desktop.
Algorithm Design and Analysis Function Graph - wheatfox 2025.2
# wheatfox 2025.2
# this script is for teaching purpose only
from matplotlib import pyplot as plt
import numpy as np
colors = plt.cm.tab10.colors
def gen(x):
x_prime = np.floor(np.log2(x))
for i in range(len(x_prime)):
x_prime[i] = np.math.factorial(int(x_prime[i]))
return x_prime
def gen2(x):
g1 = x**2
g2 = 2**(0.5*np.log2(x))
g3 = gen(x)
g4 = 2**(np.sqrt(2*np.log2(x)))
g5 = np.log2(x)**2
g6 = g5 / g4
return g1, g2, g3, g4, g5, g6
fig, axs = plt.subplots(2, 2)
plt.rcParams["text.usetex"] = True
plt.rcParams["font.family"] = "Arial"
plt.rcParams["font.serif"] = "Arial"
plt.rcParams["font.sans-serif"] = "Arial"
plt.rcParams["font.size"] = 10
x = np.arange(1, 1000, 1)
g1, g2, g3, g4, g5, _ = gen2(x)
axs[0, 0].plot(x, g1, label=r'$n^2$', color=colors[0])
axs[0, 0].plot(x, g2, label=r'$\left(\sqrt{2}\right)^{\log n}$', color=colors[1])
axs[0, 0].plot(x, g3, label=r'$\left(\log n\right)!$', color=colors[2])
axs[0, 0].plot(x, g4, label=r'$2^{\sqrt{2\log n}}$', color=colors[3])
axs[0, 0].plot(x, g5, label=r'$\log^2 n$', color=colors[4])
axs[0, 0].legend()
x = np.arange(1, 100000, 1)
g1, g2, g3, g4, g5, _ = gen2(x)
axs[0, 1].plot(x, g3, label=r'$\left(\log n\right)!$', color=colors[2])
axs[0, 1].plot(x, g1, label=r'$n^2$', color=colors[0])
axs[0, 1].legend()
axs[1, 0].plot(x, g1, label=r'$n^2$', color=colors[0])
axs[1, 0].plot(x, g2, label=r'$\left(\sqrt{2}\right)^{\log n}$', color=colors[1])
axs[1, 0].legend()
axs[1, 1].plot(x, g2, label=r'$\left(\sqrt{2}\right)^{\log n}$', color=colors[1])
axs[1, 1].plot(x, g4, label=r'$2^{\sqrt{2\log n}}$', color=colors[3])
axs[1, 1].plot(x, g5, label=r'$\log^2 n$', color=colors[4])
axs[1, 1].legend()
fig.tight_layout()
fig.set_size_inches(10.24, 7.68)
plt.savefig('graph.png', dpi=300)
fig2 = plt.figure()
x = np.arange(10000000, 1000000000, 10000)
_, _, _, _, _, g6 = gen2(x)
fig2.tight_layout()
plt.plot(x, g6, label=r'$\frac{\log^2 n}{2^{\sqrt{2\log n}}}$', color=colors[5])
plt.legend()
plt.savefig('graph2.png', dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment