23
Computational complexity in Python Jordi Soucheiron (@jordixou) @ pybcn 17/03/2016

Computational complexity in python

Embed Size (px)

Citation preview

Computational complexity in Python

Jordi Soucheiron (@jordixou) @ pybcn 17/03/2016

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)

The big O

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³)

Is it a permutation?

Is it a permutation?

O(n*n!)

Is it a permutation?

Is it a permutation?

O(n²)

Is it a permutation?

Is it a permutation?

O(n log n)

Is it a permutation?

Is it a permutation?

O(n)

Does it really matter?

Python lists

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

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

Questions