We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11
Divide & Conquer
• Divide the problem into a number of sub-problems that are
smaller instances of the • same problem. • Conquer the sub-problems by solving them recursively. If the sub-problem sizes are • small enough, however, just solve the sub-problems in a straightforward manner. • Combine the solutions to the sub-problems into the solution for the original problem. Recurrence • A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. The worst- case running time T (n) of the MERGE-SORT procedure by the recurrence
• When we state and solve recurrences, we often omit floors,
ceilings, and boundary conditions. Recursion tree • In a recursion tree, each node represents the cost of a single sub-problem somewhere in the set of recursive function invocations. • We sum the costs within each level of the tree to obtain a set of per-level costs, and then we sum all the per-level costs to determine the total cost of all levels of the recursion. T (n) = 3T (n/4) + cn2 • The sub-problem size for a node at depth i is n/4i . • The sub-problem size hits n = 1 when n/4i D = 1 or, equivalently, when i = log4 n. • The tree has log4 n + 1 levels (at depths 0, 1, 2, : : : , log4 n). • Each level has three times more nodes than the level above, and so the number of nodes at depth i is 3i . • Because sub-problem sizes reduce by a factor of 4 for each level we go down from the root, each node at depth i, for i = 0, 1, 2, : : : , log4 n - 1, has a cost of c(n/4i )2. • the total cost over all nodes at depth i, for i = 0, 1, 2, : : : , log4 n - 1, is 3i c(n/4i )2 = (3/16)i cn2. • The bottom level, at depth log4 n , has 3 log4 n = nlog4 3 nodes, each contributing cost T(1), for a total cost of nlog4 3T(1), which is Θ(nlog4 3). Total Cost • The number of levels in the tree is equal to the longest path from the root to a leaf. • The longest path from the root to a leaf is n →(2/3)n → (2/3)2n → ··· → 1. Since (2/3)kn = 1 when k = log3/2 n. • The height of the tree is log3/2 n. • Overall cost = O(cn log3/2 n) = O(n lg n) • If this recursion tree were a complete binary tree of height log3/2 n, there would be 2log3/2 n = nlog3/2 2 leaves. • log3/2 2 =1.70 • Cost at last level = O(nlog3/2 2) • This recursion tree is not a complete binary tree, so it has fewer than nlog3/2 2 leaves. Thus running time at last level is ω(n lg n). Substitution method for verification • Indeed, we can use the substitution method to verify that O(n lg n) is an upper bound for the solution to the recurrence. We show that T (n) ≤ dn lg n, where d is a suitable positive constant. We have
• as long as d ≥ c/(lg 3 - (2/3)).
Questions 1. Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) = 3T(⌊n/2⌋) + n. Use the substitution 2. Argue that the solution to the recurrence T (n) = T (n/3) + T (2n/3) + cn, where c is a constant, is Ω(n lg n) by appealing to a recursion tree. method to verify your answer. 3. Draw the recursion tree for T (n) = 4T (⌊n/2⌋)+cn, where c is a constant, and provide a tight asymptotic bound on its solution. Verify your bound by the substitution method. Ans – 2 The shortest path to a leaf occurs when we take the heavy branch each time.