Download Divide and Conquer - CS Course Webpages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Divide and Conquer
Divide and Conquer
• Probably most well-known technique in Computer Science
• Basis for many common algorithms
•
•
•
•
Binary search
Quicsort/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!
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
• 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