Binary and Ternary Search

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Lecture 3: Parallel Algorithm Design
Partitioning and Divide-and-Conquer Strategies ITCS 4/5145 Parallel Computing, UNC-Charlotte, B. Wilkinson, Jan 23, 2013.
CS4413 Divide-and-Conquer
CS 2210 (22C:19) Discrete Structures Advanced Counting
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
Binary Search Introduction to Trees. Binary searching & introduction to trees 2 CMPS 12B, UC Santa Cruz Last time: recursion In the last lecture, we learned.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Data Structures for Computer Graphics Point Based Representations and Data Structures Lectured by Vlastimil Havran.
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
CSE554Contouring IISlide 1 CSE 554 Lecture 5: Contouring (faster) Fall 2013.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Ray Tracing Optimizations
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Divide-and-Conquer Pattern ITCS 4/5145 Parallel Computing, UNC-Charlotte, B. Wilkinson, June 25, 2012 slides4.ppt. 4.1.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
3.3 Fundamentals of data representation
Introduction to Algorithms: Divide-n-Conquer Algorithms
Optimization via Search
Divide and Conquer.
Recursion Topic 5.
CSE 554 Lecture 5: Contouring (faster)
Lecture 3: Parallel Algorithm Design
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Analysis of Algorithms
Recursion notes Chapter 8.
Chapter 4 Divide-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Query Processing in Databases Dr. M. Gavrilova
Divide and Conquer.
Algorithm design techniques Dr. M. Gavrilova
KD Tree A binary search tree where every node is a
Types of Algorithms.
Orthogonal Range Searching and Kd-Trees
Divide-and-Conquer The most-well known algorithm design strategy:
Data Structures: Segment Trees, Fenwick Trees
CS 3343: Analysis of Algorithms
Chapter 6 Transform and Conquer.
Dynamic Programming.
Dynamic Programming.
Types of Algorithms.
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Recurrences (Method 4) Alexandra Stefan.
Divide and Conquer Algorithms Part I
More on Search: A* and Optimization
Divide-and-Conquer The most-well known algorithm design strategy:
Incremental Problem Solving for CS1100
CSC 380: Design and Analysis of Algorithms
Types of Algorithms.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
CSC 380: Design and Analysis of Algorithms
Major Design Strategies
Divide and Conquer Merge sort and quick sort Binary search
Major Design Strategies
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Divide and Conquer Merge sort and quick sort Binary search
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning
Algorithms Tutorial 27th Sept, 2019.
Presentation transcript:

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

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)

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

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

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?

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

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

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

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

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 3.3.1 for generating lists