Beruflich Dokumente
Kultur Dokumente
A wise king once conquered his enemies using divide and conquer :-)
To solve a problem, divide it into smaller problems like itself. Reduce
the problem to smaller ones of the same type, solve them recursively, and
then assemble the solutions to make a whole.
1.1
1.1.1
Steps
Divide
Divide the main problem into a number of subproblems that are smaller
instances of the same problem.
1.1.2
Conquer
Conquer the subproblems by solving them recursively. That is, divide them
again into even smaller problems, until we reach atomic units, where we cant
divide any more. We solve them.
1.1.3
Combine
Combine the solutions to the sub problems into the solution for the original
problem.
1.2
Merge Sort
1.3
The main procedure is the MERGE(A, p, q, r) that merges the 2 sub lists.
A is an array or list such that: p q < r
Hence, r is the index of the last element or size of the list.
The merge procedure assumes that A[p..q] and A[q+1. . . r] are already
sorted. So it merges them to form a single sorted array.
Imagine a deck of cards again for this problem.
1
1.4
Divide and conquer algorithms are analyzed Mathematically by using recurrences, since the algorithms works by creating and calling instances of itself
on smaller problems.
A problem P(n) of size n, which run time T(n) is broken down as such:
1. P(n) is split into 2 problems P1 (n/2) and P2 (n/2)
This keeps happening till the problems get to a base size c, where the
algorithm straightforwardly solves it.
At size n c, the algorithm uses constant time to solve the problem.
Therefore, at size n c, we have: (1)
2. Suppose each sub-division yields w subproblems, each of size 1/z the
size of the original. For merge sort, each step yields 2 sub problems
with each having 1/2 the size of its direct parent. Hence, w, z are both
2 for merge sort.
It takes T(n/z) to solve one subproblem of size n/z. And since there
are w subproblems, the total is wT(n/z)
3. Suppose it takes D(n) time to divide the problem into the subproblems
and
4. It takes C(c) time to combine all sub problems.
The runtime
{ then is the sum total of the runtimes of all the subproblems
(1)
if n c
T(n) =
aT (n/z) + D(n) + C(n)
if n otherwise