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

Daa 3

The document discusses the Divide and Conquer strategy, detailing its principles, steps, and control abstraction, along with examples such as binary search and finding maximum and minimum values in a dataset. It explains the recursive nature of the method and provides algorithms for various problems, emphasizing the analysis of complexity through recurrence relations. Additionally, it highlights the efficiency of the approach in solving complex problems by breaking them down into smaller, manageable subproblems.

Uploaded by

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

Daa 3

The document discusses the Divide and Conquer strategy, detailing its principles, steps, and control abstraction, along with examples such as binary search and finding maximum and minimum values in a dataset. It explains the recursive nature of the method and provides algorithms for various problems, emphasizing the analysis of complexity through recurrence relations. Additionally, it highlights the efficiency of the approach in solving complex problems by breaking them down into smaller, manageable subproblems.

Uploaded by

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

Unit –II

Divide and Conquer:- The general method, Binary search, Finding


maximum and minimum, Merge sort, Quick sort and selection sort,
STRASSEN’S Matrix Multiplication.
Greedy Method:- Knapsack Problem, Tree vertex splitting, Optimal
Storage on tapes, Job sequencing with deadlines, Optimal Merge
Pattern, Minimum Spanning Trees and Single source shortest paths.

Q) Explain the divide-conquer strategy with the help of an example.


Q) Explain the techniques of Divide and Conquer with the help of an
example.
Ans:-
Divide and Conquer Strategy:-
The principle behind this strategy is that it is easier to solve several small
instances of a problem than one large complex problem. The “divide - and -
conquer” technique involves in solving a particular computational problem by
dividing it into smaller sub problems, solving the problem recursively and then
combining the results of all the sub problems to produce the result for the
original complex problem ‘P’.
The strategy involves three steps at each level of recursion.
1. Divide:- Divide the problem into a number of sub problems.
2. Conquer:- Conquer the sub problems by solving them recursively. If the
sub problem sizes are small enough, then just solve the sub problems in a
straight forward manner.
3. Combine:- Combine the solutions to the sub problems to form the
solution for the original problem.

Let ‘n’ represent the size of the original problem. Let S(n) denote this problem.
We solve the problem S(n) by solving a collection of k sub problems- S(n 1),S(n2),
…S(nk), where ni<n for i=1,2,..k. Finally we merge the solutions to these
problems.

Skeleton of Divide and conquer Startegy:-


Let ‘n’ be the maximum size of the problem. The number of smaller
instances into which the input is divided is k. For an input of size ‘n’ Let
B(n) – no. of steps done to solve directly.
D(n) – no. of steps done by divide and let
C(n) – no. of steps done by combine.
Then the general form of the recurrence equation describes the amount of
work done by the algorithm which is

T(n)=D(n)+ for n > smallsize.

Solve(I)
n= size(I);
if(n≤ smallsize)
solution=directlysolve(I);
else
divide I into I1,..Ik;
for each i I1,..Ik
Si=Solve(Ii);
solution=Combine(S2,…Sk);
return solution.

Q) Write and explain the control abstraction for divide- and- conquer
method.
Q) Give the control abstraction of divide-and-conquer.
Ans:-
Control Abstraction For Divide and Conquer:-
A control abstraction is a procedure that reflects the way an actual
program based on DAndC will look like. A control abstraction shows clearly the
flow of control but the primary operations are specified by other procedures. The
control abstraction can be written either iteratively or recursively.
If we are given a problem with ‘n’ inputs and if it is possible for splitting
the ‘n’ inputs into ‘k’ subsets where each subset represents a sub problem
similar to the main problem then it can be achieved by using divide and conquer
strategy.
If the sub problems are relatively large then divide and conquer strategy
is reapplied. The sub problem resulting from divide and conquer design are of
the same type as the original problem. Generally divide and conquer problem is
expressed using recursive formulas and functions.
A general divide and conquer design strategy(control abstraction) is
illustrated as given below-
Algorithm DAndC (P)
{
if small(P) then return S(P) //termination condition
else
{
Divide P into smaller instances P1, P2, P3… Pk k≥1; or 1≤k≤n
Apply DAndC to each of these sub problems.
Return Combine (DAndC(P1), DAndC (P2), DAndC (P3)…
DAndC (Pk)
}
}
The above blocks of code represents a control abstraction for divide and
conquer strategy. Small (P) is a Boolean valued function that determines
whether the input size is small enough that the answer can be computed without
splitting. If small (P) is true then function ‘S’ is invoked. Otherwise the problem
‘P’ is divided into sub problems. These sub problems are solved by recursive
application of Divide-and-conquer. Finally the solution from k sub problems is
combined to obtain the solution of the given problem.

If the size of ‘P’ is ‘n’ and if the size of ‘k’ sub problems is n 1,n2,…. nk
Respectively then the computing time of DAndC is described by the recurrence
relation.

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


= T(n1)+ T(n1)+ T(n2)+…….+ T(nk)+ f(n) otherwise.
T(n) denotes the time for DAndC on any input of size ‘n’.
G(n) is the time to compute the answer directly for small inputs.
F(n) is the time for dividing ‘P’ and combining the solutions of sub
problems.

Q) Give the analysis of the complexity of the divide and conquer


strategy.
Ans:-
To analyze the running time of a divide and conquer algorithms we make
use of a recurrence relation. A function T(n) denotes the running time of the
algorithm on an input of size ‘n’ and characterize T(n) using an equation that
relates T(n) to values of the function T for problem sizes smaller than n.
The recurrence relations are of the form.

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


= aT(n/b)+f(n) n>1
where a,b are known constants. We assume that T(1) is known and nis a
power of b (i.e., n=bk).

Iterative Substitution Method (plug-and-chug Method):-


A method that repeatedly makes substitution for each occurrence of the
function T in the right-hand side until all such occurrences disappear is called
substitution method.
In this method we assume that the problem size ‘n’ is fairly and we then
substitute the general form of the recurrence for each occurrence of the function
T on the right hand side. By using this technique we can determine a general
pattern for T(n).

Example:-
Consider the case in which a=2 and b=2. Let T(1)=2 and f(n)=n. then we
have

T(n) = 2T(n/2)+n
= 2[2T(n/4)+n/2]+n
= 4T(n/4)+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+3n
= .
.

= 2i T(n/2i)+in , for any log 2 n ≥ I ≥ 1.


= 2 log 2 nT(n/ log 2 n)+n log 2 n, corresponding to the
choice of i= log 2 n. Thus
T(n) = n T(1)+n log 2 n
= n log 2 n+2n
Q) Write an algorithm for performing binary search for any element in
an array.
Ans:-
Problem:- Let ai, 1≤ I ≤ n , be a list of elements that are sorted in non
decreasing order. We need to determine whether a given element ‘x’ is present
in the list. If ‘x’ is present, then ‘j’ is determined such that a j=x. if x is not in the
list, then j is to be set to zero.
Solution:- Every call to the binary search splits the partition into two in the
middle according to the formula
Mid = where low is the index of the first element
of the partition and high is the last element of the partition. Generally the ‘n’
inputs are sorted in A[1..n] array. The splitting process terminates when the size
of the partition becomes ‘1’ or required element is found.

Algorithm BinSearch (A, l , h, x)


//Given an array A[l :h ] of elements in non decreasing order , 1≤ l ≤ h
//Determine whether x is present, and if so return j such that x=A[j]; else return
0.
{
if (l=h) then
{
If(x=A[h]) then return h; //If Small (P)
else return 0;
}
else
{//reduce P into a smaller sub problem.
Mid :=[(l +h)/2];
If(x=A[mid]) then return mid;
Else if(x<A[mid]) then
Return BinSearch (A, l, mid-1, x);
Else return BinSearch (A, mid+1, h, x);
}
}

The above algorithm is a recursive binary search algorithm.

Explanation:
The problem is subdivided into smaller problems until only one single
element is left out.
1. If low = high then it means there is only one single element. So compare
it with the search element ‘x’. if both are equal then return the index, else
return 0.
2. If low  high then divide the problem into smaller subproblems.
a. Calculate mid = (low+high)/2.
b. Compare x with element at mid if both are equal the return index
mid.
c. Else if x is less than A[mid] then the element x would be in the first
partition A[l:mid-1]. So make a recursive call to BinSearch with low
as l and high as mid-1.
d. Else x is greater than A[mid] then the element x would be in the
second partition A[mid+1: h]. So make a recursive call to BinSearch
with low as mid+1 and high as h.

Algorithm BinSearch (a, n, x)


//Given an array a[1:n] of elements in non decreasing order, n≥0,
//determine whether x is present, and if so return j such that x=a[j]; else return
0
{
low:=1;
high:=n;
while(low ≤ high) do
{
mid:=[(low+high)/2];
if(x<a[mid])then high:=mid-1;
else if(x>a[mid])then low:=mid+1;
else return mid;
}
return 0;
}

The above algorithm is an iterative algorithm for Binary search.

Analysis:-

If n is in the range [2 k-1,2k] , then BinSearch makes at most k element


comparisons for a successful search and either k-1 or k comparisons for an
unsuccessful search.
The algorithm can be better understood with the help of a decision tree.

Example:-

The keys are

-15,-6,0,7,9,23,25,54,82,101,112,125,131,142,151.

The no. of comparisons needed for a successful search for the above
given keys can be determined using a decision tree as follows. A decision tree is
drawn using the values of mid for every call. Circle( O ) represents an internal
node and rectangle represents an external node.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-15 -6 0 7 9 23 25 54 82 101 112 125 131 142 151
Low mi high
d
7

1
3 1

1 5 9 13

1 1
2 4 6 8 0 12 4

Each and every internal node represents a successful search and the
values denoted by internal nodes are the various values taken by the variable
mid. If the value of mid is at level zero then the no. of comparisons for an
element present at the position is one.

The no. of comparisons for a successful search if the mid value is at level
one is two and the max. No. of comparisons for a successful search in the above
decision tree is equal to four. i.e., the max. No. of comparisons is directly
proportional to the height of the tree. Therefore the time complexity for a
successful search is given by O (log2 n).
Each and every unsuccessful search terminates at an external node. The
no. of comparisons needed for an unsuccessful search of an element less than -
15 is 3. For the entire remaining unsuccessful search the no. of comparisons
made is 4. Therefore the average no. of elemental comparisons of an
unsuccessful search is

Except for one case the no. of comparisons for unsuccessful search is
constant and is equal to the height of the tree. Therefore the time for
unsuccessful search is given by O (log2 n).

Finding the Maximum and Minimum: -


Q) Write an algorithm which uses divide and conquer method for finding the
maximum and minimum items in a set of elements.
Q) Develop a recursive algorithm for finding the maximum and minimum items
in a given set of elements, also give the time complexity of the algorithm.

Ans:-
Problem: - ‘n’ inputs are given which are stored in a global array and we need
to find non and min of those elements.

The problem could be solved by applying divide and conquer and splitting the
problem in to sub-problems until the size of the partition is sufficiently small i.e.
until small (p) is satisfied. The splitting of the partition will terminate in two
cases.

i) When the size of the partition is 1 i.e. (i=j)


ii) When the size of the partition is 2 i.e. (i=j-1)
(That is i is one less than j) // two elements lie on the partition.
Design:-
Algorithm MaxMin (i, j, max, min)
//a[i=n] is a global array i and j are integers
//1≤i≤j ≤n. the effect is to set man and
// min to largest and smallest value in a (i=j) resp.
{
if (i=j) then
max:=min:=a(i); //small(p)
else if (i=j-1) then
{
if (a(i) < a(j)) then
{
max:=a(j) ; min:=a(i);
}
else
{
max:=a[i];min:=a[j];
}
}
else
{
//if p is not small, divide p
// into sub problems
mid:=[(i+j)/2];
//solve the sub problems
MaxMin (I,mid,max,min);
MaxMin (mid+1, j, max 1, min 1);
If (min>min1) then min:=min1;
}
}

