Unit II
Unit II
These sub problems must be solved, and then a method must be found to combine sub
solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-conquer strategy can
possibly be reapplied.
Often the sub problems resulting from a divide-and-conquer design are of the same
type as the original problem.
D And C(Algorithm) is initially invoked as D and C(P), where ‘p’ is the problem to be
solved.
Small(P) is a Boolean-valued function that determines whether the i/p size is small
enough that the answer can be computed without splitting.
These sub problems P1, P2 …Pk are solved by recursive application of D And C.
Combine is a function that determines the solution to p using the solutions to the ‘k’
sub problems.
If the size of ‘p’ is n and the sizes of the ‘k’ sub problems are n1, n2 ….nk,
respectively, then the computing time of D And C is described by the recurrence
relation.
Where T(n) is the time for D And C on any I/p of size ‘n’.
g(n) is the time of compute the answer directly for small I/ps.
f(n) is the time for dividing P & combining the solution to sub problems.
Example:
1) Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n.
We have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
*
*
*
= n. T(n/n) + n log n
= n. T(1) + n log n [since, log 1=0, 20=1]
= 2n + n log n
BINARY SEARCH
Algorithm, describes this binary search method, where Binsrch has 4I/ps a[], I , l & x.
It is initially invoked as Binsrch(a,1,n,x)
A non-recursive version of Binsrch is given below.
This Bin search has 3 i/ps a, n, & x.
The while loop continues processing as long as there are more elements left to check.
At the conclusion of the procedure 0 is returned if x is not present, or ‘j’ is returned,
such that a[j]=x.
We observe that low & high are integer Variables such that each time through the loop
either x is found or low is increased by at least one or high is decreased at least one.
Thus we have 2 sequences of integers approaching each other and eventually low
becomes > than high & causes termination in a finite no. of steps if ‘x’ is not present.
Example:
1) Let us select the 14 entries.
-15,-6,0,7,9,23,54,82,101,112,125,131,142,151.
Place them in a[1:14], and simulate the steps Bin search goes through as it searches for
different values of ‘x’.
Only the variables, low, high & mid need to be traced as we simulate the algorithm.
We try the following values for x: 151, -14 and 9.
for 2 successful searches & 1 unsuccessful search.
Proof:
We assume that all statements work as expected and that comparisons such as x>a[mid] are
appropriately carried out.
Otherwise we observe that each time thro’ the loop the possible elements to be
checked of or equality with x and a[low], a[low+1],……..,a[mid],……a[high].
If x=a[mid], then the algorithm terminates successfully.
Otherwise, the range is narrowed by either increasing low to (mid+1) or decreasing
high to (mid-1).
Clearly, this narrowing of the range does not affect the outcome of the search.
If low becomes > than high, then ‘x’ is not present & hence the loop is exited.
MERGE SORT
1. Algorithm MergeSort(low,high)
2. //a[low:high] is a global array to be sorted
3. //Small(P) is true if there is only one element
4. //to sort. In this case the list is already sorted.
5. {
6. if (low<high) then //if there are more than one element
7. {
8. //Divide P into subproblems
9. //find where to split the set
10. mid = [(low+high)/2];
11. //solve the subproblems.
12. mergesort (low,mid);
13. mergesort(mid+1,high);
14. //combine the solutions .
15. merge(low,mid,high);
16. }
17. }
1. Algorithm merge(low,mid,high)
2. //a[low:high] is a global array containing
3. //two sorted subsets in a[low:mid]
4. //and in a[mid+1:high].The goal is to merge these 2 sets into
5. //a single set residing in a[low:high].b[] is an auxiliary global array.
6. {
7. h=low; I=low; j=mid+1;
8. while ((h<=mid) and (j<=high)) do
9. {
10. if (a[h]<=a[j]) then
11. {
12. b[I]=a[h];
13. h = h+1;
14. }
15. else
16. {
17. b[I]= a[j];
18. j=j+1;
19. }
20. I=I+1;
21. }
22. if (h>mid) then
23. for k=j to high do
24. {
25. b[I]=a[k];
26. I=I+1;
27. }
28. else
29. for k=h to mid do
30. {
31. b[I]=a[k];
32. I=I+1;
33. }
34. for k=low to high do a[k] = b[k];
35. }
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423, 861, 254,
450, 520)
Algorithm Mergesort begins by splitting a[] into 2 sub arrays each of size five (a[1:5]
and a[6:10]).
The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and 2(a[4:5])
Then the items in a a[1:3] are split into sub arrays of size 2 a[1:2] & one(a[3:3])
The 2 values in a[1:2} are split to find time into one-element sub arrays, and now the
merging begins.
(310| 285| 179| 652, 351| 423, 861, 254, 450, 520)
Repeated recursive calls are invoked producing the following sub arrays.
(179, 285, 310, 351, 652| 423| 861| 254| 450, 520)
Next a[9] &a[10] are merged, and then a[6:8] & a[9:10]
(179, 285, 310, 351, 652| 254, 423, 450, 520, 861 )
At this point there are 2 sorted sub arrays & the final merge produces the fully
sorted result.
(179, 254, 285, 310, 351, 423, 450, 520, 652, 861)
If the time for the merging operations is proportional to ‘n’, then the computing time
for merge sort is described by the recurrence relation.
When ‘n’ is a power of 2, n= 2k, we can solve this equation by successive substitution.
In merge sort, the file a[1:n] was divided at its midpoint into sub arrays which were
independently sorted & later merged.
In Quick sort, the division into 2 sub arrays is made so that the sorted sub arrays do not
need to be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I
between 1 & n and all j between (m+1) & n for some m, 1<=m<=n.
It is assumed that a[p]>=a[m] and that a[m] is the partitioning element. If m=1 & p-
1=n, then a[n+1] must be defined and must be greater than or equal to all elements in
a[1:n]
The assumption that a[m] is the partition element is merely for convenience, other
choices for the partitioning element than the first item in the set are better in practice.
1. Algorithm Partition(a,m,p)
2. //within a[m],a[m+1],…..,a[p-1] the elements
3. // are rearranged in such a manner that if
4. //initially t=a[m],then after completion
5. //a[q]=t for some q between m and
6. //p-1,a[k]<=t for m<=k<q, and
7. //a[k]>=t for q<k<p. q is returned
8. //Set a[p]=infinite.
9. {
10. v=a[m]; i=m; j=p;
11. repeat
12. {
13. repeat
14. i=i+1;
15. until(a[i]>=v);
16. repeat
17. j=j-1;
18. until(a[j]<=v);
19. if (i<j) then interchange(a,i.j);
20. }until(i>=j);
21. a[m]=a[j]; a[j]=v;
22. return j;
23. }
1. Algorithm Interchange(a,i,j)
2. //Exchange a[i] with a[j]
3. {
4. p=a[i];
5. a[i]=a[j];
6. a[j]=p;
7. }
1. Algorithm Quicksort(p,q)
2. //Sort the elements a[p],….a[q] which resides
3. //is the global array a[1:n] into ascending
4. //order; a[n+1] is considered to be defined
5. // and must be >= all the elements in a[1:n]
6. {
7. if(p<q) then // If there are more than one element
8. {
9. // divide p into 2 subproblems
10. j=partition(a,p,q+1);
11. //’j’ is the position of the partitioning element.
12. //solve the subproblems.
13. quicksort(p,j-1);
14. quicksort(j+1,q);
15. //There is no need for combining solution.
16. }
17. }
Divide and Conquer (DAC) approach has three steps at each level of recursion:
3. Combine the solutions of all the sub-problems into a solution for the original
problem.
Maximum and Minimum:
1. Let us consider simple problem that can be solved by the divide-and conquer
technique.
2. The problem is to find the maximum and minimum value in a set of ‘n’ elements.
3. By comparing numbers of elements, the time complexity of this algorithm can be
analyzed.
4. Hence, the time is determined mainly by the total cost of the element comparison.
Explanation:
a. Straight MaxMin requires 2(n-1) element comparisons in the best, average & worst cases.
b. By realizing the comparison of a [i]max is false, improvement in a algorithm can be done.
c. Hence we can replace the contents of the for loop by, If (a [i]> Max) then Max = a [i]; Else if
(a [i]< 2(n-1)
d. On the average a[i] is > max half the time, and so, the avg. no. of comparison is 3n/2-1.
A Divide and Conquer Algorithm for this problem would proceed as follows:
a. Let P = (n, a [i],……,a [j]) denote an arbitrary instance of the problem.
b. Here ‘n’ is the no. of elements in the list (a [i],….,a[j]) and we are interested in finding the
maximum and minimum of the list.
c. If the list has more than 2 elements, P has to be divided into smaller instances.
d. For example, we might divide ‘P’ into the 2 instances, P1=([n/2],a[1],……..a[n/2]) & P2=
( n-[n/2], a[[n/2]+1],….., a[n]) After having divided ‘P’ into 2 smaller sub problems, we can
solve them by recursively invoking the same divide-and-conquer algorithm.
Algorithm:
Example:
A 1 2 3 4 5 6 7 8 9
Values 22 13 -5 -8 15 60 17 31 47
Tree Diagram:
i. As shown in figure 4, in this Algorithm, each node has 4 items of information: i, j, max &
min.
ii. In figure 4, root node contains 1 & 9 as the values of i& j corresponding to the initial call
to MaxMin.
iii. This execution produces 2 new calls to MaxMin, where i& j have the values 1, 5 & 6, 9
respectively & thus split the set into 2 subsets of approximately the same size.
iv. Maximum depth of recursion is 4.
Complexity:
If T(n) represents this no., then the resulting recurrence relations is
T (n)=T([n/2]+T[n/2]+2 n>2
1 n=2
1 n=1
When ‘n’ is a power of 2, n=2k for some positive integer ‘k’, then
T (n) = 2T(n/2) +2
= 2(2T(n/4)+2)+2
= 4T(n/4)+4+2
*
*
= 2k-1 T (2) + Σ 1 ≤ I ≤ k-1 ≤ 2i
= 2k-1+ 2k - 2
T(n) = (3n/2) – 2
Note that (3n/2) - 2 is the best-average and worst-case no. of comparisons when ‘n’ is a
power of 2.
As a simple example, consider searching through a sorted list of items for some target. Brute
force would simply start at the first item, see if it is the target, and if not sequentially move to
the next until we either find the target or hit the end of the list. For small lists this is no
problem (and would actually be the preferred solution), but for extremely large lists we could
use more efficient techniques
//Input: Array Weights contains the weights of all items Array Values contains the values of
all items
for i = 1 to 2n do
j ← n tempWeight ← 0
tempValue ← 0
A[j] ← 0 j ← j – 1
A[j] ← 1
for k ← 1 to n do
if (A[k] = 1) then
bestWeight ← tempWeight
bestChoice ← A return bestChoice
Complexity
2n 1 n 2n
Σ [Σ+ Σ]= Σ[{1+..+1}(n times) +{1+..+1}(n times)]
= O(2n*2n)
= O(n*2n)
Knapsack Problem
Given n items:
weights: w1 w2 … wn
values: v1 v2 … vn
A knapsack of capacity W
Find the most valuable subset of the items that fit into the knapsack (sum of weights ≤ W)
Example:
item weight value Knapsack capacity W=16
1 2 $20
2 5 $30
3 10 $50
4 5 $10
Therefore, the complexity of the Brute Force algorithm is O (n2n). Since the complexity of
this algorithm grows exponentially, it can only be used for small instances of the KP.
Otherwise, it does not require much programming effort in order to be implemented.
Besides the memory used to store the values and weights of all items, this algorithm requires a
two one dimensional arrays (A[] and bestChoice[]).
Example of TSP
Here a graph is given where 1, 2, 3, and 4 represent the cities, and the weight associated with
every edge represents the distance between those cities.
The goal is to find the shortest possible path for the tour that starts from the origin city,
traverses the graph while only visiting the other cities or nodes once, and returns to the origin
city.
For the above graph, the optimal route is to follow the minimum cost path: 1-2-4-3-1. And this
shortest route would cost 10+25+30+15 =80
Travelling Salesman Problem (TSP) : Given a set of cities and distances between every pair of
cities, the problem is to find the shortest possible route that visits every city exactly once and
returns to the starting point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is
to find if there exists a tour that visits every city exactly once. Here we know that Hamiltonian
Tour exists (because the graph is complete) and in fact, many such tours exist, the problem is
to find a minimum weight Hamiltonian Cycle.
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph
is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.
The problem is a famous NP-hard problem. There is no polynomial-time known solution for
this problem.
Tour Cost .
a→b→c→d→a 2+3+7+5 = 17
a→b→d→c→a 2+4+7+8 = 21
a→c→b→d→a 8+3+4+5 = 20
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
a→d→c→b→a 5+7+3+2 = 17
In this problem, we want to compute the convex hull of a set of points? What does this
mean?
Formally: It is the smallest convex set containing the points. A convex set is one in
which if we connect any two points in the set, the line segment connecting these
points must also be in the set.
Informally: It is a rubber band wrapped around the "outside" points.
Theorem: The convex hull of any set S of n>2 points (not all collinear) is a convex
polygon with the vertices at some of the points of S.
How could you write a brute-force algorithm to find the convex hull?
In addition to the theorem, also note that a line segment connecting two points P1 and P2 is
a part of the convex hull’s boundary if and only if all the other points in the set lie on the
same side of the line drawn through these points. With a little geometry:
(x2,y2
)
(x1,y1
Line defined by a=(y2-y1), b=(x1-x2),
c=(x1y2 – x2y1)
For all points above the line, ax + by > c, while for all points below the line, ax + by < c.
Using these formulas, we can determine if two points are on the boundary to the convex
hull.
DIVIDE AND CONQUER UNIT - II