Daa Part 2C
Daa Part 2C
Daa Part 2C
DAA-72
DESIGNAND ANALYSIS OF ALGORITHMS
elements.
b) Here vi is the cost and wi is the weight of the each knapsack
So, according to the Greedy Knapsack problem,
vl/ wl =30/5 =6
V2/ w2 =40/10 =4
v3/ w3 =45/15 =3
v4/ w4 =77/22 =3.5
v5/ w5 =90/25 =3.6
The knapsack capacity W= 60 kg
We consider that some fraction xi, 0sxisl of the object iis kept into the knapsack.
The highest ratio is vI/ wl =6 and insert one unit of wl=5
So, x1 1, x1.wl =5, x1.vl=30 and W= W- x1 .wl =60-5=55
Next highest ratiois v2/ w2 =4 and insert one unit of w2 = 10
So, x2 = 1, x2.w2 = 10, x2. v2 = 40 and W= W- x2 .w2 = 55-10=45
Next highest ratio is v5/ w5 =3.6 and insert one unit of w5 = 25
So, x5 = 1, x5.w5 =25, x5. v5 = 90 and W= W- x5 .w5 = 45-25-20
Next highest ratio is v4/ w4 =3.5 and insert fractional part of w4.
So, x4 = 20/ 22 =10/11. So, x4 = 10/11, x4 .w4 =(10/11)*22 = 20,
X4. v4 ={10/11)*77=70 and W= W- x4.w4= 20 20=0
So, the knapsack is full and then maximum profit is
=x1. vl + x3. v3 +x5. v5 =30+40+90+70= 230
20.a) Write an algorithm for Merge Sort. [WBUT 2018, 2019]
Answer:
The procedure MERGE-SORT(A, p, r) sorts the elements in the sub-array A[p..r]. Ifp
>r, the sub-array has at most one element and it is already sorted. Otherwise, the divide
step simply computes an index qthat partitions A[p..] into two sub-arrays: A[p.. ql,
containing [n/2] elements, and A[q +1..r], containing In/2] elements.
MERGE-SORT(A, p, r)
if (p <r) then
q+[(p tr)/2]
MERGE-SORT(A, p, )
MERGE-SORT(A, q+1, r)
MERGE(A, p, 4, r)
MERGE(A, p, q, r)
nË t-q-p+l
DAA-73
POPULAR PUBLICATIONS
L]- A(P-i-1)
forj -1to n, do
L[n, + I]
R[n; +1]
i -1
j-1
for k p to r do
ifLI] < RI] then
AR]-L)
i -i+l
else
AR] Ri]
j-j+l
10 10 20
(a) b) ()
30
40
30
20
10
20
10
(40
10 30
()
()
DAA-74
DESIGN AND ANALYSIS OF ALGORITHMS
60
40 20 40 50
10 30 60 30
10
()
40 60
40
10 30 (20 50
10 (30 20 70
30 20
40 30 20
10
(h)
10 80 60
40
10 90 40
DAA-75
POPULAR PUBLICATIONS
(100)
60
10
10
Max Heap
20 50 10 40 30
90 60 70
100
Basic Algorithm
Assumptions:
Four tapes:
Two for input - Tal, Ta2,
DAA-76
DESIGNAND ANALYSIS OF ALGORITHMS
records
After merge s-th pass: (1/2s)(1/2)(N/M)runs =N
records per run *
2tapes *2s M
is2M.
the s-th merge
Thus the length of the runs after one run equal tothe whole
file:
After the last merge there is only
2°M =N
2°= N/M
s=log(N/M)
ifsis the last merge, s = log(N/M) O(Nlog(N/M)
records, so the complexity is
Atcach pass we process N
b) Heap Creation Technique:
binary tree in which every level, except possibly the last, is
Acomplete binary tree is a possible. A Binary Heap is a Complt
completely filled, and allnodes are as far left asorder such that value in a parent node is
Binary Tree where items are stored in a special
smaller) than the values in its two children nodes. The former is called ac
greater(or represented by binary tree or
heap and the latter is called min heap. The heap can be
array. Since a Binary Heap is a Complete Binary Tree, If it can be easily represented as
the parent node is stored at
array and array based representation is space efticient. (assuming
index I, the left child can be calculated by 2*i+1and right child by 2 *i+2
the indexing starts at 0).
Example:
Min-Heap - the value of the root node is less than or equal to either of its children.
31
Max-Heap- the value of theroot node is greater than or equal to either of its children.
DAA-78
DESIGN AND ANALYSIS OF ALGORITHMS
42 35
33 31 (10
10
order of arrival.
Both trees are constructed using the same input and
we can say for n-queens problem, that the required solution space is IK-i), The
condition for placing 8-queens in an 8 x 8 checkerboard, each queen can be
square of each row or column, and there are 8 squares in each row and placed in any
place 8 queens in 8° locations. But if we consider initially that 8 column, so we can
different rows then the solution space reduces to 8!. Because, if different queens are in 8
we have placed queen 1
in row 1 then it takes any one of eight
columns. But, there is an option for second queen
being placed in just remaining 7 columns and third queen has remaining 6
placed in and so on. So, without loss of generality, we can place columns to be
S 8. Now, we can define solutions of this 8 queen qi at row ifor 1si
queens problem as 8 tupples (X1, X2 ...X3),
DAA-79
POPULAR PUBLICATIONS
where cach x, denotes the column index where queen 4, is placed, so that no two queens
are attacking cach other.
Here, we have shown some instances of 8-qucens problem in figure 1.
Cobu’ Colun ’
1 4 5 67 2 1 2 6 7
o
3
4 4
6O 6
d) Hamiltonian cycle:
Refer to Question No. 7 of Long Answer Type Questions.
c) Heuristic Algorithm:
Aheuristicalgorithm is one that is designed to solve a problem in a faster and more
efficient fashion than traditional methods by sacrificing optimality, accuracy, precision,
or completeness for speed. Heuristic algorithms often times used to solve NP-complete
problems, aclass of decision problems. In these problems, there is no known efficient
way to find a solution quickly and accurately although solutions can be verified when
given. Heuristics can produce a solution individually or be used to provide a good
baseline and are supplemented with optimization algorithms. Heuristic algorithms are
most often employed when approximate solutions are sufficient and exact solutions are
necessarily computationally expensive.
The following are well-known examples of "intelligent" algorithms that use clever
simplifications and methods to solve computationally complex problems.
Swarm Intelligence: Swarm Intelligence systems employ large numbers of agents
interacting locally with one another and the environment. Swarm intelligence refers to the
collective behavior of decentralized systems and can be used to describe both natural and
ariticial systems. Specific algorithms for this class of system include the particle swarm
optimization algorithm, the ant colony optimization algorithm, and artificial bee colony
Wgonthm. Each of the previous algorithms was inspired by the natural, self-organized
behavior of animals.
Genetic
algorithmsAlthat
gorithdescribe
ms: Genetic
a setalgorithms are asubset
of techniques inspiredofabylarger
naturalclassselection
of evolutionary
such as
inheritance, mutation, and crossover. Genetic algorithms require both a genetic
epresentation of the solution domain and a fitness function to evaluate the solution
DAA-80
DESIGN AND ANALYSIS OF ALGORITHMS
domain. The techniquegenerates a population of candidate solutions and uses the fitness
function to select the optimal solution by iterating with each generation. The algorithm
terminates when the satisfactory fitness level has been reached for the population or the
maximum generations have been reached.
Arificial Neural Networks: Artificial Neural Networks (ANNS) are models capable of
pattern recognition and machine learning, in which a system analyzes a set of training
data and is then able to categorize new examples and data. ANNs are influenced by
animals' central nervous systems and brains, and are used to solve a wide variety of
problems including speech recognition and computer vision.
Support Vector Machines: Support Vector Machines (SVMs) are models with training
data used by artificial intelligence to recognize patterns and analyze data. These
algorithms are used for regression analysis and classification purposes. Using exxample
data, the algorithmwill sort new examples into groupings. These SVMs are involved
with machine learning, a subset of artificial intelligence where systems learn from data,
and require training data before being capable of analyzing new examples.
)Bellman-Ford Algorithm:
Refer to Question No. 11 of ShortAnswer Type Questions.
DAA-81