Algorithms Lecture 4
Algorithms Lecture 4
by
Dr. Mohamed Abdel Hameed
Computer Science Dept.
Lecture 4
Lecture Rules
Chapter 4
Divide and Conquer
Divide and Conquer
Divide and Conquer
Divide and Conquer
Divide and Conquer
Divide and Conquer
• Master Method: Memorize three cases and use them to solve recurrences of the
form:
T(n) = a T(n/b) + f(n)
Note: To help us finding a solution, we may use recursion-tree method that converts
the recurrence into a tree whose nodes represent the cost incurred at various level of
the recursion. We use techniques for bounding summations to solve the recurrence.
The Maximum-subarray problem
Three possibilities for the location of the maximum-subarray with respect to the
midpoint (the divide point):
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
The Maximum-subarray problem
Substitution method
1. Guess the solution.
2. Use induction to find the constants and show that the solution works.
Substitution method
Substitution method
Substitution method
Substitution method
Substitution method
Remedy: Subtract off a lower-order term.
Recursion Tree
• Use to generate a guess. Then verify by substitution method.
• Consider the recurrence
Recursion Tree