-
-
Save j2deme/d9cc6cdb33209277584d864a06bb0d99 to your computer and use it in GitHub Desktop.
big-o-notation graph with Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
''' | |
big-o-notation.py: | |
Version: 0.2 | |
Python 3.6 | |
Date created: 22/03/2017 | |
''' | |
# Big-O Name | |
# 1 Constant | |
# log(n) Logarithmic | |
# n Linear | |
# nlog(n) Log Linear | |
# n^2 Quadratic | |
# n^3 Cubic | |
# 2^n Exponential | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Stylesheets defined in Matplotlib | |
plt.style.use('bmh') | |
# Set up runtime comparisons | |
n = np.linspace(1, 10, 1000) | |
labels = ['Constant', 'Logarithmic', 'Linear', 'Log Linear', 'Quadratic', 'Cubic', 'Exponential'] | |
big_o = [np.ones(n.shape), np.log(n), n, n * np.log(n), n**2, n**3, 2**n] | |
# Plot setup | |
plt.figure(figsize=(12, 10)) | |
plt.ylim(0, 50) | |
for i in range(len(big_o)): | |
plt.plot(n, big_o[i], label=labels[i]) | |
plt.legend(loc=0) | |
plt.ylabel('Relative Runtime') | |
plt.xlabel('Input Size') | |
plt.savefig('big-o-notation.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment