Professional Documents
Culture Documents
Dr Anwar Ghani
Introduction to recurrences
• Divide-and-Conquer Algorithm
• Decrease-and-Conquer
• Dynamic Programming
• Greedy Algorithm
• Backtracking Algorithm
Algorithm Design
Strategies
Divide-and-Conquer: The problem is divided into a two distinct and independent
subproblems which are solved and combined to obtain the solution to the main problem
Familiar application are Binary Search, Quick Sort, Merge Sort
Dynamic Programming: The problem is split into two overlapping subproblems which
are solved to produce solution to the main problem. Applications include Shortest Path
in Network, Longest Common Subsequence
Greedy Algorithm: At each stage of problem solving, a greedy choice (of largest or
smallest value ) is made from amongst the available options. Common applications are
Shortest Paths in Network, Huffman’s Coding
Backtracking: All available options for the problem are explored by pursuing different
paths in a tree-like structure. Either the solution is found or a dead end is reached. In the
latter case alternative path is explored. Familiar examples are Depth First Search,
Exhaustive Search
Algorithm Analysis
Nonrecursive veusus Recursive Implementation
The basic steps of a given algorithm design can be coded by using Iteration Loops or
Recursive Calls. The former approach is also referred to as Nonrecursive implementation
Nonrecursive Solution: The implementation is based on one or more loops. The loops may
also be nested. The time efficiency of the algorithm is determined by counting the basic
operations in each loop cycle. For example, if the inner loop executes n times and outer loop
m times, the overall time complexity would be O( n.m).
(ii) The definition includes a base value, called boundary condition or initial condition
The solution can be verified for different n. Consider, for example, n=8
(i) Using the formula T(8) =(3/2)8-2=10.
c, when n =1
T(n) =
3T(n/4) + cn2 when n > 1
in asymptotic notation is
T(n) = θ(n2)
Methods for Solving Recurrences
Basic Techniques
The basic methods for obtaining the solution to recurrence equations for the
Decrease-and-Conquer and Divide-and-Conquer algorithms are referred to Iteration
Method and Substitution Method..
Several theorems have been propounded ,which provide a convenient method to get a
quick solution to a recurrence. A celebrated theorem for the solution to generalized Divide-
and-Conquer problems is known as Master Theorem . For details of the theorem, its proof
and applications, the book Introduction to Algorithms by Cormen et al may be consulted
(3rd Edition, page-97)
Step #1: Use the recurrence to set up the equations for the arguments n, n-1,…3,2,1.
Step # 2: On reaching the bottom level ( n=0 ), apply the the boundary condition
Step#3: Add the equations, and cancel identical terms on the left-hand and the right-
hand sides
¾ In this recurrence, T(n) is time to search array of size n, T(n-1) time to search subarray of
size n-1, and c is the cost of searching one array cell. The solution is determined as follows:
Iterating the equation:.
T(n) = T(n-1 ) + c
T(n-1) = T(n-2) + c
…………………………
T(2) = T(1) + c
T(1) = T(0) + c
Adding both sides of the equations, and canceling the equal terms on the left- and right-hand
sides:
T(n) = T(0) + c+ c +………+ c ( n terms )
T(0)=0
T(n) = T(n-1) + c lg n
Iterating:
T(n) = T(n-1 ) + c lg n
T(n-1) = T(n-2) +c lg((n-1)
……………………
T(2) = T(1) +c lg(2)
T(1) = T(0) + c lg(1)
Adding the equations, and canceling equal terms on the left- and right-hand sides:
T(n) = T(0)+ c[ lg(1) + lg(2)+ ……+lg(n-1)+ lg(n) ]
Let n be the size of the main problem, a the number of subproblems, n / b the size of each
subproblem, where a, b>1 are constants. The running time of the algorithm is expressed in
terms of recurrence
T(n ) = a T( n / b ) + f(n)
¾ The subproblems are also referred to as partitions. The number and sizes of partitions,
and the cost function depend on the application
Solving Divide-and-Conquer Recurrences
Substitution Method
The Substitution Method for solving the Divide-and-Conquer recurrence consists of the
following steps
Step #1: In the recurrence, plug in progressively the values n/b, n /b2, n /b3…, on the
right-hand side of the equation
Step #2: Repeat the procedure until the base case is reached
Step #3: The iterative steps would generate some kind of pattern or a series. Perform the
summation to express the running time in closed form.
Step #4: Analyze the summation to express the running time in asymptotic notation
The Substitution Method
Examples
Example(1): The running time of binary search algorithm is given recurrence
T(1) = c ( constant)
T(n) = T(n/2) + c, n > 1
¾ Here T(n/2) is time to search left-half or right-half of a sorted array, c is the combined cost
of comparing one key and finding the middle element in the array. Solution is as follows:
Discarding constant c, and the lower order term n in favor of bigger term nlg n
T(1)= c
T(n)= 3T(n/4) + cn, n > 1
The geometric series has geometric ratio 3/4, which is less than 1. Therefore,
(3/4)0 + (3/4)1+ (3/4)2+………..(3/4)log4 n-1 = θ(1)
By using property of logarithm, 3 log 4 n = n log 4 3, and ignoring constant term θ(1)
T(n) = cn log 4 3 + cn
Since, log 4 3 < 1, the term n log 4 3 is smaller compared to n. The term c n log 4 3 is discarded
in favor of n. Therefore,
T(n) = θ(n) (Asymptotic form)
Solving Divide-and-Conquer Recurrences
Fundamental Theorem
If a and b>1 are constants, the solution to the Divide-and-Conquer recurrence
T(1) = c
The term T(n/bk) would reduce to base case T(1) when n / bk =1, or bk = n.
Or, taking logarithm with base b, k = log b n.
The term T(n/bk) would reduce to base case T(1) when n / b k =1, or bk = n.
Taking logarithm to base b, k = log b n. Substituting for k in the summation:
The geometric series has geometric ratio a/bx By considering the asymptotic behavior of the
series, it can be shown that
T(n) = θ(nx), when a < bx
T(n) = θ(nx log n), when a= bx
T(n) = θ(nlog b a), when a > bx