Sei sulla pagina 1di 17

6-7

Divide & Conquer Strategy


(The Master Theorem)
Merge Sort and its analysis
Design and Analysis of Algorithms CS-212

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.

• Conquer the subproblems by solving them


recursively. If they are small enough, solve
the subproblems as base cases.

• Combine the solutions to the subproblems


into the solution for the original problem.
Merge Sort (Concept)
• At first we need to input in an array. Then
we’ll apply the following steps:
• Step 1: If the array has not more than one
elements then the array has already been
sorted.
• Step 2: Then we keep the half of the elements
of the array in a new array named left and
merge sorted them array.
• Step 3: Then other half elements of array
should kept in right array and merge sorted
the array.
• Step 4: Finally merge the two merge sorted
array.
Any Questions?
Reference Material
• http://faculty.ycp.edu/~dbabcock/PastCourses/cs36
0/lectures/lecture03.html
• https://hackernoon.com/divide-and-conquer-merge-
sort-11c673470427
• https://www.khanacademy.org/computing/computer-
science/algorithms/merge-sort/a/divide-and-
conquer-algorithms
Instructor
Ms. Moonisa Ahsan
CS Dept. (GCWUS)
moonisa.ahsan+info@gmail.com
6
8

Iteration and Recursion


Tree Methods for Solving
Recurrence Relations
Design and Analysis of Algorithms CS-212

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 method, we draw a recurrence tree and


calculate the time taken by every level of tree.
•Finally, we sum the work done at all levels. To draw
the recurrence tree, we start from the given
recurrence and keep drawing till we find a pattern
among levels.
•The pattern is typically a arithmetic or geometric
series.
Recursion Trees
•A recursion tree is useful for visualizing what
happens when a recurrence is iterated. It
diagrams the tree of recursive calls and the
amount of work done at each call.

For instance, consider the recurrence


•T(n) = 2T(n/2) + n2.
Recursion Trees
• The recursion tree for this recurrence has the following form:

• 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

Potrebbero piacerti anche