0% found this document useful (0 votes)
24 views42 pages

DAA Unit 2

The document discusses the Divide and Conquer algorithm design paradigm, outlining its general method and applications such as Binary Search, Quick Sort, and Merge Sort. It details the three main steps of the technique: Divide, Conquer, and Combine, along with the recursive relations that define the time complexity of these algorithms. Additionally, it provides specific examples and analyses of Quick Sort and Binary Search, including their efficiency and time complexities.
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)
24 views42 pages

DAA Unit 2

The document discusses the Divide and Conquer algorithm design paradigm, outlining its general method and applications such as Binary Search, Quick Sort, and Merge Sort. It details the three main steps of the technique: Divide, Conquer, and Combine, along with the recursive relations that define the time complexity of these algorithms. Additionally, it provides specific examples and analyses of Quick Sort and Binary Search, including their efficiency and time complexities.
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/ 42

DESIGN AND ANALYSIS OF ALGORITHMS

UNIT-II DIVIDE & CONQUER


Divide and Conquer : Genereal Method, applications – Binary search, Quick sort, Merge sort,
Strassen‘s Matrix multiplication.
-*-*-*-
This technique involves in solving a particular problem by dividing it into one or more sub
problems of smaller size; Recursively solving each sub problem and then merging the solved
sub problems to produce a solution to the original problem.
This paradigm involves 3 steps at each level of recursion:

1) Divide : divide the problem into number of sub-problems.


2) Conquer : Conquer the sub problems by solving them recursively. If the sub problem
sizes are small enough, just solve the sub problem directly.
3) Combine : Combine the solutions of sub problems into the solution for Original
Problem.

Let P be a problem to be solved. Let it be divided into k smaller problems


P1, P2, P3, ……, Pk. Same as that of original problem. Let n be the size of P.
Let n1, n2, n3, …. Nk be the sizes of P1, P2, …., Pk.

The general logic (Control abstraction) code for the divide and conquer strategy can
be expressed as

Algorithm DC(P)
{
if P is too small then
return solution of P;
else
{
Divide P into smaller problems P1, P2, .. Pk k>=1
Apply DC to each of the sub problems
Return combine( DC(P1), DC(P2), DC(P3)…..DC(Pk));
}
}

Let T(n) be the time required to find the solution for the given problem. Let g(n) be
the time required to solve the problem if n is small.
Let f(n) be the time required for dividing the problem P into smaller sub problems.
Then the computing time can be expressed as recursive relation.

T(n) = g(n) if n is small


T(n1)+T(n2)+………+T(nk)+f(n) otherwise.

The complexity of many divide and conquer algorithms is given by recurrence relation.

T(n) = T(1) when n=1


aT(n/b)+f(n) when n>1

Where a and b are known constants. We assume that T(1) is known and n is a power
of b i.e n=bk.
One of the methods for solving any such recurrence relation is called the substitution
method. This method repeatedly makes substitutions for each occurrence of the
function T in the right hand side until all such occurrences disappear.

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 1
Consider the case in which a=2 and b=2. Let T(1)=2 and f(n)=n. We have
T(n) = 2T(n/2)+n
T(n) = 2[2T(n/4)+n/2]+n
T(n) = 4T(n/4)+2n
T(n) = 4[2T(n/8)+n/4]+2n
T(n) = 8T(n/8)+3n
:
:
In general we see that T(n) = 2iT(n/2i)+in for any log2n >=i>=1. In particular, then
T(n)=2lognT(n/2 logn)+nlogn corresponding to the choice of i = log n. Thus
T(n)=nT(1)+nlog2n+2n
Example 1) Solve the following recurrence relation
T(n) = 2 if n=1
2T(n/2)+n if n>1

T (n)  2n.T 1  n log2 n (Put n  2k ⇒ k  log 2n)

T (n)  n log2 n  2n (∵T (1)  2)

Example 2) Solve the following Recurrence Relation


T(n) = 2T(n/2)+2, given T(1)=0, T(2)=1 if n>1

n k
T (n)  .T (2)  n  2 (∵2  n)
2
 n
T (n)  .(1)  n  2
2

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 2
3n
T (n)  2
2
T (n)  O(n)

Applications :
Binary Search,
Finding Minimum and Maximum,
Merge Sort,
Quick Sort,
Strassen’s Matrix multiplication

Quick Sort
The technique involved in this quick sort is :
1) Consider the first element of the array as the pivot element.
2) Place the pivot element in the array such that all elements before the pivot element
have a value less than the pivot element and all the elements after the pivot element
have a value greater than the pivot element.
3) At this stage we will be obtaining two different arrays with the pivot element
positioned in between. Apply the same technique to these arrays and repeat the
process until the final solution is obtained.

The quick sort scheme is developed by C.A.R.Hoare, has the best average behavior among all
the sorting methods.

Let ‗A‘ be an array and ‗n‘ the number of elements in the array to be sorted. Choose an
element ‗x‘ from a specific position within the array i.e., x = a[1]
Suppose the elements of array ‗a‘ are partitioned, such that ‗x‘ is placed into position ‗j‘ and
the following conditions hold.

1. Each of the element in positions ‗1‘ through ‗j-1‘ is less than or equal to ‗x‘.
2. Each of the elements in positions ‗j+1‘ through ‗n‘ is greater than or equal to ‗x‘.

Notice that if these two conditions hold for a particular ‗x‘ and ‗j‘, ‗x‘ is the jth smallest
element of array ‗a‘, so that ‗x‘ remains in position ‗j‘ when the array is completely sorted. If
the foregoing process is repeated with the sub arrays a[1] through a[j-1] and a[j+1] through
a[n] and any sub arrays created by the process in successive iterations, the final result is a
sorted array.
Let us illustrate the quick sort with an example.
25 57 48 37 12 92 86 33

First element is placed in its proper position.


12 25 57 48 37 92 86 33

At this point 25 is in its proper position in the array a[2], each element below that
position is less than or equal to 25 and each element above that position is greater than or
equal to 25 Since 25 is in its final position the original problem is decomposed into the
problem of sorting two arrays.
(12) and (57 48 37 92 86 33)
The array of one element is already sorted. To sort the second sub array the process is
repeated and the sub array is further sub divided. The entire array is now be viewed as
12 25 ( 57 48 37 92 86 33)
TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 3
Where parentheses enclose the sub arrays that are yet to be sorted.
12 25 ( 48 37 33 ) 57 ( 92 86 )
and further representations are
12 25 ( 37 33 ) 48 57 ( 92 86 )
12 25 ( 33) 37 48 57 86 (92 )
12 25 33 37 48 57 86 92

Unsorted array
25 57 48 37 12 92 86 33
(12) 25 (57 48 37 92 86 33)
12 25 (48 37 33) 57 (92 86)
12 25 (37 33) 48 57 (92 86)
12 25 33 37 48 57 (92 86)
12 25 33 37 48 57 86 92 Sorted array

Note that the final array is sorted. By this time you should noticed that the quick sort may be
defined most conveniently as a recursive procedure.

Efficiency of Quick Sort is 0(n log n).


Assume that the file size is n and n is a power of 2. say n = 2m
so m = log 2n can be read as log n base 2.

And also assume that the proper position for pivot always points the exact middle element of
the sub array. In that case there will be approximately n comparisons. Actually n-1
comparisons.

The file of size n will be splitted into 2 subfiles each of size n/2.
The file of size n/2 will be splitted into 2 subfiles each of size n/4.
The file of size n/4 will be splitted into 2 subfiles each of size n/8.
The file of size n/8 will be splitted into 2 subfiles each of size n/16.
And so on until the subfile having size 1.
After having the subfile m times there are n files of size 1. Thus the total no of comparisions
for the entire sort is approximately
= n + 2*n/2 + 4 * n/4 + 8*n/8 + ……………………….n*n/n upto m terms
=n*m
= n*log n
= O(n log n)

In analyzing Quick sort, we count only the number of element comparisons C(n). It is easy to
see that the frequency count of other operations is of the same order as C(n). The number of
element comparisons in each call of partition is at most high-low+1. Let r be the total number
of elements in all the calls to partition at any level of recursion.
Cw(n) = 1+2+3+…..+n
Worst case time complexity O(n2)
Average case time complexity O(n log n)

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 4
Algorithm for Quick sort
Algorithm quicksort(a,low,high) Algorithm partition(a,low,high)
{ {
if (low<high) pivotkey := a[low];
{ pivotloc := low;
pivotloc:=partition(a,low,high); for i := low+1 to high do
quicksort(a,low,pivotloc-1); {
quicksort(a,pivotloc+1,high); if (a[i]<pivotkey)
} {
} swap(a,pivotloc+1,i);
swap(a, pivotloc, pivotloc+1);
pivotloc := pivotloc+1;
Algorithm swap(a,i,j) }
{ }
temp:=a[i]; return(pivotloc);
a[i]:=a[j]; }
a[j]:=temp;
}

Binary Search
Divide and Conquer Method can be used to solve this problem.

Recursive Algorithm for Binary Search : Divide and Conquer Method


Algorithm Binsearch(a,low,high,x)
{
if (low=high) then
{ if (x=a[low]) then return low;
else return -1;
}
else
{
mid := (low+high)/2;
if (x=a[mid]) then return mid;
else
if (x<a[mid]) then
return binsearch(a,low,mid-1,x);
else
return binsearch(a,mid+1,high,x);
}
}

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 5
Conventional Method::Iterative algorithm for Binary Search
Algorithm binary_search(a,n,x)
// a is an array of size n
{
low := 1;
high := n;
pos := 0;
while ( low<=high)
{
mid:=(low+high)/2;
if (x = a[mid]) then pos:=mid;
else
if (x > a[mid]) then
low:= mid+1;
else
high := mid – 1;
}
return pos;
}

Example)
Position 1 2 3 4 5 6 7 8 9 10 11 12 13 14
value -15 -6 0 7 9 23 54 82 101 112 125 131 142 151

If x=151 Low high Mid Condition


x a[mid]
1 14 7 151>54
8 14 11 151>125
12 14 13 151>142
14 14 14 151=151 Found

If x=-14 Low high Mid Condition


x a[mid]
1 14 7 -14<54
1 6 3 -14<0
1 2 1 -14>-15
2 2 2 -14<-6
2 1 Not found

If x=9 Low high Mid Condition


x a[mid]
1 14 7 9<54
1 6 3 9>0
4 6 5 9=9 Found

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 6
Binary decision Tree for binary search when n=14

No element requires more than 4 comparisons to be found.


Number of elements of the array are considered as power of 2
i.e n=2m
log(n)=log (2m)
= m.log(2) we know that log2 base 2 is 1
log(n)=m
m=log(n)
maximum comparisons are <= m only
Time complexity of a successful search
Best case = O(1)
Average case = O(log n)
Worst Case = O (log n)
Time complexity of an unsuccessful search = O (log n)

MERGE SORT
Worst Case time complexity O(n log(n))
Assume that the elements are to be sorted in non decreasing order.
Assume that the Array contains n elements a[1], a[2],……a[n]. Imagine them split in to two
sub arrays.

a[1],a[2] ….a[n/2]
a[n/2+1]……a[n]

Each set is individually sorted and the resulting sorted sequences are merged to produce a
single sorted sequence of n elements. Thus we have another ideal example of divide and
conquer strategy in which the splitting is into two equal sized sets and the combining
operation is the merging of two sorted sets into one.

Example
A[1:10] = (310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
A[1:5]=( 310, 285, 179, 652, 351)
A[6:10]=( 423, 861, 254, 450, 520)

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 7
Tree of calls of Merge Sort

Now the merge begins. Note that no movement of data has yet taken place. A record of the
sub arrays is implicitly maintained by the recursive mechanism.

(310| 285| 179|, 652, 351| 423, 861, 254, 450, 520)
(285, 310| 179|, 652, 351 | 423, 861, 254, 450, 520)

(179, 285, 310| 652, 351 | 423, 861, 254, 450, 520)
(179, 285, 310| 351, 652 | 423, 861, 254, 450, 520)
(179, 285, 310, 351, 652 | 423, 861, 254, 450, 520)
(179, 285, 310, 351, 652 | 423| 861| 254| 450, 520)
(179, 285, 310, 351, 652 | 423, 861| 254| 450, 520)
(179, 285, 310, 351, 652 | 254, 423, 861| 450, 520)
(179, 285, 310, 351, 652 | 254, 423, 450, 520, 861)
Final
(179, 254, 285, 310, 351, 423, 450, 520, 652, 861)

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 8
Algorithm for Merge Sort
Algorithm mergesort(low,high) Algorithm merge(low,mid,high)
//a[low:high] is a global array {
{ h:=low;
if (low<high) then i:= low;
{ j:= mid+1;
mid:= (low+high)/2; while ((h<=mid)and(j<=high)) do
mergesort(low,mid); {
mergesort(mid+1,high); if (a[h] <= a[j]) then
//combine the solutions {
merge(low,mid,high); b[i]:=a[h];
} h:=h+1;
} }
else
{
b[i]:=a[j];
h:=h+1;
}
i:=i+1;
}
if (h>mid) then
for k:= j to high do
{
b[i]:=a[k];
i:=i+1;
}
else
for k:= h to mid do
{
b[i]:=a[k];
i:=i+1;
}
for k:= low to high do
a[k]:=b[k];
}

Tree of calls of Merge

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 9
Time Complexity of Merge Sort

If the time for merging operation is 4n, then the computing time for merge sort is described
by the recurrence relation

T(n) = a if n=1
2T(n/2)+cn if n>1

Where n is a power of 2 i.e n=2k. We can solve this equation by successive substitutions

T(n) = 2T(n/2)+cn 1
Replace n with n/2
T(n/2) = 2T(n/4)+cn/2 2
Substituting T(n/2) in equation 1
T(n) = 2(2T(n/4)+cn/2)+cn
T(n) = 4T(n/4)+2cn 3
Replace n with n/4 in eq 1
T(n/4) = 2T(n/8)+cn/4 4
Substituting T(n/4) in equation 3
T(n) = 4(2T(n/8)+cn/4)+2cn
T(n) = 8T(n/8)+4cn 5
:
:
:
:
T(n) = 2k(1)+k.c.n n=2k
= n.a + log n . c. n k=log n
= an + c n logn
It is easy to see that is 2k<=n<=2k+1 then T(n)<=T(2k+1)>=T(N)=O(n log n)

FINDING MINIMUM AND MAXIMUM


Let us consider another simple problem that can be solved by the divide and conquer
technique. The problem is to find the maximum and minimum items in a set of n elements.

Conventional method- Iterative Algorithm

Algorithm minmax(a,n,min,max)
{
min:=max:=a[1];
for i:= 2 to n do
{
if a[i] < min then min:=a[i];
if a[i] > max then max:=a[i];
}
}

In analyzing the time complexity of this algorithm we once again concentrate on the number
of element comparisons. In the conventional method there are 2(n-1) element comparisons in
the best average and worst cases. Time complexity is O(n)

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 10
Recursive algorithm- Divide and Conquer Method
Algorithm minmax(lowpos,highpos,min,max)
{
if (lowpos=highpos) then min=max=a[lowpos];
else
if (lowpos=highpos-1) then
{
if (a[lowpos]<a[highpos]) then
{
min:=a[lowpos];
max:=a[highpos];
}
else
{
min := a[highpos];
max:= a[lowpos];
}
}
else
{
mid := (lowpos+highpos)/2;
minmax(lowpos,mid,min,max);
minmax(mid+1,highpos,min1,max1);
// combine
if (max<max1) then max:=max1;
if (min<min1) then min:=min1;
}
}

This is a recursive algorithm that finds the minimum and maximum of the set of elements
(a[1],a[2],a[3]….a[n]}. The situation of set sizes one ( lowpos = highpos ) and two
(lowpos=highpos-1) are handled separately. For sets containing more than two elements the
midpoint is determined and two new sub problems are generated. When the min and max of
these two sub problems are determined, the two max are compared and the two min are
compared to achieve the solutions for the entire set.

The number of comparisons needed for minmax() will be T(n). The recurrence relation is
T(n)= T(n/2)+T(n/2)+2 n>2
1 n=2
0 n=1
T (n)  2 T  2   ∑ 2
k1 i

1ik 1
3n
T (n)  2k1  2k  2  2
2

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 11
3n
Note that  2 is the best, average and worst case number of comparisons when n is a
2
power of 2.

Example ) Suppose we simulate the minmax on the following nine elements of array a:

Position 1 2 3 4 5 6 7 8 9
Array Value 22 13 -5 -8 15 60 17 31 47
minmax(1,9,min,max)
minmax(1,5,min,max) minmax(6,9,min,max)
minmax(1,3,min,max) minmax(4,5,min,max)
minmax(1,2,min,max) minmax(3,3,min,max)
minmax(1,1,min,max) minmax(2,2,min,max)
minmax(4,4,min,max) minmax(5,5,min,max)
minmax(6,7,min,max) minmax(8,9,min,max)
minmax(6,6,min,max) minmax(7,7,min,max)
minmax(8,8,min,max) minmax(8,8,min,max)
minmax(9,9,min,max) minmax(9,9,min,max)

Trees of recursive calls of minmax

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 12
Defective Chessboard problem
The Defective Chessboard problem, also known as the Tiling Problem is an interesting problem.
It is typically solved with a ―divide and conquer‖ approach. The algorithm has a time complexity
of O(n²).

The problem
Given a n by n board where n is of form 2^k where k >= 1 (Basically, n is a power of 2 with
minimum value as 2). The board has one missing square). Fill the board using trionimos. A
trionimo is an L-shaped tile is a 2 × 2 block with one cell of size 1×1 missing.

Solving the problem efficiently isn‘t the goal of this post. Visualizing the board as the algorithm
runs is much more interesting in my opinion, though. Lets discuss the algorithm first.

The board with no defects added

The Algorithm
As mentioned earlier, a divide-and-conquer (DAC) technique is used to solve the problem. DAC
entails splitting a larger problem into sub-problems, ensuring that each sub-problem is an exact
copy of the larger one, albeit smaller. You may see where we are going with this, but lets do it
explicitly.
The question we must ask before writing the algorithm is: is this even possible? Well, yes. The total
number of squares on our board is n², or 4^k. Removing the defect, we would have 4^k — 1, which
is always a multiple of three. This can be proved with mathematical induction pretty easily, so I
won‘t be discussing it.

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 13
The board in the initial state (with an added defect)
The base case
Every recursive algorithm must have a base case, to ensure that it terminates. For us, lets consider
the case when n, 2^k is 2. We thus have a 2×2 block with a single defect. Solving this is trivial: the
remaining 3 squares naturally form a trionimo.
The recursive step
To make every sub-problem a smaller version of our original problem, all we have to do is add our
own defects, but in a very specific way. If you take a closer look, adding a ―defect‖ trionimo at the
center, with one square in each of the four quadrants of our board, except for the one with our
original defect would allow use to make four proper sub-problems, at the same time ensuring that
we can solve the complete problem by adding a last trionimo to cover up the three pseudo-defective
squares we added.

Once we are done adding the ―defects‖, recursively solving each of the four sub-problems takes us
to our last step, which was already discussed in the previous paragraph.
The combine step
After solving each of the four sub-problems and putting them together to form a complete board,
we have 4 defects in our board: the original one will lie in one of the quadrants, while the other
three were those we had intentionally added to solved the problem using DAC. All we have to do is
add a final trionimo to cover up those three ‗defects‘ and we are done.

Thus, the recursive equation for time complexity becomes:

T(n) = 4T(n/2)+c

The 4 comes from the fact that to solve each problem of input n, we divide it into 4 sub-problems
of input size n/2 (half the length of the original board). Once we are done solving those sub-
problems, all that‘s left to be done is to combine them: this is done by adding the last trionimo to
cover up the pseudo-defects we added. This, of course, is done in constant time.

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 14
If you are interested in finding the asymptotic time complexity of the recurrence relation, you could
try the recursion tree method or the substitution method. For now, lets just use the Master
theorem.

The master theorem says that for a recurrence relation of the form:

T(n) = aT(n/b) + f(n)

the complexity depends on the complexities of f(n) and n ^ log_b(a) (the log is to the base b).

The cases in the image below tell us which case we need to use here.

TVVK DAA UNIT-II Divide & Conquer and Greedy Method Page 15
16

THE GREEDY METHOD


Greedy Method:General Method, Applications- 0/1 knapsackproblem, minimum cost spanning
trees, single source shortest path problem, Optimal Merge Patterns.
* * *

Divide and conquer technique is applicable only for problems, which can be divisible. There
exist some problems which cannot be divisible.

