Professional Documents
Culture Documents
Why Efficiency Matters: Madhavan Mukund
Why Efficiency Matters: Madhavan Mukund
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 2/5
A real world problem
Every SIM card needs to be linked to
an Aadhaar card
Validate the Aadhaar details for each
SIM card
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 2/5
A real world problem
Every SIM card needs to be linked to
an Aadhaar card for each SIM card S:
Validate the Aadhaar details for each for each Aadhaar number A:
SIM card check if Aadhaar details of S
match A
Simple nested loop
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 2/5
A real world problem
Every SIM card needs to be linked to
an Aadhaar card for each SIM card S:
Validate the Aadhaar details for each for each Aadhaar number A:
SIM card check if Aadhaar details of S
match A
Simple nested loop
How long will this take?
M SIM cards, N Aadhaar cards
Nested loops iterate M · N times
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 2/5
A real world problem
Every SIM card needs to be linked to
an Aadhaar card for each SIM card S:
Validate the Aadhaar details for each for each Aadhaar number A:
SIM card check if Aadhaar details of S
match A
Simple nested loop
How long will this take?
M SIM cards, N Aadhaar cards
Nested loops iterate M · N times
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
1011 /60 ≈ 1.67 × 109 minutes
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
1011 /60 ≈ 1.67 × 109 minutes
(1.67 × 109 )/60 ≈ 2.8 × 107 hours
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
1011 /60 ≈ 1.67 × 109 minutes
(1.67 × 109 )/60 ≈ 2.8 × 107 hours
(2.8 × 107 )/24 ≈ 1.17 × 106 days
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
1011 /60 ≈ 1.67 × 109 minutes
(1.67 × 109 )/60 ≈ 2.8 × 107 hours
(2.8 × 107 )/24 ≈ 1.17 × 106 days
(1.17 × 106 )/365 ≈ 3200 years!
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
A real world problem
Assume M = N = 109
Nested loops execute 1018 times for each SIM card S:
for each Aadhaar number A:
We calculated that Python can perform check if Aadhaar details of S
107 operations in a second match A
This will take at least 1011 seconds
1011 /60 ≈ 1.67 × 109 minutes
(1.67 × 109 )/60 ≈ 2.8 × 107 hours
(2.8 × 107 )/24 ≈ 1.17 × 106 days
(1.17 × 106 )/365 ≈ 3200 years!
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 3/5
Guess my birthday
You propose a date
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier April 16? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier April 16? Earlier
April 13? Earlier
...
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier April 16? Earlier
April 13? Earlier
...
April 12? Yes
What is the best strategy?
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier April 16? Earlier
April 13? Earlier
...
April 12? Yes
What is the best strategy?
Interval shrinks from 365 → 182 →
91 → 45 → 22 → 11 → 5 → 2 → 1
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
Guess my birthday
You propose a date Interval of possibilities
I answer, Yes, Earlier , Later Query midpoint — halves the interval
Suppose my birthday is 12 April June 30? Earlier
March 31? Later
A possible sequence of questions May 15? Earlier
September 12? Earlier April 22? Earlier
February 23? Later April 11? Later
July 2? Earlier April 16? Earlier
April 13? Earlier
...
April 12? Yes
What is the best strategy?
Interval shrinks from 365 → 182 →
91 → 45 → 22 → 11 → 5 → 2 → 1
Under 10 questions
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 4/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem
Madhavan Mukund Why Efficiency Matters PDSA using Python Week 1 5/5
A real world problem