Example:-
 Simulate the algorithm Max min for the following elements:

22, 13, -5, -8, 15, 60, 17, 31, 47

1 2 3 4 5 6 7 8 9
a 22 13 -5 -8 15 60 17 31 47

Max Min (1, n, x, y)


Max Min

(9)
I, 9, 60, -8

(7) (8)
1, 5, 22, 8 6, 9, 60, 17

1, 3, 22, -5 4, 5, 15, -8 6, 7, 60, 17 8, 9, 47, 31

(3) (4) (5) (6)

1, 2, 22, 13 3, 3, -5, -5

(1) (2)

The circled numbers in the upper left corner of each node represent order in which the
variables Max and Min are assigned values.

Q) Prove that the complexity of max-min algorithm using divide and conquer is T(n)=
where n, the number of elements is a power of two.

Analysis: - The no. of elemental comparisons required by the Max Min algorithm is given by
the recurrence relation.

T(n) = T [n/2] + T[n/2]+2 if n>2


1 if n=2
0 if n=1

If ‘n’ is power of 2 than we have

T(n) = 2T = 2.Ty

=2 =2

=4T = 4.T

= 4.
= 8.T

= 2x-1 T (2) +

= 2 k-1+2k – 2 = 2k-1 (1)+(2+4+8+16+32+…)


≤ = 2k-1+
= 2k-1+2k-2
n/2 +n-2 = n+2n-4 = = 2 k/4 + 2k-2 If p is power of 2.

The no/: of comparisons in the best and worst and average case for the algorithm Max Min
is given by 3n/2 – 2

Q) Describe about Merge sort.


Q) Explain how divide and conquer strategy can be applied for merge sort, and
develop an algorithm for the same. Give its time and space complexity.
Ans:-Merge Sort: -
The divide and conquer strategy for the merge sort divides the problem with ‘n’
inputs into ‘k’ sub problems by dividing each partition recursively in the middle until the ‘p’
(small) is satisfied i.e. until the size of the partition is one. Then the merge algorithm will
merge the sub problems recursively until the solution for the entire problem is being
obtained.

The merge algorithm is applied on two sub lists which are in sorted order.
1. Divide:- Divide the n-element sequence to be sorted into two sub sequences of n/2
elements each.
2. Conquer:- Sort the two subsequences recursively using merge sort.
3. Combine:- Merge the two sorted subsequences to produce the sorted answer.

Q) Write an algorithm to do merge sort for the given array of elements.


Q) Write an algorithm for merge sort.
Q) Given two sorted lists of size ‘m’ and ‘n’ respectively. To merge these lists using
merge sort how many comparisons are needed? Justify your answer.
Q) Describe about Merge sort with algorithm and suitable example.
Q) Explain the features of Merge sort as an example algorithm in the paradigm of
divide and conquer. Illustrate the algorithm with an example of ten items to be stated.

Algorithm Merge sort (low, high)


// a (low: high) is a global array to be sorted
// small (p) is true if there is only one element
// to sort, which means the list is already sorted
{ // if (low<high) then // if there is more than one element
{ // Divide p in to sub problems
mid:= ;
// solve the sub problems
Merge sort (low, mid);
Merge Sort (mid+1, high);
// combine solutions
Merge (low, mid, high)
}
}

Simulate divide strategy of merge sort on the following keys

310, 285, 179, 652, 351, 423, 861, 254, 450, 520

The tree of calls initiated by the merge sort will be as follows.

1, 10

1, 5 6, 10

1, 3 4, 5 6,8 9, 10

1, 2 3, 3 4, 4 5, 5 6, 7 8, 8 9, 9 10,
10

1,1 2, 2 6, 6 7, 7

(1) a(1,1) is merged with a(2,2) to give a (1,2)

285, 310/ 179/ 652, 351/ 423, 861, 254, 450, 520

(2) a(1,2) is merged with a (3,3) to give a (1,2,3)

179, 285, 310/ 652, 351/ 423, 861, 254, 450, 520

(3) a (4,4) is merged with a (5,5) to give a (4,5)


179, 285, 310/ 351, 652/ 423, 861, 254, 450, 520
(4) a (1,3) is merged with a (4,5) to give a (1,5)
179, 285, 310, 351, 652/ 423, 861, 254, 450, 520

At this point the algorithm merge sort will return to the first invocation and is about to
process the second recursive call repeated recursive calls are invoked producing the
following sub-arrays.

(5) a(6,6) is merged with a(7,7) to give a(6,7)


179, 285, 310, 351, 652/423, 861/ 254/ 450, 250
(6) a(6,7) is merged with a(8,8) to give a (6,8)
179, 285, 310, 351, 652/ 254, 423, 861/ 450, 520
(7) a (9,9) is merged with a(10,10) to give a (9,10)
179, 285, 310, 351, 652/254, 423, 861/ 450, 520
(8) a (6,8) is merged with a (9,10) to give a (6,10)
179, 285, 310, 351, 652/ 254, 423, 450, 520, 861
(9) a (1,5) is merged with a (6,10) to give a (1,10)
179, 254, 285, 310, 351, 423, 450, 520, 652, 861

 Algorithm Merge (low, mid, high)