In divide and conquer approach, a problem is divided recursively into sub problems of same
kind as the original problem, until they are small enough to be solved and finally the
solutions of the sub problems are combined to get the solution of the original problem. In
Greedy approach, a problem is solved by determining a subset to satisfy some constraints. If
that subset satisfies the given constraints, then it is called as feasible solution, which
maximizes or minimizes a given objective function. A feasible solution that either
maximizes or minimizes an objective function is called as optimal solution.

Most of the problems have n inputs and require us to obtaining a subset that satisfies some
constraints. Any subset that satisfies these constraints is called a feasible solution. We need to
find a feasible solution that either maximizes or minimizes a given objective function. A
feasible solution that does this is called an optimal solution.
The greedy method suggests that one can devise an algorithm that works in stages,
considering one input at a time. At each stage, a decision is made regarding whether a
particular input is in an optimal solution. This is done by considering the inputs in an order
determined by some selection procedure.
Example 1) Let us find the maximum value for the following problem. Given objective
function is Z = 3x + 4y subjected to 0 <= x <= 1
-1 <= y <= 1
Assume input set as (x,y)
{(0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1), (2, 2), (3, 3), (-4, -4) }
After applying conditions to input set, {(2, 2), (3, 3), (-4, -4)} pairs are removed from input
set. Remaining pairs {(0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1) }are called feasible solution.
Among the feasible solution at (1,1) the objective function is maximum.
Z=3x1 + 4x1 = 7 So (1, 1) is an optimal solution for the given objective function.
Control Abstraction of Greedy Method
Algorithm greedy(a,n) // a contains n inputs
{
solution:=0;
for i:= 1 to n do
{
x=select(a);
if feasible (solution, x) then
{
solution := Union( solution, x);
}
else
reject();
}
return solution;
}

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
17

APPLICATIONS
JOB Sequencing with Dead Lines, 0/1 KNAPSAK PROBLEM , MINIMUM COST
SPANNING TREES, SINGLE SOURCE SHORTEST PATH PROBLEM

KNAPSACK PROBLEM
We are given n objects and a knapsack (Bag). Object i has a weight wi and knapsack has a
capacity of m. If a fraction xi such that 0<=xi<=1 of object i is placed into a knapsack, then a
profit of pixi is earned. Since the knapsack capacity is m, we require the total weight of all
chosen objects to be at most m.
Maximize ∑ pi xi ----------------------------(1)
1in

Subject to ∑ wx i i  m ------------------------- (2)


1in

And 0  xi  1, 1  i  n ---------------(3)
We obtain a feasible solution when equations (2) & (3) are satisfied & optimal solution is
obtained when eq(1) is also satisfied.
Algorithm greedyknapsack(m,n)
{
for i:= 1 to n do
x[i]:=0;
u:=m;
for i:= 1 to n do
{
if (w[i] > u) then break;
x[i]:=1.0;
u:=u-w[i];
}
if (i<=n) then
x[i]:=u/w[i];
}
Example 1)
Total number of objects n=3,
Total capacity m=20,
Profits of Knapsack (p1,p2,p3)=(25,24,15),
Weights (w1,w2,w3)=(18,15,10)
Algorithm greedy_knapsack(20,3)
{
for i:= 1 to 3 do
x[1]:=x[2]:=x[3]:=0;
u:=20;
for i:= 1
{
if (w[1] > 20) then break; i.e. 18>20 false
x[1]:=1.0;
u:=u-w[i]; u=20-18=2
}
for i:= 2
if (w[2] > 2) then break; i.e. 15>2 true so break

if (2<=3) then
x[i]:=u/w[i]; x[2]:=2/15=0.13
}
Total profit = 25*1 +24*0.13 = 28.2
TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
18

To solve the knapsack problem we consider 3 optimization measures:


1) Consider the objects with their profits in descending order.
2) Consider the objects with their weights in ascending order.
3) Consider the objects with their profit/weight ratio in descending order.

Case 1: Try to fill the knapsack by including the object with largest profit. If an object
under consider does not fit, then a faction of it is included to fill the knapsack. Thus each
time an object is included into the knapsack, we obtain largest possible increase in profit
i.e. object1 (p1=25) is placed into the knapsack, and then x1=1 and a profit of 25 is
earned. Then m=20-18=2. i.e. 2 units of space is left in the knapsack. Objet2 has the
second largest profit (p2=24) but w2=15>2 and does not fit into the knapsack. Using
x2=2/15 fills the knapsack exactly with the part of object2. Profit earned is 24*2/15=3.2
Total profit earned is 25+3.2=28.2 .

This method used to obtain the solution is termed as “Greedy method” because at each
step, we choose to introduce that object which will increase the objective function value
the most. However, this did not yield the optimal solution.
n
px px p x p x

i1 i i 11 2 2 3 3
= 25*1+24*2/15+15*0
= 25+3.2+0
= 28.5
Case 2: Try to be greedy with the capacity & use it up as slowly as possible. This
requires to consider the objects in the order of increasing weights. The object with lowest
weight is object3 (w3=10) is placed into the knapsack first. So, x3=1 and the profit of
15*1=15 is earned. Object 2 has the next highest weight(w2=15). But it does not fit into
the knapsack. Using x2=10/15 fits the knapsack exactly with part of object2 and the profit
earned is 24*10/15=16.
n profit earned is 15+16=31
Total
px px p x p x

i1 i i 11 2 2 3 3
= 25*0+24*10/15+15*1
= 0+16+15
= 31
Case 3: Consider the object that has max profit/weight ratio used, i.e consider the objects
in the ratio of pi/wi in decreasing order. The first object i.e to be considered is object2
(p2/w2=1.6). So, x2=1 and a profit of 21*1=24 is earned. M= 20-15=5 units of space is
left in the knapsack. The object to be considered next is object3 (p3/w3=1.5) but it does
not fit into the knapsack. So, fraction of object of object3 i.e x3=5/10=05 is inserted into
the knapsack & profit earned is 15*0.5=7.5.

Total profit earned is 31.5.

p1/w1=1.4 p2/w2=1.6 p3/w3=1.5

descending order of profit/weight ratio p2, p3, p1


n
px px p x p x

i1 i i 11 2 2 3 3
= 25*0 + 24*1 + 15*1/2
= 0 + 24 + 7.5
= 31.5
TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
19

X1 X2 X3 ∑ wixi ∑ pixi
1 2/15 0 20 28.2
0 2/3 1 20 31
0 1 ½ 20 31.5 Optimal Solution

The solution for Knapsack problem is obtained when the objects are considered
according to their profits/weights ratio in descending order.

Example 2) Find the optimal solution for given instance of Kanpsack problem
N=7,
M=15,
(p1, p2, p3, p4, p5, p6, p7) = (10, 5, 15, 7, 6, 18, 3)
(w1,w2,w3,w4,w5,w6,w7) = ( 2, 3, 5, 7, 1, 4, 1)

Find the optimal solution for


1) Maximum profit
2) Minimum weight
3) Maximum profit per unit weight

