Sie sind auf Seite 1von 3

Contents

Divide and Conquer

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. Divide a list of n elements into 2 lists of n/2 elements each


2. Solve each list recursively using mergeSort
3. Merge the 2 sorted sequences to produce the sorted answer.

1.3

Anatomy of Merge Sort

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. Divide the deck of cards into 2 decks of 26 cards each


2. Recursively use merge sort to divide the 2 decks into n/2 * 1/2 sized
decks until each deck has just 1 card. At this point, each single card
is know as an invariant. That is each single card is invariably sorted.
Note that each deck is split into 2 decks of size n/2. A better picture
is into decks leftDeck and rightDeck. Until we have a group that can
only be broken down into 2 decks of with with size n/n = 1 or more
productively n % n = 1
At this point, leftDeck and rightDeck are sorted. We hand over the
various decks to the merge routine for combination.
3. At the bottom level, there are 54 decks, although the algorithm does
not split them 54 times.
Normally, given 2 decks, say leftDeck and rightDeck, the algorithm
assumes that they are sorted and that leftDeck is less than rightDeck,
so it appends rightDeck to lefDeck.
This happens even at the miniscule levels of very bottom decks with
just one card. Merge reeives 2 cards and appends the big one on top
of the small one. It then receives 2 more and adds them ontop of the
already sorted 2 if they are large or if the first card of the received deck
is larger than the last card of the already sorted deck.
Each deck has size n/2 where, n = n/2, and this grows or decays
recursively.
At the top level, there are just 2 decks, each with size n/2.
Merge sort combines them and returns the result as the sorted array.
Again, we bump into a problem if we just mechanically append both
lists.
A good solution is to computer the first 2 elements of the sublists. We
elimitate the smallest element and push it into the sorted list. Next
step is to compare the next 2 first lists until one of the lists is exhausted,
whence we just append the remaining list to the end of the sorted list.
The sorted list in created by the Merge routine is the invariant.
Example:

1.4

Analysis of Merge sort and divide and conquer in general

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

Das könnte Ihnen auch gefallen