2. 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)
19. 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)
20. 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²)
21. 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