Algorithm complexity can be a difficult concept to grasp, even presented with compelling mathematical arguments. This article presents a tiny Python program that shows the relative complexity of several typical functions. It can be easily adapted to other functions.
Complexity. Why it matters?
Computational complexity is a venerable subject in Computer Science. It can be defined as the amount of time and space that an algorithm needs to solve an instance of a problem.
The underpinnings of computational complexity are mathematical, but its implications very practical. There are problems that are “intractable”. They are not impossible (i.e. undecidable) but no efficient algorithm is known for them. That is: they are very difficult to solve with today technology, and even with the foreseeable one.
Usually, worst-case is the best
The most popular analysis in computational complexity is the worst-case scenario. Despite its pessimism, it is very reasonable: the size of the problems willing to be solved increases as time goes. We want to process petabytes of data, instead of megabytes. So, size is an all-important factor in algorithm complexity.
Consider the input size as the independent variable, and the growth rate is the dependent variable, and try to analyze its performance as the input size grows to infinity. This analysis is called big-Oh and has many rules that you can consult in any good algorithmic textbook. The most important one is that constants do not affect algorithmic performance for large inputs. The reason, again, is that the size of the input is the most important factor and constants do not depend on the input size.
Comparing function growths in Python
Newcomers to the theory of computation often get confused by the fact that exponential functions like are worse than polynomial functions like, say . This is clear from the mathematical definition of the Big-Oh function, but it is not easy to see unless we think that we account for very large .
The following Python code visualizes the growth as the problem instance (N) increases of several functions: . Note that is considered a bad performance as it takes operations to process 1000 inputs. In general, is considered bad for k>=2.
The code uses the libraries NumPy and MatPlotLib and employs a functional programming technique called currying to compute for constant k. It is easy to compute other functions by modifying the list FUNCTIONS.
Code : Python code explaining asymptotic behaviour of several functions.
The results are not surprising: the exponential function has the worst performance, as it grows very quick given the input size. For N=20, the other functions are insignificant compared with the exponential.
The logarithm is shown in cyan, the polynomials in blue, magenta and yellow and the exponential in red.
- Python | Visualizing image in different color spaces
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- gcd() in Python
- bin() in Python
- max() and min() in Python
- try and except in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.