how to win coding competitions: secrets of champions week ... filelecture 10: introduction to binary...
TRANSCRIPT
![Page 1: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/1.jpg)
How to Win Coding Competitions: Secrets of Champions
Week 3: Sorting and Search AlgorithmsLecture 10: Introduction to binary search
Maxim BuzdalovSaint Petersburg 2016
![Page 2: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/2.jpg)
Binary search
Recall the example from Lecture 1: how to search in sorted data
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
This is an example of binary search. We will have more in this video
2 / 6
![Page 3: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/3.jpg)
Binary search
Recall the example from Lecture 1: how to search in sorted data
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Mey
er
This is an example of binary search. We will have more in this video
2 / 6
![Page 4: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/4.jpg)
Binary search
Recall the example from Lecture 1: how to search in sorted data
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Ga
mm
a
This is an example of binary search. We will have more in this video
2 / 6
![Page 5: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/5.jpg)
Binary search
Recall the example from Lecture 1: how to search in sorted data
Ab
elso
n
Ah
o
Bro
ok
s
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
Cor
men
This is an example of binary search. We will have more in this video
2 / 6
![Page 6: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/6.jpg)
Binary search
Recall the example from Lecture 1: how to search in sorted data
Ab
elso
n
Ah
o
Bro
ok
s
Cor
men
Dij
kst
ra
Eck
el
For
isek
Ga
mm
a
Go
del
Gra
ha
m
Ho
are
Ker
nig
ha
n
Kn
uth
La
mp
ort
McC
on
nel
l
Mey
er
Nor
vig
Pa
pa
dim
itri
ou
Pa
tash
nik
Rit
chie
Ru
ssel
Set
hi
Sed
gew
ick
Sk
ien
a
Sp
ols
ky
Str
ou
stru
p
Su
ssm
an
Ta
nen
ba
um
Tu
rin
g
Wir
th
This is an example of binary search. We will have more in this video
2 / 6
![Page 7: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/7.jpg)
Binary search – Problem to solve
A very general form of binary search
I Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 8: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/8.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 9: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/9.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operation
I We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 10: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/10.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and b
I D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 11: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/11.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 12: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/12.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 13: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/13.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 14: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/14.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0
I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 15: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/15.jpg)
Binary search – Problem to solve
A very general form of binary searchI Given a function F : D → C , such that:
I There exists a totally ordered set S equipped with an average-of-two operationI We will write it Avg(a, b) for arguments a and bI Avg(a, b) should be between a and b and should not be equal to neither a nor b,
unless there is no element of S between a and bI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}
I Simply speaking, a piece of S between Dmin and Dmax
I C = {−1, 0,+1} with the following meanings:I −1: “too early”I 0: “just in time”I +1: “too late”
I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x and y as near as possible, such that F (x) = −1, F (y) = 1
3 / 6
![Page 16: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/16.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
4 / 6
![Page 17: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/17.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
![Page 18: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/18.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
![Page 19: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/19.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find where q is in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” if q = x , “−1” if x < q, “+1” if x > q
I If no such element, “0” is infeasible: then you will find where to insert q
4 / 6
![Page 20: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/20.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
![Page 21: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/21.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
![Page 22: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/22.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find first occurence of q in a sorted array
I D = [1;N]: the set of array indices, ordered naturally
I Avg(a, b) = b(a + b)/2cI F (x): “0” never, “−1” if x < q, “+1” if x ≥ q
I But first check if F (y) = q. . .
4 / 6
![Page 23: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/23.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
![Page 24: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/24.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
![Page 25: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/25.jpg)
Binary search – Problem examples
I Given a function F : D → C , such that:I There exists a totally ordered set S equipped with an Avg(a, b) operationI D is a bounded subset of S : D = {s | s ∈ S ,Dmin ≤ S ,S ≤ Dmax}I C = {−1 (“too early”), 0 (”just in time”),+1 (“too late”)}I Monotonicity: Whenever a < b, F (a) ≤ F (b)
I Need to find x ∈ D such that F (x) = 0I Or, if impossible, find x , y as near as possible, such that F (x) = −1, F (y) = 1
Example: Find a root of a monotonically growing function f
I D = [min; max]: the segment of R which we are interested in
I Avg(a, b) = (a + b)/2
I F (x): “0” if f (x) = 0, “−1” if f (x) < 0, “+1” if f (x) > 0
I Warning: you are unlikely to find the root exactly. . .
4 / 6
![Page 26: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/26.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R)if Vmin = 1 then return 〈Null, Dmin〉 end ifif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 27: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/27.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end ifif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 28: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/28.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end ifif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 29: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/29.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end ifif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 30: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/30.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end iffor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 31: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/31.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 32: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/32.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R)if M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 33: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/33.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end ifv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 34: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/34.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M)if v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 35: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/35.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end ifif v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 36: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/36.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if
end forend function
5 / 6
![Page 37: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/37.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if . “Too early”: use right part
end forend function
5 / 6
![Page 38: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/38.jpg)
Binary search: Algorithm
function BinarySearch(F , Avg, Dmin, Dmax)L← Dmin, R ← Dmax, Vmin ← F (L), Vmax ← F (R) . First evaluate endpointsif Vmin = 1 then return 〈Null, Dmin〉 end if . If true, no zeros at allif Vmax = −1 then return 〈Dmax,Null〉 end if . If true, no zeros at allif Vmin = 0 then return 〈Dmin, Dmin〉 end if . If true, zero is foundif Vmax = 0 then return 〈Dmax, Dmax〉 end if . If true, zero is foundfor ever do . Invariant: F (L) = −1, F (R) = 1
M ← Avg(L,R) . Getting new query pointif M = L or M = R then return 〈L,R〉 end if . (L,R) empty → no zerosv ← F (M) . Evaluating Mif v = 0 then return 〈M,M〉 end if . Direct hit!if v = −1 then L← M else R ← M end if . “Too early”: use right part
end for . “Too late”: use left partend function
5 / 6
![Page 39: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/39.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 40: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/40.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 41: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/41.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 42: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/42.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 43: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/43.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 44: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/44.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 45: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/45.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of Avg
I If Avg is a “real” average, the running time is O(logD)I the size of [L;R) range is divided by two on every iteration
6 / 6
![Page 46: How to Win Coding Competitions: Secrets of Champions Week ... fileLecture 10: Introduction to binary search Maxim Buzdalov Saint Petersburg 2016. Binary search Recall the example from](https://reader030.vdocument.in/reader030/viewer/2022040711/5e120964be32a637fa22e088/html5/thumbnails/46.jpg)
Binary search: Properties
Termination
I Guaranteed to terminate if D is finite
I Proof: [L,R] shrinks at least by one item on every iteration
Correctness
I Loop invariant: F (L) = −1, F (R) = 1
I Zeros are always between → will be found if exist
I Nonexisting zeros: will report a point where −1 switches to 1
Running time
I Strongly depends on properties of AvgI If Avg is a “real” average, the running time is O(logD)
I the size of [L;R) range is divided by two on every iteration
6 / 6