Divide and Conquer Algorithm
Divide and Conquer Algorithm
1
divide-and-conquer algorithm AAST
Parallelism
Breaking a Stone into Dust
Divide-and-conquer algorithms are naturally adapted for execution
in multi-processor machines, especially shared-memory systems
where the communication of data between processors does not need
to be planned in advance because distinct sub-problems can be
executed on different processors.
2
Prof. Ossama Ismail
Relation between divide and conquer and parallel
programming AAST
3
Prof. Ossama Ismail
Parallel Programming Overview: AAST
Recursive Nature:
•Divide and conquer algorithms are often recursive in nature.
Recursive calls to solve subproblems lend themselves well to parallel
execution because each call can be assigned to a different processor.
This helps in reducing the total execution time.
Parallelizing the Conquer Step:
•In some divide and conquer algorithms, the combine (conquer) step
is the final stage, where the results of the subproblems are merged. In
many cases, this step can also be parallelized, especially if it involves
operations like merging or summing up results from independent
subproblems.
5
Prof. Ossama Ismail
Benefits of Using Divide and Conquer in Parallel
Programming: AAST
6
Prof. Ossama Ismail
Challenges in Parallelizing Divide and Conquer:
AAST
7
Prof. Ossama Ismail
Examples of Divide and Conquer in Parallel Programming:
AAST
•Parallel Merge Sort: Both halves of the array can be sorted in parallel, and the merge step can
also be parallelized by dividing it into smaller sections.
•Parallel Matrix Multiplication: The divide and conquer strategy for matrix multiplication (like
Strassen’s algorithm) can be parallelized by solving smaller matrix products simultaneously.
•Parallel FFT (Fast Fourier Transform): The FFT algorithm, which is often used in signal
processing, can be parallelized using the divide and conquer approach.
Summary:
•Divide and conquer provides a natural framework for parallel programming because of its
recursive division into independent subproblems.
•By executing these subproblems concurrently in parallel, you can achieve significant
performance gains, especially in multi-core or distributed systems.
•While the fit between divide and conquer and parallel programming is strong, careful
consideration of overhead, load balancing, and data dependencies is necessary to realize the
full benefits of parallelism.
8
Prof. Ossama Ismail
2. Parallel Programming Overview: AAST
9
Prof. Ossama Ismail
AAST
10
Prof. Ossama Ismail
Recursion in Words of Wisdom AAST
• Philosopher Lao-tzu:
The journey of a thousand miles begins with a single step
11
Prof. Ossama Ismail
divide-and-conquer algorithm AAST
12
Prof. Ossama Ismail
divide-and-conquer algorithm AAST
Parallelism
Divide-and-conquer algorithms are naturally adapted for execution
in multi-processor machines, especially shared-memory systems
Breaking a Stone into Dust
where the communication of data between processors does not need
to be planned in advance because distinct sub-problems can be
executed on different processors.
13
Prof. Ossama Ismail
divide-and-conquer algorithm AAST
Memory access
Divide-and-conquer algorithms naturally tend to make efficient use
of memory caches. The reason is that once a sub-problem is small
enough, it and all its sub-problems can, in principle, be solved within
the cache, without accessing the slower main memory.
Stack size
In recursive implementations of D&C algorithms, one must make sure that
there is sufficient memory allocated for the recursion stack, otherwise, the
execution may fail because of stack overflow.
Recursion
Divide-and-conquer algorithms are naturally implemented
as recursive procedures. In that case, the partial sub-problems
leading to the one currently being solved are automatically stored in
the procedure call stack. A recursive function is a function that calls
itself within its definition.
H.W.
Write a piece of that returns the stack size used by your computer?
16
Prof. Ossama Ismail
Divide-and-Conquer Algorithm AAST
Method
• Divide problem into subproblems
• Solve subproblems
• If subproblems have same nature as original
problem, the strategy can be applied recursively
17
Prof. Ossama Ismail
Breaking a Stone into Dust AAST
Break Stone:
You want to ground a stone into dust (very small stones)
What is your first step?
First Step
• Use a hammer and strike the stone
Next Step
• If a stone pieces that result is small enough, we are done with
that part
• For pieces that are too large, repeat the
BreakStone process
18
Prof. Ossama Ismail
Breaking a Stone into Dust CONT’d AAST
19
Prof. Ossama Ismail
Divide-and-Conquer Algorithm AAST
20
Prof. Ossama Ismail
Divide-and-Conquer Algorithm AAST
Divide_Conquer(problem P)
{
if Small(P) return S(P);
else {
return Combine(Divide_Conque(P1),
Divide_Conque(P2),…, Divide_Conque(Pk));
}
}
21
Prof. Ossama Ismail
Divide & Conquer recurrence relation AAST
n small
otherwise
22 22
Prof. Ossama Ismail
A Typical Divide and Conquer Case AAST
a problem of
size n
subproblem 1 subproblem 2
of size n/2 of size n/2
Cobine solutions to
Solve the original problem
23
Prof. Ossama Ismail
Divide-and-Conquer Examples AAST
Factorial Problem
Divide-and-Conquer
n
* n!
Factorial n Factorial n -1
25
Prof. Ossama Ismail
Divide-and-Conquer Examples AAST
26
Prof. Ossama Ismail
Sorting Problem AAST
28
Prof. Ossama Ismail
Merge Sort AAST
29
Prof. Ossama Ismail
AAST
30
Prof. Ossama Ismail
Merge Sort Review AAST
31
Prof. Ossama Ismail
Algorithm: Merge-Sort(S,p,r) AAST
• MergeSort(A,p,r)
– if p >= r, do nothing
– if p< r then
• MergeSort(A,p,q)
• MergeSort(A,q+1,r)
• Merge(A,p,q,r)
• Starting by calling MergeSort(A,1,n)
32
Prof. Ossama Ismail
Recurrence Equation Analysis AAST
33
Prof. Ossama Ismail
Analysis of Merge sort AAST
34
Prof. Ossama Ismail
Solving recurrence eqn. for Mergesort
AAST
Iterative Substitution
• In the iterative substitution, or “plug-and-chug,” technique, we
iteratively apply the recurrence equation to itself and see if we
can find a pattern:
bn
1 2 n/2
bn
k 2k n/2k bn
Total time = cn + n log2 n
…
… … …
(last level plus all previous levels)
36
Prof. Ossama Ismail
Merge sort Example AAST
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
37
Prof. Ossama Ismail
Quicksort AAST
A[i]≤p A[i]≥p
• Exchange the pivot with the last element in the first (i.e., ≤)
subarray — the pivot is now in its final position
• Sort the two subarrays recursively 38
Prof. Ossama Ismail
Quicksort Example AAST
Sort [5 3 1 9 8 2 4 ]
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
39
Prof. Ossama Ismail
Analysis of Quicksort AAST
41
Prof. Ossama Ismail
Binary Search Algorithm AAST
42
Prof. Ossama Ismail
Example: binsearch(14, 0,13, S, 22)
AAST
[ ]= S
43
Prof. Ossama Ismail
Analysis of Binary Search AAST
• Time efficiency
– worst-case recurrence: T (n) = 1 + ( ⎣n/2⎦ ), T (1) = 1
solution: T (n) = ⎡log2(n+1)⎤ = O(log n)
44
Prof. Ossama Ismail
AAST
Questions ?
45
Prof. Ossama Ismail
CC 412 AAST