0% found this document useful (0 votes)
25 views15 pages

B Search

The document discusses how binary search can be used to find values in functions rather than just arrays. It explains that an array can be thought of as a function, and as long as the function is monotonic, binary search can be applied. A monotonic function is one where the outputs preserve or reverse the order of the inputs. This allows problems like finding the minimum value that satisfies a predicate to be solved using binary search. Predicates must have the property that satisfying the predicate for one value implies satisfying it for all larger values.

Uploaded by

Madhav Thakker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

B Search

The document discusses how binary search can be used to find values in functions rather than just arrays. It explains that an array can be thought of as a function, and as long as the function is monotonic, binary search can be applied. A monotonic function is one where the outputs preserve or reverse the order of the inputs. This allows problems like finding the minimum value that satisfies a predicate to be solved using binary search. Predicates must have the property that satisfying the predicate for one value implies satisfying it for all larger values.

Uploaded by

Madhav Thakker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CODECLASS [49] - ADVANCED

BINARY SEARCH
CODECLASS[49]

FINDING VALUE IN SORTED SEQUENCE


‣ We know that given a sorted array, we can find an element
in O(log N) time using binary search.

‣ We can also find the smallest number larger than X or


largest number smaller than X (in case the question
doesn’t ask for exact X but closest larger or smaller
number)

‣ Let’s look further into what we’re doing when we do these


things.
CODECLASS[49]

BINARY SEARCH ON AN IMPLICIT ARRAY

▸ Let’s say we have an array which is defined as a function

▸ Arr[i] = F(i)

▸ F is defined as F(x) = 5*x + 1

▸ So Arr = [……,-4,1,6,11,………]

▸ Can we use binary search to find if 101 is a member of this


array, and if so, then at what index?
WE CAN!
CODECLASS[49]

WHAT’S THE ADVANTAGE?

▸ We could’ve easily solved the linear equation for the value


of x s.t. F(x) = 2*x + 1 = 101

▸ But can we solve it if we are given a more complex


function

▸ Let’s say F(x) = 12x5 + 5x3 + 2x + 7

▸ A lot harder to find F(x) = some number.

▸ Binary search might work??


ARRAY CAN BE THOUGHT OF AS A FUNCTION
Array = [1,3,55,92,189]

is equivalent to

F(0) = 1

F(1) = 3

F(2) = 55

F(3) = 92

F(4) = 189

F(X) not defined for any other value of X


WHAT DOES
BINARY SEARCH
REQUIRE?
CODECLASS[49]

MONOTONIC FUNCTION
▸ A monotonic function is a function between ordered sets that
preserves or reverses the given order.

▸ In other words, if there’s a function which takes integers (domain


of function) to integers (range of function), then it’s monotonic in
two cases-

▸ f(x) > f(y) iff x > y (increasing monotonic)

▸ f(x) < f(y) iff x > y (decreasing monotonic)

▸ If a function is monotonic, is that a sufficient condition for binary


search?
CODECLASS[49]

A PROBLEM
▸ FairWorkload Problem - Given an array of workloads, split it among ‘k’
workers, such that the maximum work that any worker has to do is
minimised (can’t change order of workloads). Print as answer that 

maximum work.

▸ Eg. 10 20 30 40 50 60 70 80 90

Solution : 10 20 30 40 50 | 60 70 | 80 90 

First worker - 150, 

Second worker - 130,

Third worker - 170

▸ Is it possible to partition workload in a way that the highest workload of



any worker is less than 170? Hence, answer is 170.
CODECLASS[49]

A SIMPLER PROBLEM
▸ FairWorkLoad simplified- Given an array of workloads, split it among ‘k’
workers, such that the maximum work that any worker has to do is less 

than K. If split is possible, print answer as true, else print false.

▸ Eg. 10 20 30 40 50 60 70 80 90, 

K = 170

Solution : 10 20 30 40 50 | 60 70 | 80 90 

First worker - 150, 

Second worker - 130,

Third worker - 170
SOLVE ORIGINAL PROBLEM USING 


SOLUTION TO SIMPLER PROBLEM 

+
BINARY SEARCH
CODECLASS[49]

LET’S FORMALISE THINGS - PREDICATE


▸ Consider a predicate p defined over some ordered set S (the search space). The
search space consists of candidate solutions to the problem. In our case, a
predicate is a function which returns a boolean value, true or false. We use the
predicate to verify if a candidate solution is legal (does not violate some
constraint) according to the definition of the problem.

▸ Binary search can be used if and only if for all x in S, p(x) implies p(y) for all y > x.
This property is what we use when we discard the second half of the search
space.

▸ It is equivalent to saying that ¬p(x) implies ¬p(y) for all y < x (the symbol ¬
denotes the logical not operator), which is what we use when we discard the
first half of the search space.

▸ Think of this in terms of monotonic function.


CODECLASS[49]

▸ Behind the cryptic mathematics I am really stating that if you had a yes or no
question (the predicate), getting a yes answer for some potential solution x means
that you’d also get a yes answer for any element after x. Similarly, if you got a no
answer, you’d get a no answer for any element before x. As a consequence, if you
were to ask the question for each element in the search space (in order), you
would get a series of no answers followed by a series of yes answers.

▸ Careful readers may note that binary search can also be used when a predicate
yields a series of yes answers followed by a series of no answers. This is true and
complementing that predicate will satisfy the original condition. For simplicity we’ll
deal only with predicates described in the theorem.

▸ We can use binary search to find the smallest legal solution, i.e. the smallest x
for which p(x) is true.
CODECLASS[49]

Predicate example
DISCUSSION,
SUMMARY AND

PRACTICE QUESTIONS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy