0% found this document useful (0 votes)
23 views

Algo 3

Uploaded by

csindirareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Algo 3

Uploaded by

csindirareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 3

Divide-and-conquer

LEARNING OBJECTIVES

 Divide-and-conquer  Performance of quick sort


 Divide-and-conquer examples  Recurrence relation
 Divide-and-conquer technique  Searching
 Merge sort  Linear search
 Quick sort  Binary search

DiviDe-anD-conQuer A problem of size n


Divide-and-conquer is a top down technique for designing algo-
rithms that consists of dividing the problem into smaller sub prob-
lems hoping that the solutions of the sub problems are easier to
find and then composing the partial solutions into the solution of
Sub-problem 1 Sub-problem 2
the original problem. of size n/2 of size n/2
Divide-and-conquer paradigm consists of following major phases:
• Breaking the problem into several sub-problems that are similar
to the original problem but smaller in size.
• Solve the sub-problem recursively (successively and independently)
• Finally, combine these solutions to sub-problems to create a A solution to sub- A solution to sub-
solution to the original problem. problem 1 problem 2

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

Example 2: Make bold


99 6 86 15 58 35 86 4 0
<Pivot 1 >Pivot 1 >Pivot
99 6 86 15 58 35 86 4 0 Low Pivot 1 Pivot  
High

99 6 86 15 58 35 86 4 0 Divide: Partition the array A [p - r] into 2 sub arrays A [p


- q – 1] and A [q + 1 - r] such that each element of A [p - q
99 6 86 15 58 35 86 4 0 – 1] is less than or equal to A[q], which is, in turn, less than
or equal to each element of A [q + 1 - r]
4 0
Conquer: Sort the 2 sub arrays A [p − q – 1] and A [q +
Merge: 1 − r] by recursive calls to quick sort.

0 4 6 15 35 58 86 86 99 Combine: Since the sub arrays are sorted inplace, no work


is needed to combine them.
Sort left partition in the same way. For this strategy to
be effective, the partition phase must ensure that the pivot,
6 15 86 99 0 4 35 58 86
is greater than all the items in one part (the lower part) and
less than all those in the other (upper) part. To do this, we
choose a pivot element and arrange that all the items in the
lower part are less than the pivot and all those in the upper
6 99 15 86 35 58 0 4 86 part are greater than it. In the general case, the choice of
pivot element is first element.
(Here number of elements/2 is pivot)
99 6 86 15 58 35 86 0 4
Quick sort (A, 1, 12)
38 81 22 48 13 69 93 14 45 58 79 72
14 58 22 48 13 38 45 69 93 81 79 72
4 0

Quick sort (A, 1, 7) Quick sort (A, 9, 12)


38 58 22 48 13 14 45 93 81 79 72
38 45 22 14 13 48 58
Implementing Merge Sort 72 79 81 93

Merging is done with a temporary array of the same size as


the input array. quick sort (A, 1, 5) quick sort (A, 9, 10)
Pro: Faster than in-place since the temp array holds the result- 38 45 22 14 13 72 79
13 14 22 45 38 72 79
ing array until both left and right sides are merged into the temp
array then the temp array is appended over the input array.
Con: The memory required is doubled. The double mem- quick sort (A, 1, 2) quick sort (A, 4, 5)
ory merge sort runs O(N log N) for all cases, because of its 13 14 45 38
Divide-and-conquer approach. 13 14 38 45

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

Figure 3 The ideal quick sort on a random array Implementation


boolean linear search (int [ ] arr, int target)
Performance of Quick Sort {
•• Running time of quick sort depends on whether the par- int i = 0;
titioning is balanced or unbalanced, it depends on which while (i < arr. length) {
elements are used for partitioning. If the partitioning is if (arr [i] = = target){
balanced, the algorithm runs asymptotically as fast as
return true;
merge sort. If the partitioning is unbalanced, it runs as
}
slowly as insertion sort.
+ + i;
•• The worst case of quick sort occurs when the partitioning
}
routine produces one sub-problem with n – 1 elements and
return false;
one with ‘1’ element. If this unbalanced partitioning arises
}
in each recursive call, the partitioning costs q (n) time.
Example:
Recurrence Relation
Consider the array
T(n) = T(n – 1) + T(1) + q (n)
10 7 1 3 –4 2 20
(\ T(0) = q (1))
= T(n – 1) + q (n) Search for 3
If we sum the costs incurred at each level of the recursion 10 7 1 3 –4 2 20
we get an arithmetic series, which evaluates to q (n2). 3?
•• Best case partitioning–PARTITION produces 2 sub prob- Move to next element
10 7 1 3 –4 2 20
lems, each of size no more than n/2, since one is of size
3?
 n/2  and one of size  n/ 2  – 1
The recurrence for the running time is then Move to next element
10 7 1 3 –4 2 20
T(n) ≤ 2T(n/2) + q(n)
3?
The above Recurrence relation has the solution T(n) = O(n
Move to next element
log n) by case 2 of the master theorem.
10 7 1 3 –4 2 20
•• The average–case time of quick sort is much closer to the
3?
best than to the worst case
Element found; stop the search.
For example, that the partitioning algorithm always pro-
duces a 8-to-2 proportional split, which at first seems unbal- Binary Search
anced. The Recurrence relation will be A binary search algorithm is a technique for finding a particu-
T(n) ≤ T(8n/10) + T(2n/10) + cn lar value in a linear array, by ruling out half of the data at each
3.110 | Unit 3 • Algorithms

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) 3 (B) 4 (A) 14325


(C) 5 (D) 6 (B) 12435
(C) 14253
2. Construct a Binary search tree with the given list of
(D) 12354
elements:
300, 210, 400, 150, 220, 370, 450, 100, 175, 215, 250 5. When pre-order traversal is applied on a given tree,
what is the order of elements?
Which of the following is a parent node of element
250? 1
(A) 220
(B) 150
(C) 370 2 3
(D) 215
3. What is the breadth first search order of the given tree? 4 5

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

7. Find the number of bridges in the given graph (C) 0 1 2 3 4 5 6 7 8 9


h
(D) 9 8 6 4 2 3 0 1 5 7
f n
b t
12. Consider the following graph:

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

(A) a (B) a (C) a (D) a


e b e
b e b b
c f c f c f c f
d g d g d g d g

h h

Practice Problems 2 (A) C (B) A


Directions for questions 1 to 15: Select the correct alterna-
tive from the given choices.
B A B C
1. Which of the following algorithm design technique is
used in finding all pairs of shortest distances in a graph? (C) A (D) B
(A) Divide-and-conquer
(B) Greedy method
(C) Back tracking C B C A
(D) Dynamic programming 6. Consider an undirected unweighted graph G. Let a
breadth first traversal of G be done starting from a node r.
2. Let LASTPOST, LASTIN and LASTPRE denote the
Let d(r, u) and d(r, v) be the lengths of the shortest
last vertex visited in a post-order, in-order and pre-
paths from r to u and v respectively in ‘G’. If u is visited
order traversals respectively of a complete binary tree.
before v during the breadth first travel, which of the fol-
Which of the following is always true?
lowing is correct?
(A) LASTIN = LASTPOST
(A) d(r, u) < d(r, v) (B) d(r, u) > d (r, v)
(B) LASTIN = LASTPRE
(C) LASTPRE = LASTPOST (C) d(r, u) ≤ d (r, v) (D) None of these
(D) LASTIN = LASTPOST = LASTPRE 7. In a complete 5-ary tree, every internal node has exactly
3. Match the following: 5 children. The number of leaves in such a tree with ‘3’
internal nodes are:
X : Depth first search (A) 15 (B) 20
Y : Breadth first search (C) 13 (D) Can’t predicted
Z : Sorting 8. Which of the following algorithm is single pass that is they
a : Heap do not traverse back up the tree for search, create, insert
b : Queue etc.
(A) Depth first search (B) Pre-order traversal
c : Stack (C) B-tree traversal (D) Post-order traversal
(A) X – a, Y – b, Z – c
(B) X – c, Y – a, Z – b 9. Which of the following is the adjacency matrix of the
(C) X – c, Y – b, Z – a given graph?
(D) X – a, Y – c, Z – b a b
4. Let G be an undirected graph, consider a depth first tra-
versal of G, and let T be the resulting DFS Tree. Let ‘U’
be a vertex in ‘G’ and let ‘V’ be the first new (unvisited) c d
vertex visited after visiting ‘U’ in the traversal. Which
of the following is true? (A) 0 1 1 1 (B) 1 1 1 1
1 0 0 
(A) {U, V} must be an edge in G and ‘U’ is a descend-
 0 0 0   0 0
ant of V in T.
1 0 0 1 0 0 0 1
(B) {U, V} must be an edge in ‘G’ and V is a descend-    
ant of ‘U ’ in T. 1 0 1 0 0 0 1 0
(C) If {U, V} is not an edge in ‘G’ then ‘U’ is a leaf in T.
(C) 1 1 1 1 (D) 1 1 1 1
(D) if {U, V} is not an edge in G then U and V must 0
have the same parent in T.
0
 0 0 0   0 0 0 
0 0 0 1 1 0 0 1
5. Identify the binary tree with 3 nodes labeled A, B and C    
on which preorder traversal gives the sequence C, B, A. 1 0 1 0 0 0 1 0
Chapter 3 • Divide-and-conquer | 3.113

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

11 33 (A) I – 1, II – 2, III – 3 (B) I – 2, II – 1, III – 3


(C) I – 2, II – 3, III – 1 (D) I – 1, II – 2, III – 3
10 16 32 44 15. If x is the root of an n-node subtree, then the inorder-
tree-walk takes
(A) q (n) (B) q (n2)
43 55 (C) q (n )3
(D) q (n log n)
3.114 | Unit 3 • Algorithms

Previous Years’ Questions


1. Which one of the following is the tightest upper (C) (a+left_end+1, n-left_end-1, k-left_end-1) and
bound that represents the time complexity of insert- (a, left_end, k)
ing an object into a binary search tree of n nodes? (D) (a, n-left_end-1, k-left_end-1) and (a, left_end, k)
 [2013] 5. Assume that a mergesort algorithm in the worst case
(A) O(1) (B) O(log n) takes 30 seconds for an input of size 64. Which of the
(C) O(n) (D) O(n log n) following most closely approximates the maximum
2. Consider a rooted n node binary tree represented input size of a problem that can be solved in 6 min-
using pointers. The best upper bound on the time utes? [2015]
required to determine the number of sub trees having (A) 256 (B) 512
exactly 4 nodes is O (na logb n). The value of a + 10b (C) 1024 (D) 2048
is _______ [2014]
6. The given diagram shows the flowchart for a recur-
3. Which one of the following is the recurrence equation sive function A(n). Assume that all statements, except
for the worst case time complexity of the Quicksort for the recursive calls, have O (1) time complexity. If
algorithm for sorting n(≥2) numbers? In the recur- the worst case time complexity of this function is O
rence equations given in the options below, c is a con- (na), then the least possible value (accurate up to two
stant. [2015] decimal positions) of α is ____. [2016]
(A) T(n) = 2T(n/2) + cn
Flowchart for Recursive Function A(n)
(B) T(n) = T(n – 1) + T(1) + cn
(C) T(n) = 2T(n – 1) + cn
(D) T(n) = T(n/2) + cn
4. Suppose you are provided with the following function
declaration in the C programming language.
int partition (int a[ ], int n);
The function treats the first element of a [ ] as a pivot,
and rearranges the array so that all elements less than
or equal to the pivot is in the left part of the array, and
all elements greater than the pivot is in the right part
in addition, it moves the pivot so that the pivot is the
last element of the left part. The return value is the
number of elements in the left part. 7. Let A be an array of 31 numbers consisting of a
sequence of 0’s followed by a sequence of 1’s. The
The following partially given function in the C pro-
problem is to find the smallest index i such that A[i]
gramming language is used to find the k th smallest
is 1 by probing the minimum number of locations in
element in an array a [ ] of size n using the partition
A. The worst case number of probes performed by an
function. We assume k ≤ n.
optimal algorithm is . [2017]
int kth_smallest (int a [ ], int n, int k) [2015]
8. Match the algorithms with their time complexities:
{
int left_end = partition(a, n); Algorithm Time complexity
if (left_end+1 == k) {
(P) Towers of Hanoi with n disks (i) Θ (n2)
  return a [left_end];
) (Q) Binary search given n sorted (ii) Θ (n log n)
if (left_end+1 > k) { numbers
  return kth_smallest (__________); (R) Heap sort given n numbers (iii) Θ (2n)
} else {
at the worst case
  return kth_smallest (__________);
} (S) Addition of two n × n (iv) Θ (log n)
} matrices
The missing argument lists are respectively [2017]
(A) (a, left_end, k) and (a+left_end+1, n-left_end-1, (A) P → (iii), Q → (iv), R → (i), S → (ii)
k-left_end-1) (B) P → (iv), Q → (iii), R → (i), S → (ii)
(B) (a, left_end, k) and (a, n-left_end-1, k-left_end-1) (C) P → (iii), Q → (iv), R → (ii), S → (i)
(D) P → (iv), Q → (iii), R → (ii), S → (i)
Chapter 3 • Divide-and-conquer | 3.115

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

You might also like