Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary and Ternary Search

Similar presentations


Presentation on theme: "Binary and Ternary Search"— Presentation transcript:

1 Binary and Ternary Search
And Divide-and-Conquer, more generally

2 Divide and Conquer Probably most well-known technique in Computer Science Basis for many common algorithms Binary search Quicksort/Mergesort Tree data structures Numerous individual/specialized algorithms Basic idea Split problem into one or more smaller problems Solve the smaller (simpler) problem If needed, combine results from smaller problems to get answer Generally reduces O(n) to O(lg n)

3 Binary Search: the basic algorithm
Also called “Decrease and Conquer” since you have just one subproblem Can be used any time there is a sorted range to search over Typical case: an array of sorted values More general case: a range of discrete values Can be thought of as an array with 1 of each Even more general case: a range of continuous values General idea here is root finding

4 Root Finding as Binary Search
Also called bisection Idea is to find the “zero” of a function Need “transverse” intersection of the function – transition between positive and negative Assumes all positive on one side, all negative on the other. The “function” could be a complex problem that is not as easy to analyze e.g. it fails below some value or succeeds above some value. Can perform binary search on the values Take middle value, determine if positive or negative Keep half the interval remaining Should stop in some tolerance of exact answer If you know the range you begin with and the tolerance, you know exactly how many evaluations you will need! Can easily adapt to find a particular value search on above/below value, rather than above/below 0

5 Binary Search applications
Most interesting cases are when you have a complex function, that you can’t compute all values ahead of time Evaluation process itself can be slow, and might not be solvable for a given value Binary search only works when you have a monotonic function (never decreasing or never increasing) What if you have a function and are trying to find a maximum (or minimum) in some range?

6 Ternary search Used when you’re looking for a maximum (or minimum) in some range. Like binary search, most interesting when you can’t precompute values Needs to be a function that is monotonically increasing to a maximum, then monotonically decreasing

7 Ternary Search (continued)
Idea: Divide range into 3 parts. You can get rid of one of the three parts at each stage (leaving new problem 2/3 the size of old one). Still gives a log search (though not base 2) For range [a,b], form new points at: P = a+(b-a)/3 Q = b-(b-a)/3 (= a+2(b-a)/3) Evaluate function at all 4 points: F(a), F(P), F(Q), F(b) If F(P) > F(Q) then set b to Q i.e. max can’t be between Q and b If F(Q) > F(P) then set a to P i.e. max can’t be between a and P Repeat until a and b are sufficiently close

8 Extensions of Divide and Conquer
Spatial Subdivision (e.g. binary space partition/quadtree/octree) If trying to classify a region as in/out (or all > val, or all < val, etc.) Classify the region as one of three things: Entirely inside Entirely outside Undetermined (usually, partially inside, partially outside) For undetermined cases, form children Children are subregions of the original region BSP: two children (any dimension) Quadree: 4 children (in 2D – four subrectangles of a rectangle) Octree: 8 children (in 3D – 8 subblocks of a block) Recursively classify the children Forms a tree structure

9 Another example: computing powers
If you need to compute xn, could do it with n multiplications But, it is faster to compute xn/2 * xn/2 And, can recursively compute smaller and smaller cases from there (or work bottom-up). Need to account for the cases where n is odd: x*xn/2*xn/2 More generally, notice that x can be things other than numbers (e.g. a matrix) and * does not have to be standard multiplication

10 One more example: binary search a data structure
Can sometimes convert one data structure to another that is easier to search Valuable if you will perform multiple searches Searches can be made binary instead of linear Example: generate lists for all paths in tree from root to leaves Creates large set of lists But, can search those lists in O(lg n) time, once created See book example in for generating lists


Download ppt "Binary and Ternary Search"

Similar presentations


Ads by Google