SlideShare a Scribd company logo
1 of 23
Download to read offline
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 lists
Operation 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
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/

More Related Content

What's hot (20)

Files and streams
Files and streamsFiles and streams
Files and streams
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Combinational Circuits & Sequential Circuits
Combinational Circuits & Sequential CircuitsCombinational Circuits & Sequential Circuits
Combinational Circuits & Sequential Circuits
 
Switch case in C++
Switch case in C++Switch case in C++
Switch case in C++
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
 
ADA_2_Analysis of Algorithms
ADA_2_Analysis of AlgorithmsADA_2_Analysis of Algorithms
ADA_2_Analysis of Algorithms
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Python GUI
Python GUIPython GUI
Python GUI
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Modular programming
Modular programmingModular programming
Modular programming
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
 
basic logic gates
 basic logic gates basic logic gates
basic logic gates
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
13 Boolean Algebra
13 Boolean Algebra13 Boolean Algebra
13 Boolean Algebra
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 

Similar to Computational complexity in python

Data Structures and Algorithms - Lec 02.pdf
Data Structures and Algorithms - Lec 02.pdfData Structures and Algorithms - Lec 02.pdf
Data Structures and Algorithms - Lec 02.pdfRameshaFernando2
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsAsen Bozhilov
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...Joe Andelija
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Spacegueste5dc45
 
GDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision MakingGDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision MakingGDSCSSN
 
Neural Processes
Neural ProcessesNeural Processes
Neural ProcessesSangwoo Mo
 
Design and analysis of algorithm
Design and analysis of algorithmDesign and analysis of algorithm
Design and analysis of algorithmVarun Ojha
 
Python in education
Python in education Python in education
Python in education pyconfi
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1SSE_AndyLi
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Algorithms__Data_Structures_-_iCSC_2018.pptx
Algorithms__Data_Structures_-_iCSC_2018.pptxAlgorithms__Data_Structures_-_iCSC_2018.pptx
Algorithms__Data_Structures_-_iCSC_2018.pptxNurBudiNugroho
 

Similar to Computational complexity in python (19)

Big o
Big oBig o
Big o
 
Data Structures and Algorithms - Lec 02.pdf
Data Structures and Algorithms - Lec 02.pdfData Structures and Algorithms - Lec 02.pdf
Data Structures and Algorithms - Lec 02.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
 
GDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision MakingGDSC SSN - solution Challenge : Fundamentals of Decision Making
GDSC SSN - solution Challenge : Fundamentals of Decision Making
 
Neural Processes
Neural ProcessesNeural Processes
Neural Processes
 
Design and analysis of algorithm
Design and analysis of algorithmDesign and analysis of algorithm
Design and analysis of algorithm
 
Python in education
Python in education Python in education
Python in education
 
The bog oh notation
The bog oh notationThe bog oh notation
The bog oh notation
 
Complexity.pdf
Complexity.pdfComplexity.pdf
Complexity.pdf
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
Randomization
RandomizationRandomization
Randomization
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
Algorithms__Data_Structures_-_iCSC_2018.pptx
Algorithms__Data_Structures_-_iCSC_2018.pptxAlgorithms__Data_Structures_-_iCSC_2018.pptx
Algorithms__Data_Structures_-_iCSC_2018.pptx
 

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 

Computational complexity in python

  • 1. Computational complexity in Python Jordi Soucheiron (@jordixou) @ pybcn 17/03/2016
  • 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)
  • 4. 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³)
  • 5. Is it a permutation?
  • 6. Is it a permutation? O(n*n!)
  • 7.
  • 8. Is it a permutation?
  • 9. Is it a permutation? O(n²)
  • 10.
  • 11. Is it a permutation?
  • 12. Is it a permutation? O(n log n)
  • 13. Is it a permutation?
  • 14. Is it a permutation? O(n)
  • 15. Does it really matter?
  • 17. Python lists Operation 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)
  • 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
  • 23. 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/