computational complexity in python
TRANSCRIPT
What is computational complexity?● It’s a theoretical branch of computer science● It’s used to determine the cost of algorithms in:
○ Instructions (time)○ Space
● Algorithms are sorted in classes (like P and NP)
Basic rules● Cut your algorithm in pieces● The piece with bigger cost wins● Discard constants● Examples:
○ O(n!) + O(4n³) + O(1) →O(n!)○ O(5n!) →O(n!)○ O(3) →O(1)○ O(n³) + O(n) →O(n³)
Python listsOperation Average Case Amortized Worst Case
Copy O(n) O(n)
Append O(1) O(1)
Insert/Delete item O(n) O(n)
Get/Set Item O(1) O(1)
Iteration O(n) O(n)
Sort O(n log n) O(n log n)
x in s O(n) O(n)
Get length O(1) O(1)
Python dictionaries
Operation Average Case Amortized Worst Case
Copy O(n) O(n)
Get Item O(1) O(n)
Set Item O(1) O(n)
Delete item O(1) O(n)
Iteration O(n) O(n)
Python sets
Operation Average Case Worst Case
x in s O(1) O(n)
union s|t O(n) O(n)
intersection s&t O(n) O(n²)
Difference s-t O(n) O(n)
Symmetric Difference s^t O(n) O(n²)
Conclusions● Use the best suited data structure for your use case● If you need to use an expensive algorithm:
○ Reduce n size first*○ Split the problem in smaller chunks (n³ > ((n/2)³ + (n/2)³)*
● Sometimes a slower algorithm is just fine:○ Know your requirements○ For small inputs it may not be worth it (premature optimization is the root
of all evil)
● Avoid expensive algorithms*
*whenever possible
Sources and more information● https://www.raywenderlich.com/79850/collection-data-structures-swift● https://www.youtube.com/watch?v=8TDZsaATmqQ● https://en.wikipedia.org/wiki/Computational_complexity_theory● https://wiki.python.org/moin/TimeComplexity● http://www.laurentluce.com/posts/python-list-implementation/● http://www.laurentluce.com/posts/python-dictionary-implementation/● https://github.com/jsoucheiron/computational-complexity-talk● http://xkcd.com/353/