Download presentation
Presentation is loading. Please wait.
1
Divide and Conquer Algorithms Part I
2
The principle of Divide and Conquer
To solve a problem of size n, we divide it into a number of smaller subproblems, and solve one or more of those as needed. Then we conquer the original problem by combining the solution(s) from the smaller subproblems.
3
Divide and conquer definitions
Let b be the ways we divide our problem at each step Then each subproblem is of size One subproblem may be smaller if n is not divisible by b Let f(n) be the cost (count of operations) needed to solve the problem for size n Let g(n) be the cost of combining solutions for a problem of size n, including the preparatory costs for setting up the divisions n/b
4
Recurrence relations for divide and conquer
f(n) = af(n/b) + g(n) a is the number of smaller problems we solve at each step If we divide into non-overlapping problems and solve all of them, If we divide into non-overlapping problems and solve some of them, If some problems overlap, a = b a < b a > b
5
Binary search Input: Output: Algorithm:
Sorted list of n objects and a target object t Output: Is t in the list? Algorithm: Split the list in two halves (size n/2 for even n) Recursively process only the larger half if t is greater than the dividing point, else the other half Repeat until the list has shrunk to size 1
6
Recurrence for binary search
What are b and a? What is g(n)? Check if any objects remain (if none, we are done) For lists of size 1, we need to check if t is it For lists of size greater than 1, we also need to compare t with the middle object When we finish, we do not need to do anything – locating the correct subproblem provides the overall answer f(n) = f(n/2) for even n b=2, a=1 g(n) = 2
7
Finding the minimum and maximum of a sequence
Recursive algorithm Divide the sequence in two halves Find the minimum and maximum of each half The overall minimum (maximum) is the smallest (largest) of the two minima (maxima) Recurrence relation f(n) = 2f(n/2) + 2 for n even
8
Merge sort Recursive algorithm Cost of merging
Split the list in two halves Sort each half Merge the two halves Cost of merging At each step, we compare the two smallest elements from each half and select the smaller – n comparisons at worst f(n) = 2f(n/2) + n in the worst case
9
O notation A function f(n) is O(g(n)) if Practical significance
f(n) ≤ cg(n) for all n≥n0, where c and n0 are constants Practical significance O notation highlights the term that dominates the function f(n), i.e., indicates how fast f(n) grows in the long term It is useful for studying the complexity trend without being cluttered with lesser terms and constants
10
Multiplication of integers
a = (an-1an-2...a0)2, b= (bn-1bn-2...b0)2 Add the following partial products For each position j from 0 to n-1 If aj = 1, add b shifted j bits to the right How many shifts? n-1 = O(n2) How many bit additions? n additions of numbers no longer than 2n bits, total O(n2) Overall total O(n2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.