// a (low: high) is a global array containing
// two sorted subsets in a (low: mid) and in
// a (mid+1, high). The goal is to merge these two
//subsets in to a single set residing in a (low: high)
// b[ ] is a temporary global array.
{
h:=low, i:=low; j=mid+1;
while ((h≤mid) and (j≤ high)do
{ if (a(h) ≤ a(j)) then
{ b(i) = a(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(x);
}
 Simulate merge sort on the following sub list.

Merge (low, mid, high)


H=1, i=1, j=6, mid=5, high=10
While ((h ≤ mid) and (j ≤ high))

Left sub list Right sub list


no 1 executed No. 1 executed

h=2, i=2, j=6 mid =5, high =10


h=2, i=3, j=7 mid =5, high =10
h=3, i=4, j=7 mid =5, high =10
h=4, i=5, j=7 mid =5, high =10
h=5, i=6, j=7 mid =5, high =10
h=5, i=7, j=8 mid =5, high =10
h=5, i=8, j=9 mid =5, high =10
h=5, i=9, j=10 mid =5, high =10
h=6, i=10, j=10 mid =5, high =10

Out of while (h ≤mid) – false


K=10 to (j to high)
i.e. 861 is copied to b

b 17 28 31 35 65 25 42 45
520 861
9 5 0 1 2 4 3 0
1 2 3 4 5 6 7 8 9 10

Tree of calls of merge

1, 1, 2 6, 6, 7

1, 2, 3 4, 4, 5 6, 7, 8 9, 9, 10

6, 8, 10
1, 3, 5

1, 5, 10
Q)By means of an example demonstrate how merge sort works. Derive
the timing and space complexities of the same.
Analysis: -

If the time for merging operation is proportional to ‘n’ then the computing time
for merge sort is described by the recursion relation.
a n=1, a is constant
T(n) 2 T(n/2) + cn, n>1, c is constant

When n is power of 2 then

T(n) =2 + Cn

= 4T + Cn + C n

= 4T + 2 Cn

=8T + 3cn

= 2k T(1) + Kcn 2k = n
= an+cn logn k = log2 n
k K+1
2 <n≤2
Then T(n) ≤ T (2 K+1)

T(n) = 0 (n log2n)
Q)Give the quick sort algorithm and illustrate the method with suitable
example. Also derive the time complexity for an average case.
Q) Describe quick sort algorithm and explain it with an example.
Discuss its time complexity.
Q) Develop an algorithm for quick sort using divide and conquer
strategy. Find its time and space complexity both for average and
worst cases.

Q) Give an algorithm for quick sort.Analyse its complexity. On what


input data does quick sort exibhit its best and worst behavior.
Q) Give the partition and Quick sort algorithms and analyse them in
detail.

Q) Show how quick sort sorts the following sequence of keys.


5 5 8 3 4 3 2.
Q) Show how procedure QUICKSORT sorts the following sets of keys:
(1,1,1,1,1,1,1) and (5,5,8,3,4,3,2).
Q) Write and explain the Quick sort Algorithm.
Q) Describe about quicksort.
Q) Derive the average-case timing complexity of quicksort.
Q) Do the average case analysis of the quicksort algorithm.

Quick Sort: -
In the divide and conquer of quick sort we recursively divide the inputs
into sub lists until the small (p) condition is occurred. The partitions in this are
formed unlike the other problems involved in divide and conquer. In every
execution of quick sort the partition location ‘j’ is determined where ‘j’ is the
location of an element place in its sorted order. Then we form two partitions
from 1st element of the partition to j-1 and j+1 to the lout element of the
partition.
 Steps in Partition algorithm: -

Step - 1: - Initially we take ‘p’ pointing to the first element and q pointing to the
last element i.e. p is the location of 1 st element q is the location of last element.

Step - 2: - Initialize i:=p and j:q+1

Step - 3: - Increment I until we find an element which is greater than the


element present at location ‘p’

Step - 4: - Decrement j by 1 until we find an element which is less than the


element present at location ‘p’

Step - 5: - If i<j then swap (a(i), a(j))

Step - 6: - If i>j swap (a(i), a(p))

Step – 7: - If i>j then we get two partitions i.e. two sub lists from (p,j-1) and
(j+1, q)

Step – 8: - Solve each sub problem recursively until p becomes greater than q

Algorithm Quick sort (p, q)


// sorts the elements a(p)……., a(q)
// which reside in global array a(1:n) in to assending
// order; a (n+1) is considered to be defined and must be
//  all the elements in a (1,n)
{ if (p<q) then // if there is more than one element
{//divide p into sub problems
j:= partition (a,p,q+1);
//j is position of partitioning element
//solve the sub problems
quicksort (p,j-1);
quicksort (j+1,q)
//need not combine the solutions
}
}

Algorithm partition (a,m,p)

{ v:=a(m); i=m; j:=p;


repeat
{ repeat
f= i+1;
until (a(i) v);
repeat
j:=j-1;
until (a(j) ≤v);
if (i<j) then interchange (a,i,j)
} until (i>j)
a (m):= a(j); a(j):=v; return j;
}
Algorithm Interchange (a,i,j)
//echaange a(i) with a (i)
{ p: = a(i);
a(i): = a(j);
a(j): = p;
}

Example:
26, 5, 37, 1, 61, 11, 59, 15, 48, 19
1 2 3 4 5 6 7 8 9 10
26, 5, 37, 1, 61, 11, 59, 15, 48, 19
ip q j (q+1)

26, 5, 37 , 1, 61, 11, 59, 15, 48, 19 i<j


i j Swap a (i) a(j)

26, 5, 19, 1, 61 , 11, 59, 15 , 48, 37 i<j

i j Swap a(i), a(j)

26, 5, 19, 1, 15, 11 , 59 , 61, 48, 37 (i>j)


Swap a(i), a(j)
j i

[11, 5, 19, 1 , 15,] 26, [59, 61, 48, 37 ] i< j

Ii j p i j i< j

Partition

[11, 5, 1 19 15] 26 [59, 37 48 61 ] i>j

i j j i I>j
[1, 5] 11 [19 15] 26 [ 48, 37] 58 [61]
1 5 11 15 19 26 37 48 59 61
Analysis: -
In analyzing the quick sort we count the no/: of element comparisons. Quick sort
requires O (n2) comparisons in the worst case and 0 (along) comparisons in the
average case.

Q) Explain the general method of greedy strategy.


GREEDY METHOD

It is used to solve problems that have ‘n’ inputs and require us to obtain a
subset that satisfies some constraints. Any subset that satisfied the constraints
is called as a feasible solution. We need to find the optimum feasible solution i.e.
the feasible solution that optimizes the given objective functions.
The greedy method suggests that one can divide the algorithm that works in
stages. Considering one input at a time. At each stage a decision is made
regarding weather or particular input is in the optimum solution. For this purpose
all the inputs must be arranged in a particular order by using a selection
process. If the inclusion of next input in to the partially constructed solution will
result in an infeasible solution then the input will not be considered and will not
be added to partially constructed set otherwise it is added.

Q) Give the control abstraction of greedy class of algorithms and


explain the features.

CONTROL ABSTRACTION FOR GREEDY METHOD: -

Algorithm Greedy (a,n)

//a[1:n] contains the ‘n’ inputs


{ Solution: = 0; //initialize the solution
for i: = 1 to n do
{ x: = select (a);
if feasible (solution, x) then
solution: = Union (solution, x);
}
return solution;
}

Select is a function which is used to select an input from the set. Feasible is a
function which verifies the constraints and determines weather the resultant
solution is feasible or not. Union is a function which is used to add elements to
the partially constructed set.

Q) Explain the terms “feasible solution” and “Optimal solution” with


the help of an example.
Feasible Solution:-
Any subset of the solutions that satisfies the constraints of the problem is
known as a feasible solution.
Optimal Solution:-
The feasible solution that maximizes or minimizes the given objective
function is an optimal solution. Every problem will have a unique optimal
solution.

Q) Define the knapsack problem and give the greedy algorithm for it.
Also prove the correctness of the heuristic employed.
KNAPSACK PROBLEM: -

A knapsack with capacity ‘m’ is given in to which we are required to place


certain weights such that the sum of the weights will not exceed the knapsack
capacity. Associated with each weight we have associated profit which will be
earned by the inclusion of the object in to the knapsack.

If it is not possible to include an object entirely a fraction of the object can be


included and accordingly a fraction of the profit is earned.

Given ‘n’ objects and a bag of capacity ‘n’ and each object ‘i’ has a profit p i and
weight wi associated with it. If a fraction x j (o≤ x, ≤ 1) of the object i is placed in
the bad. A profit pi x xi is made. The objective in the problem is to obtain the
maximum profit by filling the bag with given objects.

The knapsack problem can be stated mathematically as follows.

Pi xi Maximize ∑ Pi xi
1≤i ≤n
S.T.C
∑ Pi xi ≤ m
1≤u≤n
o ≤ xi ≤ 1
1≤i≤n
Ex: - Consider 3 objects whose profits and weights are defined as

(P1, P2, P3) = ( 25, 24, 15 )


(W1, W2, W3) = ( 18, 15, 10 )
n=3 m=20
Consider a knapsack of capasing 20. Determine the optimum strategy for
placing the objects in to the knapsack. The problem can be solved by the greedy
approach where in the inputs are arranged according to selection process
(greedy strategy) and solve the problem in stages. The various greedy strategies
for the problem could be as follows.

(1) Greedy about profit: -

(x1, x2, x3) ∑ xiwi ∑ xipi


(1, 2/15, D) 18x1+ x15 25x1+ x24 8

= 20 25+ = 28.2
(2) Greedy about weight: -
5

(0, 2/3, 1) x15+10x1= 20 15x1+ x24 8 = 31


(3) Greedy about profit / unit weight: -

(0, 1, ½ ) 1x15+ x10 = 20 1x24+ x15 = 31.5


If an additional constraint of including each and every object is placed then the
greedy strategy could be

(4) (½, ⅓, ¼ ) ½ x 18+⅓ x15+ ¼ x10 = 16. 5


½ x 25+⅓ x24+ ¼ x15 = 12.5+8+3.75 = 24.25

Algorithm Greedy knapsack (m, n)


//p (1>n) and w(1:n) contain the profits and weights
//resp. of the n objects ordered such that p(i)/W(i)
// p[i+1)/w (i+1]. M is the knapsack size and x (1:n)
// is the solution vector
{
for i: = 1 to n do x(i) = 0.0i// initialize x
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);
}
Analysis: - If. we do not consider the time considered for sorting the inputs then
all of the three greedy strategies complexity will be O(n).

Q) Explain job sequencing with deadlines problem with a suitable


example.
JOB SEQUENCING WITH DEADLINES: -
Problem: -
We are given a set of ‘n’ jobs. Associated with each job there is a integer
dead line di0 and a profit pi>0. For any job i the profit pi is earned if and only if
the job is completed by its dead line. To complete a job one has to process the
job on a machine for one unit of time. Only one machine is available for
processing the jobs. A feasible solution for the problem will be a subset ‘j’ of jobs
such that each job in this subset can be completed by its deadline. The value of
a feasible solution ‘j’ is the sine of the profits of the jobs in ‘j’. An optimum
solution is a feasible solution with maximum value.
The problem involves identification of a subset of jobs which can be completed
by its deadline. Therefore the problem suites the subset methodology and can
be solved by the greedy method.
Ex: - Obtain the optimal sequence for the following jobs.
j 1 j2 j 3 j4
(P1, P2, P3, P4) = (100, 10, 15, 27)

(d1, d2, d3, d4) = (2, 1, 2, 1)


n =4

Feasible Processing Value


soln sequence
j1 j 2
(2,1) 100+10=110
(1, 2)
(1,3) (1,3) or (3,1) 100+15=115
(1,4) (4,1) 100+27=127
(2,3) (2,3) 10+15=25
(3,4) (4,3) 15+27=42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27
In the example solution ‘3’ is the optimal. In this solution only jobs 1&4
are processed and the value is 127. These jobs must be processed in the order
j4 followed by j1. the process of job 4 begins at time 0 and ends at time 1. And
the processing of job 1 begins at time 1 and ends at time2. Therefore both the
jobs are completed within their deadlines. The optimization measure for
determining the next job to be selected in to the solution is according to the
profit. The next job to include is that which increases ∑pi the most, subject to
the constraint that the resulting “j” is the feasible solution. Therefore the greedy
strategy is to consider the jobs in decreasing order of profits.

Q) Define the problem of job sequencing with deadlines and develop a


greedy heuristic algorithm for solving it. Also give a brief note on the
correctness of the developed algorithm.
Algorithm Greedy Job (d,j,n)

//j is a set of jobs that can be completed by


// their dead lines
{ j: = {1};
for i: = 2 to n do
{ if (all jobs in j u {i} can be completed by their dead lines) then
j: = ju{ i};
}
}

Q) Explain the faster version of job sequencing with deadlines problem


using an example. What is its timing complexity?
Q) Write a greedy algorithm for sequencing unit time jobs with
deadlines and profits. Using this algorithm, find the optimal solutions
when n=5,(p1,p2,p3,p4,p5)=(20,15,10,5,1) and
(d1,d2,d3,d4,d5)=(2,2,1,3,3).

Problem:-
Find the optimum job sequence for the following problem.
n=7 P7 J1 j2 j3 j 4 j5 j 6 j7
(P1, P2, P3, P4, P5, P6) = (3, 5, 10, 18, 1, 6, 30)

d7
(d1, d2, d3, d4, d5, d6) = (1,3, 4, 3, 2, 1, 2)

Feasible soln. Processing Sequence Value

(1, 2, 3, 4) (1,2,4,3) or (1,4,2,3) 3+5+10+18= 36

Q) Define spanning tree and minimum cost spanning tree with the help
of an example.
SPANNING TREE: - A Sub graph ‘n’ of o graph ‘G’ is called as a spanning tree if

(i) It includes all the vertices of ‘G’


(ii) It is a tree

For a given graph ‘G’ there can be more than one spanning tree. If
weights are assigned to the edges of ‘G’ then the spanning tree which has the
minimum cost of edges is called as minimal spanning tree.

The greedy method suggests that a minimum cost spanning tree


can be obtained by contracting the tree edge by edge. The next edge to be
included in the tree is the edge that results in a minimum increase in the some
of the costs of the edges included so far.

Q) Write and explain prim’s algorithm for determining the minimum


cost spanning tree.Derive its time and space complexities.
Q) Write and explain Prim’s minimum cost spanning tree algorithm.

PRIM’S ALGORITHM: -

i) Select an edge with minimum cost and include in to the spanning tree.
ii) Among all the edges which are adjacent with the selected edge, select
the one with minimum cost.
iii) Repeat step 2 until ‘n’ vertices and (n-1) edges are been included. And
the sub graph obtained does not contain any cycles.

Notes: - At every state a decision is made about an edge of mi9nimum cost to


be included into the spanning tree. From the edges which are adjacent to the
last edge included in the spanning tree i.e. at every stage the sub-graph
obtained is a tree.

1 1
28
2
2 10 11
10 16
11 16
6 7 3
6 7 3
24 18
25 12 25 12
5 4 5 4

22 22

Algorithm Prim (E, cost, n,t)

// E is the set of edges in G. Cost (1:n, 1:n) is the


// Cost adjacency matrix of an n vertex graph such that
// Cost (i,j) is either a positive real no. or q if no edge
// (i,j) exists. A minimum spanning tree is computed and
// Stored in the array + (1:n-1), 1:2). (t (i, 1), + (i,2)
// is an edge in the minimum cost spanning tree
// The final cost is returned
{
Let (k, l) be an edge with min cost in E
Min cost: = Cost (x,l);
T(1,1):= k; + (1,2):= l;
for i:= 1 to n do//initialize near
if (cost (i,l)<cost (i,k) then n east (i): l;
else near (i): = k;
near (k): = near (l): = 0;
for i: = 2 to n-1 do
{//find n-2 additional edges for t
let j be an index such that near (i) 0 & cost (j, near (i)) is minimum;
t (i,1): = j + (i,2): = near (j);
min cost: = Min cost + cost (j, near (j));
near (j): = 0;
for k:=1 to n do // update near ()
if ((near (k) 0) and (cost {k, near (k)) > cost (k,j)))
then near Z(k): = ji
}
return mincost;
}

The algorithm takes four arguments E: set of edges, cost is nxn adjacency
matrix cost of (i,j)= +ve integer, if an edge exists between i&j otherwise infinity.
‘n’ is no/: of vertices. ‘t’ is a (n-1):2matrix which consists of the edges of
spanning tree.
E = { (1,2), (1,6), (2,3), (3,4), (4,5), (4,7), (5,6), (5,7), (2,7) }
n = {1,2,3,4,5,6,7)
1 28
Cost 1 2 3 4 5 6 7

1  28    10  10 2
2 28  16    14 14
3  10  12   
16
6 7 3
4   12  22  18
5    22  25 24
6 10    25   25 27 18 12
7  14  18 24   5 4
22

1
1 2 3 4 5 6
2
1 1 6 5 4 3 2 10
14 16
2 6 5 4 3 2 7
6 7 3

Start Vertex
Ending Vertex Edges of spanning tree 25
18
5 4

22
i) The algorithm will start with a tree that includes only minimum cost
edge of G. Then edges are added to this tree one by one.
ii) The next edge (i,j) to be added is such that i is a vertex which is
already included in the treed and j is a vertex not yet included in
the tree and cost of i,j is minimum among all edges adjacent to ‘i’.
iii) With each vertex ‘j’ next yet included in the tree, we assign a value
near ‘j’. The value near ‘j’ represents a vertex in the tree such that
cost (j, near (j)) is minimum among all choices for near (j)
iv) We define near (j):= 0 for all the vertices ‘j’ that are already in the
tree.
v) The next edge to include is defined by the vertex ‘j’ such that (near
(j))  0 and cost of (j, near (j)) is minimum.
Analysis: -

The time required by the prince algorithm is directly proportional to the no/: of
vertices. If a graph ‘G’ has ‘n’ vertices then the time required by prim’s
algorithm is 0(n2)

Q) Write and explain the Kruskal’s algorithm.


KRUSKAL’S ALGORITHM: -

In Kruskals algorithm for determining the spanning tree we arrange the edges in
the increasing order of cost.

i) All the edges are considered one by one in that order and deleted from
the graph and are included in to the spanning tree.
ii) At every stage an edge is included; the sub-graph at a stage need not
be a tree. Infact it is a forest.

iii) At the end if we include ‘n’ vertices and n-1 edges without forming
cycles then we get a single connected component without any cycles
i.e. a tree with minimum cost.
At every stage, as we include an edge in to the spanning tree, we get
disconnected trees represented by various sets. While including an edge in to
the spanning tree we need to check it does not form cycle. Inclusion of an edge
(i,j) will form a cycle if i,j both are in same set. Otherwise the edge can be
included into the spanning tree.

Ex: - Let the partition be {1,2} {3,4,6} {5}

1 3

2 4 5

I we want to include the edge (1,4) it can be included as land 4 are in different
sets.

1 3

2 4 5

6
So we get (1,2,3,4,6) (5) subsets respectively. If we want to include the edge
between (2,6) because 2&6 are in the same subsets and its i9nclusion will lead
to a cycle.

1 1
3

2 9

5 Min spanning Tree;


6

1
1
6 1 11 1

32
2 3
2
7
3 8 2 3 2
4 5 4 5

10
4 5 9 4 5
6 6

//Algorithm Kruskal (E, cost, n,t)


//E is the set of edges in G. ‘G’ has ‘n’ vertices
//Cost {u,v} is the cost of edge (u,v) t is the set
//of edges in the minimum cost spanning tree
//The final cost is returned
{ construct a heap out of the edge costs using heapify;
for i:= 1 to n do parent (i):= -1 // place in different sets
//each vertex is in different set {1} {1} {3}
i: = 0; min cost: = 0.0;
While (i<n-1) and (heap not empty))do
{
Delete a minimum cost edge (u,v) from the heaps; and reheapify using adjust;
j:= find (u); k:=find (v);
if (jk) then
{ i: = 1+1;
+ (i,1)=u; + (i, 2)=v;
mincost: = mincost+cost(u,v);
Union (j,k);
}
}
if (in-1) then write (“No spanning tree”);
else return mincost;
}

Analysis: - If the no/: of edges in the graph is given by /E/ then the time for
Kruskals algorithm is given by 0 (|E| log |E|).

Q) Show the step by step procedure of deriving the minimum cost


spanning tree using prim’s and kruskal’s algorithm on the below graph.

45 30
1
4 8

55 25

3 20 50

2 40 7
15

5 10
5 35 6

Prim’s Algorithm:

2
1) 2 2)
5 7
5 5 5
15

8
3) 2 4)
15 50
5 5 7 2 5

5 7
10 15

10
6
6

5) 30 6)
4 8 4 8
50
2 2
5 7 5 7

6 6
5 15

10

7) Min cost= 5+5+10+50


45 30
1 4 8

25
50
3

2 5 7
5 15
10

Kruskals Algorithm

1) 1 2)
1

2 3 4 8 2 3 4 8

5 5
5 7 5 7

10
6 6

3) 4)
1 1
5 5

2 3 4 2 3 4
7 7
20
5 10
5 6 5 6
5) 6)
1 1
5
25
25
2 3
2 3 4
20 7

5 10 5 20 30
15 5 4 8
5 6
15
7
10

7)
1
25

3
40
2
5
5 30 4 30 8 5+20+15+30+10+40+25 = 145

15
7

10

Q) Explain the optimal storage on tapes problem by means of an


example.

Optimum Storage on Tapes: -

There are ‘n’ programs that are to be stored on a computer tape of length ‘l’.
Associated with each program I there is a length li, 1≤ i ≤ n. It is clear that all the
programs can be stored on to the tape it and only if the sum of the lengths of all
the programs is ≤ l. We assume that when ever a program is to be retried from
the tape, the tape is positioned at the beginning. In the optimum storage on
tapes problem we are required to find a permutation for ‘n’ programs so that
when they are stored on the tape in that order the mean retrieval time will be
minimum.
Example: - Let n=3; (l1, l2, l3) = (8,5,3)

There are n1 i.e. 3!=6 orderings. These orderings and their respective‘d’ values
are as follows.

Orderings  (I)

1 2 3 8+(8+5)+(8+5+3)= 37
1 3 2 8+(8+3)+(8+3+5)= 35
3 2 1 3+(3+5)+(3+5+8)= 27
2 3 1 5+(5+3)+5+3+8 = 29
3 1 2 3+(3+8)+(3+8+5)= 30
2 1 3 5+5+8+5+8+3 = 34

The optimal ordering is 3 2 1.

The greedy strategy studies that programs are to be arranged in the increasing
order of lengths to achieve the optimal solution.

The tape problem can be extended to several tapes. If there are ‘m’ tapes then
the n programs are to be distributed over these tapes. For each tape a storage
permutation is to be provided. The object5ive again here is to store the
programs in such a way that the total requirement, time is minimum.

Q) Write the algorithm for generating 2-way merge tree and illustrate it
with suitable example. Also analyze the algorithm.
Optimal Merge Patterns: -

When more than two socket files are to be merged together the merge can be
accomplished by repeated merging sorted files in pairs. If we are given four files
x1, x2, x3, x4 which are to be merged, then we could merge in the following ways.

i) Merge x1, x2 to get y1, y is merged with x3 to get y2 and finally y2 is


merged with x4 to get y3.

x1, x2, x3, x4

y1 , x3

y1 , x3

y3

Ex: - 5, 10, 15, 20

15, 15

30 20

50

