11 Invariants
11 Invariants
11 Invariants
Vladimir Podolskii
Computer Science Department, Higher School of Economics
Outline
Invariants
More Coffee
Debugging Problem
Invariants
Invariants
More Coffee
Debugging Problem
Coffee with milk
Problem
There are two cups, one with coffee and another with milk.
We take a spoon of coffee and add it to the cup with milk.
After that we take one spoon of a drink in the second cup
and put it to the rst cup. What is larger, the amount of milk
in the coffee cup or the amount of coffee in the milk cup?
Cup 2 Cup 2
Coffee with milk
Before After
Cup 1 Cup 1
Cup 2 Cup 2
Cup 2 Cup 2
Invariants
More Coffee
Debugging Problem
More Coffee
Problem
There are two equally sized cups, one with coffee, another
with milk. Both cups are half full. We like the rst cup and
we like coffee with lots of milk: 1/3 coffee, 2/3 milk. We
can pour drinks from one cup to another back and forth.
Can we get our favorite coffee in our favorite cup? Any
amount would do, the right proportion is what matters.
We
We“coffee
“milk down”
down”drink
drinkfrom
fromCup
Cup12ininCup
Cup21
Outline
Invariants
More Coffee
Debugging Problem
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0
Pending 1
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1
Pending 1
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1
Pending 1 3
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2
Pending 1 3
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2
Pending 1 3 5
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2 3
Pending 1 3 5
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2 3
Pending 1 3 5 7
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2 3 4
Pending 1 3 5 7
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2 3 4
Pending 1 3 5 7 9
(Typical) Debugging
Problem
Bob is debugging his code. When he starts, he has only one
bug. But once he xes one bug, 3 new bugs appear. In
several hours Bob xed 15 bugs. How many pending bugs
Bob has at this point?
Fixed 0 1 2 3 4 …
Pending 1 3 5 7 9 …
(Typical) Debugging
Fixed 0 1 2 3 4 …
Pending 1 3 5 7 9 …
• #Pending = 1 + 2×#Fixed
(Typical) Debugging
Fixed 0 1 2 3 4 …
Pending 1 3 5 7 9 …
• #Pending = 1 + 2×#Fixed
Fixed 0 1 2 3 4 …
Pending 1 3 5 7 9 …
• #Pending = 1 + 2×#Fixed