Algo 3
Algo 3
Divide-and-conquer
LEARNING OBJECTIVES
Divide-and-Conquer Examples
• Sorting: Merge sort and quick sort
• Binary tree traversals
• Binary Search A solution to the
• Multiplication of large integers original problem
• Matrix multiplication: Strassen’s algorithm
• Closest-pair and Convex-hull algorithm
Figure 1 Divide-and-conquer technique.
3. Merge the two sorted sub lists back into one sorted list
Merge Sort 4. The key of merge sort is merging two sorted lists into one,
Merge sort is a sorting algorithm for rearranging lists (or any other such that if we have 2 lists
data structure that can only be accessed sequentially, e.g., file X(x1 ≤ x2 ≤ x3 … ≤ xm) and
streams) into a specified order. Y (y1 ≤ y2 ≤ y3 … ≤ yn) the resulting list is z (z1 ≤ z2 ≤ … ≤ zm+n)
Merge sort works as follows:
1. Divide the unsorted list into two sub lists of about half the size. Example 1: L1 = {3, 8, 9}, L2 = {1, 5, 7}
2. Sort each of the two sub lists. Merge (L1, L2) = {1, 3, 5, 7, 8, 9}
3.108 | Unit 3 • Algorithms
T(N) = 2T(N/2) + N
Figure 2 Tree of recursive calls to quick sort.
= O(N log N)
•• Quick sort is a sorting algorithm with worst case run-
Quick Sort ning time O(n2) on an input array of n numbers. Inspite
Quick sort is an example of Divide-and-conquer strategy. In of this slow worst case running time, quick sort is often
Quick sort we divide the array of items to be sorted into two the best practical choice for sorting because it is effi-
partitions and then call the quick sort procedure recursively cient on the average: its expected running time is O(n
to sort the two partitions, i.e., we divide the problem into log n) and the constants hidden in the O-notation are
two smaller ones and conquer by solving the smaller ones. quite small
The conquer part of the quick sort routine looks like this •• Quick sort algorithm is fastest when the median of the array
is chosen as the pivot element. This is because the resulting
<Pivot >Pivot partitions are of very similar size. Each partition splits itself
Low Pivot High in two and thus the base case is reached very quickly.
Chapter 3 • Divide-and-conquer | 3.109
Example: Underlined element is pivot. The recursion tree for this recurrence has cost ‘cn’ at every
level, until a boundary condition is reached at depth log10n =
3 1 4 5 9 2 6 8 7
q (log n). The recursion terminates at depth log10/8n = q(log n).
3 1 4 5 9 2 6 8 7 The total cost of quick sort is O(n log n)
3 1 4 2 5 9 6 8 7
Searching
Two searching techniques are:
3 1 4 2 9 6 8 7
•• Linear search
3 1 4 2 9 6 8 7 •• Binary search
1 2 4 3 6 7 8 9
Linear Search
4 3 Linear search (or) sequential search is a method for find-
6 8 9
1 ing a particular value in list that consists of checking every
3 4 one of its elements, one at a time and in sequence, until the
1 2 3 4 6 7 8 9
desired one is found. Linear search is a special case of brute
force search. Its worst case cost is proportional to the num-
ber of elements in the list.
1 2 3 4 5 6 7 8 9
step; a binary search finds the median, makes comparison, to Example: Value being searched 123
determine whether the desired value comes before or after it,
and then searches the remaining half in the same manner. A 2 6 7 34 76 123 234 567 677 986
binary search is an example of Divide-and-conquer algorithm.
First (1) mid(5) Last(10)
Implementation 2 6 7 34 76 123 234 567 677 986
function binary search (a, value, left, right)
{ First (6) mid(8) Last(10)
if right < left 2 6 7 34 76 123 234 567 677 986
return not found
mid: = floor ((right –left)/2) + left
First (6) Last(7)
if a [mid] = value Mid (6)
return mid
2 6 7 34 76 123 234 567 677 986
if value < a[mid]
return binary search (a, value, left, mid –1) else return binary search
(a, value, mid + 1, right) First, mid, last (6)
}
Exercises
Practice Problems 1 4. What is the depth first search order of the given graph?
Directions for questions 1 to 15: Select the correct alterna-
1 4
tive from the given choices.
1. How many comparisons are required to search an item
89 in a given list, using Binary search? 2
4 8 19 25 34 39 45 48 66 75 89 95
3 5
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
a
(A) 1–2–4–5–3
(B) 1–4–2–5–3
b c (C) 1–2–4–3–5
(D) 1–2–3–4–5
d e f g
6. What is the order of post-order traversal and in-order
traversals of graph given in the above question?
h (A) 4 – 2 – 5 – 1 – 3 and 4 – 5 – 2 – 3 – 1
(B) 4 – 5 – 2 – 3 – 1 and 4 – 2 – 5 – 1 – 3
(A) acbhdefg (B) abcdefgh (C) 4 – 5 – 2 – 1 – 3 and 4 – 2 – 5 – 1 – 3
(C) adbcefgh (D) aebcdfgh (D) 4 – 5 – 2 – 3 – 1 and 4 – 2 – 5 – 3 – 1
Chapter 3 • Divide-and-conquer | 3.111
a c e k l u s v a
d i g m q o
e
b f
j r p w
(A) 12 (B) 13 h
(C) 11 (D) 10
8. Match the following:
g
I. In-order 1. ABCDEFGHI
II. Pre-order 2. DBHEIAFCG
Among the following sequences
III. Post-order 3. ABDEHICFG
IV. Level-order 4. DHIEBFGCA I. a b e g h f II. a b f e h g
For the tree III. a b f h g e IV. a f g h b e
Which are depth first traversals of the above graph?
A (A) I, II and IV only (B) I and IV only
(C) I, III only (D) I, III and IV only
B C 13. The breadth first search algorithm has been imple-
mented using the queue data structure. One possible
order of visiting the nodes is
D E F G
A B C
H I
F E D
(A) I – 2, II – 3, III – 4, IV – 1
(B) I – 3, II – 1, III – 4, IV – 2
(C) I – 1, II – 2, III – 3, IV – 4 (A) A B C D E F (B) B E A D C F
(D) I – 4, II – 3, III – 2, IV – 1 (C) E A B D F C (D) Both (A) and (B)
9. A complete n-array tree in which each node has ‘n’
14. An undirected graph G has ‘n’ nodes. Its adjacency
children (or) no children.
matrix is given by an n × n square matrix.
Let ‘I’ be the number of internal nodes and ‘L’ be the
(i) Diagonal elements are 0’s
number of leaves in a complete n-ary tree.
(ii) Non-diagonal elements are 1’s
If L = 51 and I = 10 what is the value of ‘n’?
Which of the following is true?
(A) 4 (B) 5
(C) 6 (D) Both (A) and (B) (A) Graph G has no minimum spanning tree
(B) Graph G has a unique minimum spanning tree of
10. A complete n-ary tree is one in which every node has 0
cost (n –1)
(or) n children. If ‘X ’ is the number of internal nodes of a
(C) Graph G has multiple distinct minimum spanning
complete n-ary tree, the number of leaves in it is given by
trees, each of cost (n – 1)
(A) X(n – 1) + 1 (B) Xn – 1
(D) Graph G has multiple spanning trees of different cost.
(C) Xn + 1 (D) X(n + 1) + 1
11. The numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in the 15. Which of the following is the breadth first search tree
given order into an initially empty binary search tree. for the given graph?
The binary search tree uses the usual ordering on natu- a b c d
ral numbers. What is the in-order traversal sequence of
the resultant tree?
(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7 h g f e
3.112 | Unit 3 • Algorithms
h h
10. Which one of the following is the post-order traversal of Which of the following is the resultant binary search tree
the given tree? after deletion of 33?
a
(A) 22
c
b 11 43
f
d e 10 16 32 44
(A) d e a f c b a (B) d e b f c a
(C) e b d f c a (D) a b c d e f 55
(B) 22
Common data for questions 11 and 12:
11. The pre-order traversal of a tree is a b d h i e c f g. Which
of the following is the correct tree?
11 44
(A) a
d 10 16 43 55
b
e c
h i
45
f g
(C) 22
(B) a
c
b 11 32
f g
d e
10 16 43 44
h i
(C) a
55
b
c (D) 22
f g
d e
h i 11 55
(D) a
c 10 16 43 44
b
f g
e d
43
h i
14. Match the following:
12. Which of the following is in-order traversal of the above
tree? I. Articulation Point 1. An edge whose removal
(A) a b h d e i f g c (B) a b d h e i f g c disconnects graph
(C) h d i b e a f c g (D) i d h b e a f c g II. Bridge 2. A vertex whose removal
disconnects graph
13. Consider the below binary search tree
III. Bi connected 3. Maximal set of edges such
22 component that any two edges in the set
lie on a common simple cycle
Answer Keys
Exercises
Practice Problems 1
1. A 2. A 3. B 4. C 5. A 6. B 7. B 8. A 9. C 10. A
11. C 12. D 13. A 14. C 15. A
Practice Problems 2
1. B 2. B 3. C 4. B 5. A 6. D 7. C 8. C 9. A 10. B
11. B 12. C 13. A 14. B 15. A
Previous Years’ Questions
1. C 2. 1 3. B 4. A 5. B 6. 2.2 to 2.4 7. 5 8. C