Total No/: of recor4d moves 15+30+50 = 95

ii) x1, x2 are merged to get y1, x3, x4 are merged to get y2 and y & y2 are
merged to get y3.
x1, x2, x3, x4

y1 , y2

y3
Ex: - 5, 10, 15, 20

15 35

50

Total record moves = 15+35+50 = 100

Given ‘m’ socket files there are many ways in which we can merge then in to a
single socket file. Different orders require different amounts of computing time.
We have to determine. The optimum sequence for pair wise merging of sorted
files which minimizes the no/: of comparisons. A greedy attempt is to merge two
smallest size files at each stage.

Let us consider 5 files with sizes

x1, x2, x3, x4, x5


20 30 10 5 30

We first merge x3 and x4 to get z1, z1 is merged with next smallest size 5 i.e. x 1 to
get z2. z2 & z5 are then combined to get z 3 and finally z2 is combined with z3 to
get z4.

x1, x2, x3, x4, x5


20 30 10 5 30
95
20 z1
z4
z2 z3 35 60
z2 z3
z4

15 20 30 30
z1
x1 x2 x5
5 10
x3 x4

ALGORITHM TO GENERATE A 2 WAY MERGE TREE: -


treenode = record
{
treenode ‘*lchild; treenode *rchild;
integer weight; //Datafield
}
Algorithm tree(n)
//list is a global list of n single node
//binary trees as described above for i = 1 to n-1 do
{ pt: = new treenode; //Get arrow node
(pt lchild):= least(list); //merge two trees
(pt rchild): = least (list)://smallest lengths
(ptweight): = ((ptlchild)  weight) + ((ptrchild) weight);
insert (list, pt);
}
return least (list); //tree left in the list is merge tree;
}
Ex: -
lchild pt weight rchild
200 30
15
0 0
1000

10

 Simulate the algorithm tree for the following

2,3,5,7,9,13

Iteration List

Initial 2 3 5 7 9 13

1 5

2 3

1
2 0

5 5

2 3

3 1
0 1
6

5 5
7 9

2 3
2
4. 3 1
6

10 13
7 9

5 5
2
3

2 3

23 16
5.

10 7 9
13

5 5

2 3

Q) Find an optimal binary merge pattern for ten files whose lengths are
28,32,12,5,84,53,91,35,3 and 11.

Q) Write an algorithm to find the shortest path from a single source in


a graph. Discuss its time complexity.
Q) Write and explain single source shortest path problem.
Q) Develop the greedy algorithm for generating shortest paths from a
single source in a given directed graph.
SINGLE SOURCE SHORTEST PATH: -
250
1500
4 5
1200
3250 2400
300 1000 250
2 3

6 250
300 1000 1400
900

1 8 7
1700 1000
3350 1450 1150
1 2 3 4 5 6 7 8
1 0 0 0 0 0 0 0 0

2 300 0 0 0 0 0 0 0

3 1000 800 0 0 0 0 0 0

4 0 0 1200 0 0 0 0 0

5 0 0 0 1500 0 250 0 0

6 0 0 0 1000 0 0 900 1400

7 0 0 0 0 0 0 0 1000

8 1700 0 0 0 0 0 0 0

Length adjacency matrix

Shortest path from vertex file to remaining vertices

Vertex
Internatio
S Select 1 2 3 4 5 6 7 8
n
s
150 20
Initial - -    0  
0 0
125 25 115 165
1 {5} 6    0
0 0 0 0
125 25 115 165
2 {5,6} 7    0
0 0 0 0
245 125 25 115 165
3 {5,6,7} 4   0
0 0 0 0 0
335 245 125 25 115 165
4 {5,6,7,4} 8  0
0 0 0 0 0 0
335 325 245 125 25 115 165
5 {5,6,7,4,8} 3 0
0 0 0 0 0 0 0
335 325 245 125 25 115 165
6 {5,6,7,4,8,3} 2 0
0 0 0 0 0 0 0
{5,6,7,4,8,3,
7 1 0 0 0 0 0 0 0 0
2}

We have to find a shortest path from a selected source to all of the remaining
destinations in a directed graph.

One possibility is to built shortest paths one by one. As an optimization measure


we can use the sum of the lengths of all paths generated so far. For this
measure to be minimized each individual path must be of minimum length. If we
have already constructed ‘i’ shortest paths, then using this optimization
measure, the next path to be constructed should be the next shortest minimum
length path.

The greedy way to generate the shortest paths from a specific vertex to the
remaining vertices is to generate these paths in non-decreasing order of path
lengths. First a shortest path to the next vertex is generated. This shortest path
to the second next nearest vertex is generated and so on.

Algorithm shortest paths (v, cost, dist, n)

//dist (j), 1≤ j ≤ n, is set to the length of shortest path


//from vertex v to vertex j in a diagraph G with n vertices
//dist(v) is set to zero. G is represented by its cost
//adjacency matrix cost (1:n, 1:n)
{ for i = 1 to n do
{ // initializes
s(i) = false;
dist(i) = cost (v,i);
}
s(v)= tree; dist (v) = 0.0; // put vins
for num: = 2 to n-1 do
{ determine n-1 paths from v choose u from among those vertices not in
s.
Such that dist (u) is min.
S(u):= true; //put u ins
For (each u adjancent to u with s(w)= false)do
// update distances
if (dist (w) > dist (u) + cost (u,w)) then
dist (w): = dist (u) + Cost (u,w);
}

10
5
5 2
15 30
2
10 4
4
4
4 1

15
6
6 10 3

Exercises:-
1. Solve the recurrence relation (3,2) for the following choices of a, b, and
f(n) (c being a a constant):
(a) a=1, b=2 and f(n)=cn
(b) a=5, b=4 and f(n)=cn2
(c) a=28, b=3 and f(n)=cn3

Questions:-
1. Theorem for knapsack problem (given in notes) April/may-00,Q4(b)

***** All the Best *****

You might also like