Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
big-o-notation graph with Python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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'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.ylabel('Relative Runtime')
plt.xlabel('Input Size')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment