Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam - - PowerPoint PPT Presentation

clever uses of binary search
SMART_READER_LITE
LIVE PREVIEW

Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam - - PowerPoint PPT Presentation

Clever Uses of Binary Search Section 3.3 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 23, 2015 Guessing games Im thinking of a number between 1 and 100 Im thinking of a last name of a JMU


slide-1
SLIDE 1

Clever Uses of Binary Search

Section 3.3

  • Dr. Mayfield and Dr. Lam

Department of Computer Science James Madison University

Oct 23, 2015

slide-2
SLIDE 2

Guessing games

◮ I’m thinking of a number between 1 and 100 ◮ I’m thinking of a last name of a JMU student

Oct 23, 2015 Clever Uses of Binary Search 2 of 9

slide-3
SLIDE 3

STL implementation

#include <algorithm> typedef T int; // or string, whatever T value; bool found; vector<T> v; ... sort(v.begin(), v.end()); // don't forget to sort! found = binary_search(v.begin(), v.end(), value);

http://www.cplusplus.com/reference/algorithm/binary search/

Oct 23, 2015 Clever Uses of Binary Search 3 of 9

slide-4
SLIDE 4

Java implementation

import java.util.*; T value; int index; List<T> v; ... Collections.sort(v); // don't forget to sort! index = Collections.binarySearch(v, value); // returns a negative value if not found

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

Oct 23, 2015 Clever Uses of Binary Search 4 of 9

slide-5
SLIDE 5

Iterative solutions

Basic idea:

hi = initial_high_guess(); lo = initial_low_guess(); while (hi > lo) { mid = (hi - lo) / 2 + lo; if (is_less_than(mid)) { hi = mid; // answer is in [lo,mid) } else { lo = mid; // answer is in [mid,hi] } }

Trick:

mid = (hi - lo) / 2 + lo; // intuitive mid = (hi + lo) / 2; // same but quicker // and less error-prone

Oct 23, 2015 Clever Uses of Binary Search 5 of 9

slide-6
SLIDE 6

Iterative solution #1

// adjustable threshold #define EPSILON 1e-9 while (hi - lo > EPSILON) { double mid = (lo + hi) / 2.0; if (/* depends on the problem */) hi = mid; // guess lower else lo = mid; // guess higher } return hi;

Be careful with floating point precision

◮ Correct up to n decimal places ◮ Remember that printf rounds

Oct 23, 2015 Clever Uses of Binary Search 6 of 9

slide-7
SLIDE 7

Iterative solution #2

for (i = 0; i < 50; i++) { double mid = (lo + hi) / 2.0; if (/* depends on the problem */) hi = mid; // guess lower else lo = mid; // guess higher } return hi;

The book points out that log2((10000 − 0)/10−9) ≈ 43

◮ Constant number of iterations avoids precision errors ◮ Avoids the case when #1 results in an infinite loop ◮ Plus you don’t need the check overhead every time

Oct 23, 2015 Clever Uses of Binary Search 7 of 9

slide-8
SLIDE 8

Bisection method

http://en.wikipedia.org/wiki/Bisection method

Oct 23, 2015 Clever Uses of Binary Search 8 of 9

slide-9
SLIDE 9

Binary search the answer

Oct 23, 2015 Clever Uses of Binary Search 9 of 9