Daa 3
Daa 3
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.
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.
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
= .
.
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.
Analysis:-
Example:-
-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).
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.
Example:-
Simulate the algorithm Max min for the following elements:
1 2 3 4 5 6 7 8 9
a 22 13 -5 -8 15 60 17 31 47
(9)
I, 9, 60, -8
(7) (8)
1, 5, 22, 8 6, 9, 60, 17
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) = 2T = 2.Ty
=2 =2
=4T = 4.T
= 4.
= 8.T
= 2x-1 T (2) +
The no/: of comparisons in the best and worst and average case for the algorithm Max Min
is given by 3n/2 – 2
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.
310, 285, 179, 652, 351, 423, 861, 254, 450, 520
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
285, 310/ 179/ 652, 351/ 423, 861, 254, 450, 520
179, 285, 310/ 652, 351/ 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.
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.
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
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
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.
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 – 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
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)
Ii j p i j i< j
Partition
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.
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.
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) Define the knapsack problem and give the greedy algorithm for it.
Also prove the correctness of the heuristic employed.
KNAPSACK PROBLEM: -
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.
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
= 20 25+ = 28.2
(2) Greedy about weight: -
5
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)
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
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.
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.
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
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)
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.
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
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
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|).
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
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
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 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.
y1 , x3
y1 , x3
y3
15, 15
30 20
50
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
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.
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.
15 20 30 30
z1
x1 x2 x5
5 10
x3 x4
10
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.
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
7 0 0 0 0 0 0 0 1000
8 1700 0 0 0 0 0 0 0
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.
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.
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)