Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Lecture 04 - Divide and Conquer

This document discusses using the divide and conquer algorithmic approach to solve problems. It covers sorting algorithms like quicksort and merge sort, searching algorithms like binary search, and other algorithms like integer multiplication, binary tree traversal, and closest pair point problems.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 04 - Divide and Conquer

This document discusses using the divide and conquer algorithmic approach to solve problems. It covers sorting algorithms like quicksort and merge sort, searching algorithms like binary search, and other algorithms like integer multiplication, binary tree traversal, and closest pair point problems.

Uploaded by

waheedgx
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Algorithms and Problem

Solving
Lecture 04 – Problem Solving
using Divide and Conquer

AL-Zaiem AL-Azhari University


Faculty of Computer Sciences and Information
Technology
Mohammed Mahmoud
Overview
• A divide-and-conquer algorithm recursively
breaks down a problem into two or more sub-
problems of the same or related type, until
these become simple enough to be solved
directly.
• The solutions to the sub-problems are then
combined to give a solution to the original
problem.
a problem of size n
(instance)

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to It general leads to a


the original problem
recursive algorithm!
Divide and Conquer
Algorithms
• Sorting
• Quick Sort
• Merge Sort

• Searching
• Binary Search

• Binary Tree Traversal


• Large Integer Multiplication
• Matrix Multiplication
• Closest-pair
Quick Sort
• A quick sort first selects a value, which is called the
pivot value.
• there are many different ways to choose the pivot value,
we will simply use the first item in the list, to assist with
splitting the list.
• Rearrange the list so that all the elements in the first s
positions are smaller than or equal to the pivot and all
the elements in the remaining n-s positions are larger
than or equal to the pivot
• 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
Quick Sort
p

A[i]p A[i]p

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
Quick Sort
Merge Sort
• Split array A[0..n-1] into about equal halves and make
copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed
portions of the arrays
• copy the smaller of the two into A, while incrementing the index
indicating the unprocessed portion of that array
• Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 71 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9
Merge Sort
Merge Sort
Binary Search Tree
• Binary tree is a divide-and-conquer ready structure!
• Classic traversals (preorder, inorder, postorder) .
• In the preorder traversal, the root is visited before
the left and right subtrees are visited (in that order).
• In the inorder traversal, the root is visited after
visiting its left subtree but before visiting the right
subtree.
• In the postorder traversal, the root is visited after
visiting the left and right subtrees (in that order).
Binary Search Tree
Large Integer
Multiplication
• Consider the problem of multiplying two (large) n-
digit integers represented by arrays of their digits
such as:
• A = 12345678901357986429
• B = 87654321284820912836

• The grade-school algorithm:


a1 a2 … an
b1 b2 … bn
--------------------------------

(d10) d11d12 … d1n


(d20) d21d22 … d2n
…………………
(dn0) dn1dn2 … dnn
Divide and Conquer for
Large Integer Multiplication
• A small example: A  B where A = 2135 and B = 4014
• A = (21·102 + 35), B = (40 ·102 + 14)
• So, A  B = (21 ·102 + 35)  (40 ·102 + 14)
• = 21  40 ·104 + (21  14 + 35  40) ·102 + 35  14

• = (21*10^2 + 35) * (40*10^2 + 14)


• = (21*40)*10^4 + c1*10^2 + 35*14
• where c1 = (21+35)*(40+14) - 21*40 - 35*14, and
• 21*40 = (2*10 + 1) * (4*10 + 0)
• = (2*4)*10^2 + c2*10 + 1*0
• where c2 = (2+1)*(4+0) - 2*4 - 1*0, etc.
Divide and Conquer for
Large Integer Multiplication
Divide and Conquer for
Closest Pair
• Step 0 Sort the points by x (list one) and then
by y (list two).
• Step 1 Divide the points given into two subsets
S1 and S2 by a vertical line x = c so that half
the points lie to the left or on the line and
half the points lie to the right or on the line.
• Step 2 Find recursively the closest pairs for the
left and right subsets.
Divide and Conquer for
Closest Pair
• Step 3 Set d = min{d1, d2} We can limit our
attention to the points in the symmetric vertical strip
of width 2d as possible closest pair. Let C1 and C2 be
the subsets of points in the left subset S1 and of the
right subset S2, respectively, that lie in this vertical
strip. The points in C1 and C2 are stored in increasing
order of their y coordinates, taken from the second
list.
• Step 4 For every point P(x,y) in C1, we inspect
points in C2 that may be closer to P than d. There
can be no more than 6 such points (because d ≤ d2)!
Divide and Conquer for
Closest Pair
Divide and Conquer for
Closest Pair
END!

You might also like