Documente Academic
Documente Profesional
Documente Cultură
Chapter No. 4
Introduction to Algorithms, 2nd Edition
1
Divide and conquer
• Divide and conquer is an algorithmic strategy works by breaking down
a problem into two or more sub-problems of the same or related type,
solving them and make an addition of the sub problems.
• We need to divide a problem into sub-problems , solving them
recursively and combine the sub-problems. So we can assume that, to
follow this strategy we need to divide a into some parts then conquer
or solve the parts and finally combine them. Thus we can solve a
problem easily.
• There are many algorithms those follow divide and conquer
technique. Such as Recursive Binary Search, Merge Sort, Quick sort,
Selection sort, Strassen’s Matrix Multiplication etc.
Parts of Divide & Conquer
• Divide the problem into a number of
subproblems that are smaller instances of
the same problem.
Chapter No. 4
Introduction to Algorithms, 2nd Edition
7
Recurrences
•A recurrence relation is an equation that defines a sequence based
on a rule that gives the next term as a function of the previous
term(s). The simplest form of a recurrence relation is the case
where the next term depends only on the immediately previous
term.
•Recurrences are generally used in divide-and-conquer paradigm.
Let us consider T(n) to be the running time on a problem of size n.
•This description is called a recurrence equation. The study of these
equations and there solutions is an important aspect of a course
on algorithms.
Recurrences
• Recurrence: an equation that describes a function in terms of its value
on smaller functions
• The expression: c n 1
T ( n)
2T n cn n 1
2
is a recurrence.
Recurrences and Running Time
• An equation or inequality that describes a function in terms
of its value on smaller inputs.
T(n) = T(n-1) + n
• Recurrences arise when an algorithm contains recursive calls
to itself
• What is the actual running time of the algorithm?
• Need to solve the recurrence – Find an explicit formula of the
expression – Bound the recurrence by an expression that
involves n.
10
Example Recurrences
• T(n) = T(n-1) + n Θ(n2 )
– Recursive algorithm that loops through the input to eliminate one item
• T(n) = T(n/2) + c Θ(lgn)
– Recursive algorithm that halves the input in one step
• T(n) = T(n/2) + n Θ(n)
– Recursive algorithm that halves the input but must examine every item in the
input
• T(n) = 2T(n/2) + 1 Θ(n)
– Recursive algorithm that splits the input into 2 halves and does a constant amount
of other work
11
Recurrence Tree Method / Recurrence Relations
• In this case, it is straightforward to sum across each row of the tree to obtain the total work
done at a given level:
Recursion Trees
• This a geometric series, thus in the limit the sum is O(n2). The depth of the tree in this case
does not really matter; the amount of work at each level is decreasing so quickly that the total
is only a constant factor more than the root.
Recursion Trees – Expanded Example
• Let's consider another example, T(n) = T(n/3) + T(2n/3) + n.
• Expanding out the first few levels, the recurrence tree is:
Any Questions?
Additional Resources
• https://www.geeksforgeeks.org/analysis-
algorithm-set-4-master-method-solving-
recurrences/
• https://web.stanford.edu/class/archive/cs/
cs161/cs161.1168/lecture3.pdf
Instructor
Ms. Moonisa Ahsan
17
CS Dept. (GCWUS)
moonisa.ahsan+info@gmail.com