Solution:

Case 1) Maximum profit ---- Decreasing order of profits ( P6,P3,P1,P4,P5,P2,P7 )


X6=1
X3=1
X1=1
X4=4/7
∑pixi = p1x1+p2x2+p3x3+p4x4
= 18*1+15*1+10*1+7*4/7=47

Case 2) Minimum Weight – Increasing order of weights ( w5,w7,w1,w2,w6,,w3,w4 )


X7=1
X5=1
X1=1
X2=1
X6=1
X3=4/5
∑pixi =p1x1+p2x2+p3x3+p5x5+p6x6+p7x7
=10*1+5*1+15*4/5+6*1+18*1+3*1=54

Case 3) Descending order of profit / weight ratio


p1/w1 =10/2=5
P2/w2=5/3=1.6
P3/w3=15/5=3
P4/w4=7/7=1
P5/w5=6/1=6
P6/w6=18/4=4.5
P7/w7=3/1=3
∑pixi = p5x5+p1x1+p6x6+p3x3+p7x7+p2x2
= 6*1+10*1+ 18*1 + 15*1 + 3*1+ 5*2/3 = 55.3

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
20

Spanning Trees :
A Spanning tree of a graph is any tree that includes every vertex in the graph.
A Spanning tree of a graph G is a sub graph of G that is a tree and contains all the vertices of
G containing no circuit or cycle.
An edge of a spanning tree is called a branch
An edge in the graph that is not in the spanning tree is called a Chord.
It spans the graph, i.e. it includes every vertex of the graph.
It is a minimum cost spanning tree i.e. the total weights of all the edges is as low as possible.

Example 1) An Undirected graph and three of its spanning trees

If a graph consist of n vertices then the possible spanning trees are nn-2, for above example
n=3, i.e 33-2=3 spanning trees.

Example 2) Number of vertices =4


Number of spanning trees= 4(4-2) = 42 = 16

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
21

MINIMUM-COST SPANNING TREES


Let G=(V,E) be an undirected connected graph. A sub graph t=(V,E‟) of G is a spanning tree
of G iff t is a tree.

Figure 2) A graph and its minimum cost spanning tree

Applications of Spanning Trees:


1) They can be used to obtain an independent set of circuit equations for an electric network.

2) Using the property of spanning trees that a spanning tree is a minimum su graph G‟ of G
such that V(G‟)=V(G) and G‟ is connected. If the nodes of G represent cities, edges of G
represent possible communication links connecting the 2 cities, then minimum no of links
needed to connect „n‟ cities is (n-1) .

Given a weighted graph in which edges have weights assigned to them where weights
represent cost of construction, length of link,… One need to have min total cost or minimum
total length. In either case the links selected have to form a tree. If this is not so, then the
selection of links contain a cycle.

The identification of min cost spanning tree involves the selection of subset of edges.

The two algorithms used to obtain minimum cost spanning trees from a given graph are
1) Prim‟s Algorithm
2) Kruskal‟s Algorithm

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
22

PRIM’S ALGORITHM
Algorithm prim(E, cost, n, t)
{
let (k,l) be an edge of minimum cost in E;
mincost := cost[k, l];
t[1,1]:=k;
t[1,2]:=1;
for i:=1 to n do
if (cost[i,l]<cost[i,k]) then near[i]:=l;
else near[i]:=k;
near[k]:=near[l]:=0;
for i:= 2 to n-1 do
{
Let j be an index such that near[j] != 0 and cost[j, near[j]] is minimum
t[I,1]:=j;
t[I,2]:=near[j];
mincost := mincost + cost[j,near[j]];
near[j] := 0;
for k:= 1 to n do
if (near[k]!= 0) and (cost[k,near[k]]>cost[k,j]) then
near[k]:=j;
}
return mincost;
}

This is a greedy method to obtain a minimum cost spanning tree which builds the tree edge
by edge. The next edge to be included is chosen according to a criteria i.e. choose an edge
that results in minimum increase in sum of edges cost so far included.

The algorithm will start with a tree that includes only the min cost edge of „G‟, then edges are
added to this tree one by one. The next edge (i, j) to be added is such that „i‟ a vertex already
included in the tree & „j‟ is a vertex not yet included, in the tree & cost (i, j) is minimum.
Among all edges (i, j) efficiently,. We associate with each vertex j, a value near[j] which is a
vertex in the tree such that cost [j, near[j]] is min. among all choices for next near[j]. We
define near[j]=0 for all vertices j that are already in the tree. The next edge to be included is
defined by vertex „j‟ such that near[j]!=0 and cost[j, near[j]] is minimum.

The Time Complexity of Prim‟s algorithm is O(n2). The algorithm spends most of the time in
finding the smallest edge. So time of the algorithm basically depends on how do we search
this edge. Therefore Prim‟s algorithm runs in O(n2) time.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
23

Figure 3) Stages in Prim’s algorithm

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
24

Tracing of the Prim‟s algorithm

Cost Matrix
1 2 3 4 5 6 7
1 0 28 α α α 10 α
2 28 0 16 α α α 14
3 α 16 0 12 α α α
4 α α 12 0 22 α 18
5 α α α 22 0 25 24
6 10 α α α 25 0 α
7 α 14 α 18 24 α 0

Minimum cost edge(k,l) = (1,6) i.e. mincost=10 select (1,6)


t[1,1] = k = 1
t[1,2] = l = 6

for i=1
if cost[i,l]<cost[i,k] then near[i]=l else near[i]=k
cost[1,6]<cost[1,1] ?
10<0 ? no so near[i]=k i.e. near[1]=1

for i=2
cost[2,6]<cost[2,1] ?
α < 28 ? no so near[i]=k i.e. near[2]=1

for i=3
cost[3,6]<cost[3,1] ?
α < α ? no so near[i]=k i.e. near[3]=1

for i=4
cost[4,6]<cost[4,1] ?
α < α ? no so near[i]=k i.e. near[4]=1

for i=5
cost[5,6]<cost[5,1] ?
25 < α ? yes so near[i]=l i.e. near[5]=6

for i=6
cost[6,6]<cost[6,1] ?
0 < 10 ? yes so near[i]=l i.e. near[6]=6

for i=7
cost[7,6]<cost[7,1] ?
α < α ? no so near[i]=k i.e. near[7]=1

near[1]=0
near[6]=0 since edge(1,6) is included in the tree

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
25

i=2
j 1 2 3 4 5 6 7
near[j] 0 1 1 1 6 0 1
cost[j, near[j] - 28 α α 25 -- α

we select j=5 since cost[j,near[j] i.e. cost [5,6]=25 is minimum


edge(5,6) is included
near[j]!=0
t[2,1]=5
t[2,2]=6
mincost = mincost + cost[j,near[j]]
= 10 + 25 = 35

near[5]=0

k 1 2 3 4 5 6 7
near[k] 0 1 1 1 0 0 1
cost[k, near[k] -- 28 α α -- -- α

for all k where near[k]!=0 && (cost[k,near[k]]>cost[k,j])


j=5
k=2 near[k]!=0 && cost[2,1]>cost[2,5] ?
28>α ? no

k=3 near[k]!=0 && cost[3,1]>cost[3,5] ?


α > α ? no

k=4 near[k]!=0 && cost[4,1]>cost[4,5] ?


α > 22 ? yes so near[k]=j i.e. near[4]=5
k=7 near[k]!=0 && cost[7,1]>cost[7,5] ?
α > 24 ? yes so near[7]=j i.e. near[7]=5

i=3
J 1 2 3 4 5 6 7
near[j] 0 1 1 5 0 0 5
cost[j, near[j] -- 28 α 22 -- -- 24

we select j=4 since cost[j,near[j]] i.e cost[4,5] = 22 is minimum edge(4,5) is included


j=4
t[3,1] = 4
t[3,2] = 5
mincost = mincost + cost[j,near[j]]
= 35+22 = 57
near[4]=0

K 1 2 3 4 5 6 7
near[k] 0 1 1 0 0 0 5
cost[k, near[k] -- 28 α -- -- -- 24

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
26

For all k where near[k]!=0 && (cost[k,near[k]>cost[k,j])


K=2
near[k]!=0 && cost[2,1] > cost[2,4] ?
28 > α ? no

K=3
near[k]!=0 && cost[3,1] > cost[3,4] ?
α >12 ? yes
near[k]=j i.e. near[3]=4

K=7
near[k]!=0 && cost[7,5] > cost[7,4] ?
24 > 18 ? yes
near[k]=j i.e. near[7]=4
i=4
J 1 2 3 4 5 6 7
near[j] 0 1 4 0 0 0 4
cost[j, near[j] -- 28 12 -- -- -- 18

we select j=3 since cost[j,near[j]]


i.e. cost[3,4]=12 is minimum
edge(3,4) is included
j=3

t[4,1] = 3
t[4,2] = 4
mincost=mincost+cost[j,near[j]]
=57+12= 69
near[3]=0

K 1 2 3 4 5 6 7
near[k] 0 1 0 0 0 0 4
cost[k, near[k] -- 28 -- -- -- -- 18

For all k where near[k]!=0 && (cost[k,near[k]>cost[k,j])


j=3
K=2
near[k]!=0 && cost[2,1] > cost[2,3] ?
28 > 16 ? yes
Near[k]=j i.e near[2]=3

K=7
near[k]!=0 && cost[7,4] > cost[7,3] ?
18 > α ? no

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
27

i=5
j 1 2 3 4 5 6 7
near[j] 0 3 0 0 0 0 4
cost[j, near[j] -- 16 -- -- -- -- 18

we select j=2 since cost[j,near[j]]


i.e. cost[2,3]=16 is minimum
edge(2,3) is included
j=2

t[5,1] = 2
t[5,2] = 3
mincost=mincost+cost[j,near[j]]
=69+16= 85
near[2]=0

K 1 2 3 4 5 6 7
near[k] 0 0 0 0 0 0 4
cost[k, near[k] -- -- -- -- -- -- 18

For all k where near[k]!=0 && (cost[k,near[k]>cost[k,j])


j=2

K=7
near[k]!=0 && cost[7,4] > cost[7,2] ?
18 > 14 ? yes
Near[k]=j i.e. near[7]=2
i=6
j 1 2 3 4 5 6 7
near[j] 0 3 0 0 0 0 2
cost[j, near[j] -- -- -- -- -- -- 14

we select j=7 since cost[j,near[j]]


i.e. cost[7,2]=14 is minimum
edge(7,2) is included
j=7

t[6,1] = 7
t[6,2] = 4
mincost=mincost+cost[j,near[j]]
=85+14= 99
near[7] = 0

K 1 2 3 4 5 6 7
near[k] 0 0 0 0 0 0 0
cost[k, near[k] -- -- -- -- -- -- --
i reaches n-1 i.e. (7-1=6) the algorithm terminates and returns mincost as 99 and the edges
of MST are stored in array t.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
28

Ex 2) Minimal spanning tree using Prim’s algorithm


1
55 45
25
2
3 4 30
8

5 40 20
50

5 7
15
10
35
6

Total weight = 5+ 15 + 10 + 20 + 30 + 40 + 25 = 145

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
29

KRUSKAL’S ALGORITHM

The set t(edges) is initially empty. As the algorithm progresses, edges are added to „t‟. When
„t‟ is initially empty, each node of G forms a distinct trivial connected component. As long as
no solution is found, partial graph formed by the nodes and edges in the „t‟ consists of several
connected components. The elements of t included in a given connected component form a
minimum spanning tree for the nodes in this component. At the end of the algorithm only one
connected component remains. So, t is then a minimum spanning tree for all nodes of G. To
build bigger and bigger connected components, we examine the edges of G in the order of
increasing length. If an edge joins 2 nodes in different connected components, we add it to t.
Consequently, the 2 connected components now form a simple one. Otherwise the edge is
rejected.

To construct a minimal spanning tree, we use the following procedure.


1) Arrange all edges in the increasing order of weight
2) Select an edge with minimum weight. This is the first edge of spanning tree T to be
constructed.
3) Select the next edge with minimum weight that do not form a cycle with the edges
already included in T.
4) Continue step 3 until T contains (n-1 edges, where n is the number of vertices of G.
Arranging the edges in increasing order of their weights.

Edge Cost
{1,6} 10
{3,4} 12
{2,7} 14
{2,3} 16
{7,4} 18
{5,4} 22
{7,5} 24
{6,5} 25
{1,2} 28

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
30

Figure 4) Stages in Kruskal’s algorithm

Algorithm Kruskal(E,cost,n,t)
{
Construct a heap out of the edge costs using Heapify;
for i:= 1 to n do
parent[i]:= -1;
i:=0;
mincost := 0.0;
while ((i<n-1) and heap not empty)) do
{
delete a minimumcost edge (u,v) from heap;
and reheapify using Adjust;
j:=find(u);
k:=find(v);
if (j!=k) then
{
i:=i+1;
t[i,1]:=u;
t[i,2]:=v;
mincost := mincost+cost[u,v];
union(j,k);
}
}
if (i!= n-1) then
write (“no spanning tree”);
else
return mincost;
}

The computing time of Kruskal‟s algorithm is O(E log n).


Where E is the number of edges.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
31

Tracing of the Kruskal’s algorithm for MST


Edge Cost
{1,6} 10
{3,4} 12
{2,7} 14
{2,3} 16
{7,4} 18
{5,4} 22
{7,5} 24
{6,5} 25
{1,2} 28

Initialization of all vertices as roots


parent -1 -1 -1 -1 -1 -1 -1
vertex 1 2 3 4 5 6 7

i=0
mincost=0
(u,v)=(1,6) with a cost of 10 Tree matrix
j=find(1) = 1 t 1 6
k=find(6) = 6
as j!=k include the edge in the spanning tree
i=1
t[1,1]=1
t[1,2]=6
mincost=0+10=10
union(1,6)

parent -1 -1 -1 -1 -1 1 -1
vertex 1 2 3 4 5 6 7

i=1
mincost=10
(u,v)=(3,4) with a cost of 12 Tree matrix
j=find(3) = 3 t 1 6
k=find(4) = 4 3 4
as j!=k include the edge in the spanning tree
i=2
t[2,1]=3
t[2,2]=4
mincost=10+12=22
union(3,4)

parent -1 -1 -1 3 -1 1 -1
vertex 1 2 3 4 5 6 7

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
32

i=2
mincost=22 Tree matrix
(u,v)=(2,7) with a cost of 14 t 1 6
j=find(2) = 2 3 4
k=find(7) = 7 2 7
as j!=k include the edge in the spanning tree
i=3
t[3,1]=2
t[3,2]=7
mincost=22+14=36
union(2,7)

parent -1 -1 -1 3 -1 1 2
vertex 1 2 3 4 5 6 7

i=3
mincost=36
(u,v)=(2,3) with a cost of 16 Tree matrix
j=find(2) = 2 t 1 6
k=find(3) = 3 3 4
as j!=k include the edge in the spanning tree 2 7
i=4 2 3
t[4,1]=2
t[4,2]=3
mincost=36+16=52
union(2,3)

parent -1 -1 2 2 -1 1 2
vertex 1 2 3 4 5 6 7

i=4
mincost=54
(u,v)=(7,4) with a cost of 18
j=find(7) = 2
k=find(4) = 2
as j=k inclusion of this edge (7,4) forms a cycle in the MST so we discard this edge

parent -1 -1 2 3 -1 1 2
vertex 1 2 3 4 5 6 7

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
33

i=5
mincost=54 Tree matrix
(u,v)=(5,4) with a cost of 22 t 1 6
j=find(5) = 5 3 4
k=find(4) = 3 2 7
as j!=k include the edge in the spanning tree 2 3
i=5 5 4
t[5,1]=5
t[5,2]=4
mincost=52+22=74
union(5,4)

parent -1 -1 2 3 4 1 2
vertex 1 2 3 4 5 6 7

i=5
mincost=74
(u,v)=(7,5) with a cost of 24
j=find(7) = 2
k=find(5) = 2
as j=k inclusion of this edge forms a cycle discard this edge

parent -1 -1 2 3 4 1 2
vertex 1 2 3 4 5 6 7

i=5
mincost=74 Tree matrix
(u,v)=(6,5) with a cost of 25 t 1 6
j=find(6) = 1 3 4
k=find(5) = 2 2 7
as j!=k include the edge in the spanning tree 2 3
i=6 5 4
t[6,1]=6
6 5
t[6,2]=5
mincost=74+25=99
union(6,5)

parent 6 -1 2 3 4 5 2
vertex 1 2 3 4 5 6 7

The minimum cost spanning tree is with 99

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
34

Kruskal’s Algorithm

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
35

SINGLE SOURCE SHORTEST PATH PROBLEM


Let G=(V,E) be a directed graph with weighting function w for the edges of G. The starting
vertex of the path is called the source and the last vertex is called the destination. Let v be any
other vertex which belongs to set of vertices V. The problem to determine a shortest path to
given destination vertex v from source is called single source shortest path problem.

Dijkstra’s Algorithm
Algorithm shortestpath(v,cost,dist,n)
// dist[i], 1<=i<=n is the distance or short path starting from source passing through the
// vertices that are in S and ending at i
{
for i:= 1 to n do
{
S[i]:=0; //1 for true ; 0 for false
dist[i]:= cost[v,i];
}

S[v]:=1; //1 for true 0 for false


dist[v]:=0;

for k:= 2 to n-1 do


{
choose u from among those vertices not in S such that dist[u] is minimum;
S[u]:=1; // put u in S.
for (each w adjacent to u with s[w] =0) do
{
if (dist[w] > dist[u]+ cost[u,w]) then
dist[w]:=dist[u]+cost[u,w];
}
}
}

Example 1) Find shortest path from node 1 to all other nodes

Path length
1,2 4
1,3 2
1,3,4 3
1,3,4,5 6

If 1 is the source vertex, the shortest path from 1 to 5 is 6. The shortest path from 1 to all
other vertices are given in the table.

The greedy method to generate shortest paths from source vertex to the remaining vertices is
to generate these paths in increasing order of path length.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
36

According to Dijkstra‟s algorithm, first we select a source vertex and include that vertex in
the set S. To generate the shortest paths from source to the remaining vertices a shortest path
to the nearest vertex is generated first and it is included in S. Then a shortest path to the
second nearest vertex is generated and so on. To generate these shortest paths we need to
determine,

1) The next vertex to which a shortest path must be generated.


2) A shortest path to this vertex.
The first for loop takes O(n) time. Each executin of second for loop requires O(n) time to
select the next vertex and again at the for loop to update dist. So that total time for this loop is
O(n2). Therefore time complexity for this algorithm is O(n2).

interaction set Vertex Distance


selected 1 2 3 4 5
initial -- -- 0 4 2 ∞ 8
1 {1} 3 0 4 2 3 8
2 {1,3} 4 0 4 2 3 6
3 {1,3,4} 2 0 4 2 3 6
4 {1,3,4,2} 5

Tracing the Algorithm

S[1]=0,S[2]=0,S[3]=0,S[4]=0,S[5]=0
dist[1]=cost[1,1]=0
dist[2]=cost[1,2]=4
dist[3]=cost[1,3]=2
dist[4]=cost[1,4]=∞
dist[5]=cost[1,5]=8

Initially set S is empty. i.e. S={}


as we want to found shortest distance for node 1 to all other nodes the source node i.e node 1
is included in the set S
S={1}
We search for the nearest node from 1 which is node 3.
Node 3 is included in the set i.e. S={1,3}
Now find all adjacent vertices of node 3 other than in set S..
Node 4 is adjacent of node 3
if (dist[4] > dist[3]+ cost[3,4]) then
dist[4]:=dist[3]+cost[3,4];
the nearest node is selected and added to S.
S={1,3,4}
Usually this is repeated for all the adjacent vertices other than the nodes in S.
Now find all adjacent nodes of 4 other than the nodes in S.
Node 5 is adjacent
The dis[5] is modified
The node with smallest dist is selected.
Node 2 is selected and added to S
S={1,3,4,2}
The remaining node is 5
S={1,3,4,2,5}
The paths from 1 to all other nodes is shown in the spanning tree.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
37

Example 2) Single source shortest path problem: Find shortest path from node 1 to all other
nodes.

Path Length
1,4 10
1,4,5 25
1,4,5,2 45
1,3 45

Consider the above directed graph. The numbers on the edges are weights. If
node 1 is the source vertex, then the shortest path from 1 to 2 is 1-4-5-2. The length of
this path is 10 + 15 + 20 = 45. Even though there are three edges on this path it is
shorter than the path 1,2 which is of length 50. There is no path from 1 to 6.

To formulate greedy based algorithm to generate shortest paths, we must


conceive of a multi stage solution to the problem and also of an optimization measure.
One possibility is to build the shortest paths one by one. As an optimization measure
we can use the sum of the lengths of all paths so far generated.

The algorithm known as Dijkstra‟s algorithm determines the lengths of the


shortest paths from V0 to all other vertices in G. it is assumed that the n vertices are
numbered from 1 through n. The set S is maintained as a bit array with S[i]=0 if
vertex I is not in S and S[i]=1 if it is.

It is assumed that the graph itself is represented by its cost adjacency matrix
with cost[I,j] being the weight of the edge <i,j>. The weight cost[i,j] is set to some
large number, ∞, in case the edge <i,j> is not in E(G). For i=j cost[i,j] can be set to
nonnegative number without affecting the outcome of the algorithm.

The time taken by the algorithm on a graph with n vertices is O(n2).

interaction set Vertex Distance


selected 1 2 3 4 5 6
initial -- -- 0 50 45 10 ∞ ∞
1 {1} 4 0 50 45 10 25 ∞
2 {1,4} 5 0 45 45 10 25 ∞
3 {1,4,5} 2 0 45 45 10 25 ∞
4 {1,4,5,2} 3 0 45 45 10 25 ∞
Shortest paths from 1 in increasing order
1-4=10
1-5=1-4-5=25
1-2=1-4-5-2=45
1-3=45

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
38

Spanning tree which shows shortest paths from node 1

In Divide and Conquer approach, a problem is divided recursively into sub problems of same
kind as the original problem, until they are small enough to be solved and finally the
solutions of the sub problems are combined to get the solution of the original problem. In
Greedy approach, a problem is solved by determining a subset to satisfy some constraints. If
that subset satisfies the given constraints, then it is called as feasible solution, which
maximizes or minimizes a given objective function. A feasible solution that either
maximizes or minimizes an objective function is called as optimal solution.

Single Source Shortest Path Problem

There are many paths from A to H.


For example length of path A F D E H = 1 + 3 + 4 + 6 = 14
A B C E H = 2 + 2 + 3 + 6 = 13
We may further look for a path with length shorter than 13 if exists.

Algorithm:
1. We start with source vertex A.
2. We locate the vertex closest to it. B F are adjacent vertices. Length
of AF < length of AB so we choose F.

3. Now we look for all the adjacent vertices excluding the just earlier
vertex of newly added vertex and the remaining adjacent vertices of earlier vertices, i.e.,
we have D,E and G (as adjacent vertices of F) and B (as remaining adjacent vertex of A).
Vertices that may be attached Path from A Length
D AFD 4
E AFE 4
G AFG 6
B AB 2
We choose vertex B.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
39

4) We go back to step 3 and continue till we exhaust all the vertices.


Vertices that may be attached Path from A Length
ABD 4
D
AFD 4
G AFG 6
C ABC 4
E ABE 6
B AFE 4
We may choose D, C or E.
We choose say D through B.
Vertices that may be Path from A Length
attached
G AFG 6
C ABC 4
AFE 4
E ABE 6
BDE 8
We may choose C or E, choose C.

Vertices that may be attached Path from A Length


G AFG 6
AFE 4
ABE 6
E
ABDE 8
ABCE 7
H ABCH 5
We choose E via AFE.

Vertices that may be attached Path from A Length


AFG 6
G
AFEG 11
ABCH 5
H AFEH 10

We choose H via ABCH.

Vertices that may be attached Path from A Length


AFG 6
AFEG 11
G

We choose path AFG.


Therefore the shortest paths from source vertex A to all the other vertices are
AB
ABC
ABD
ABCH
AF
AFE
AFG.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
40

S.No Divide & Conquer Method Greedy Method


1 Divide and conquer approach is a result By Greedy method, there are some
oriented approach chances of getting an optimal solution to
a specific problem
2 The time taken by this algorithm is efficient The time taken by this algorithm is not
when compared by greedy method. that much efficient when compared to
divide-and-conquer approach.
3 This approach does not depend on This approach cannot make further
constraints to solve a specific problem move, if the subset chosen does not
satisfy the specified constraints.
4 This approach is not efficient for larger This approach is applicable and as well
problems as efficient for a wide variety of
problems.
5 As the problem is divided into large Space requirement is less when
number of sub problems, the space compared to the divide-and –conquer
requirement is very much large approach.
6 This approach is not applicable to problems This problem (Knapsack) is rectified in
which are not divisible. Example Knapsack the greedy method.
problem

Example ) Prove that any weighted connected graph with distinct weights has exactly one
minimum spanning tree.

We may get so many spanning trees if weights are equal. If the weights of the connected
graph are all distinct, then the minimum spanning tree is unique.

Optimal Merge Patterns


Merge a set of sorted files of different length into a single sorted file. We need to find an optimal solution, where
the resultant file will be generated in minimum time.
If the number of sorted files are given, there are many ways to merge them into a single sorted file. This merge
can be performed pair wise. Hence, this type of merging is called as 2-way merge patterns.
As, different pairings require different amounts of time, in this strategy we want to determine an optimal way of
merging many files together. At each step, two shortest sequences are merged.
To merge a p-record file and a q-record file requires possibly p + q record moves, the obvious choice being,
merge the two smallest files together at each step.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
41

Two-way merge patterns can be represented by binary merge trees. Let us consider a set of n sorted files {f1, f2,
f3, …, fn}. Initially, each element of this is considered as a single node binary tree. To find this optimal solution,
the following algorithm is used.
Algorithm: TREE (n)
for i := 1 to n – 1 do
declare new node
node.leftchild := least (list)
node.rightchild := least (list)
node.weight) := ((node.leftchild).weight) + ((node.rightchild).weight)
insert (list, node);
return least (list);
At the end of this algorithm, the weight of the root node represents the optimal cost.

Example
Let us consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30 number of elements respectively.
If merge operations are performed according to the provided sequence, then
M1 = merge f1 and f2 => 20 + 30 = 50
M2 = merge M1 and f3 => 50 + 10 = 60
M3 = merge M2 and f4 => 60 + 5 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Hence, the total number of operations is
50 + 60 + 65 + 95 = 270
Now, the question arises is there any better solution?
Sorting the numbers according to their size in an ascending order, we get the following sequence −
f4, f3, f1, f2, f5
Hence, merge operations can be performed on this sequence
M1 = merge f4 and f3 => 5 + 10 = 15
M2 = merge M1 and f1 => 15 + 20 = 35
M3 = merge M2 and f2 => 35 + 30 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Therefore, the total number of operations is
15 + 35 + 65 + 95 = 210
Obviously, this is better than the previous one.
In this context, we are now going to solve the problem using this algorithm.

Initial Set

Step 1

Step 2

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method
42

Step 3

Step 4

Hence, the solution takes 15 + 35 + 60 + 95 = 205 number of comparisons.

TVVK III CSE --- DAA UNIT-II Divide & Conquer and Greedy Method

You might also like