Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer.

Similar presentations


Presentation on theme: "Divide and Conquer."— Presentation transcript:

1 Divide and Conquer

2 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)

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!

5 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

6 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 "Divide and Conquer."

Similar presentations


Ads by Google