Daa Part 2C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

POPULAR PUBLICATIONS

b) Using greedy strategy, schedule the folowing jobs within deadline so as to


maximize the profit. Deadlines and profits are mentioned as follow: [WBUT 2014]
3 4
Job i 1 2
Deadline d, 3 2
7 7 2
Profit g 9
Answer:
All the jobs are already arranged in the above matrix according to their profit.
Suppose, initially the solution vector is S={0}
Highest profitable job is J1. So, we add job J1 to the solution vector.
i.e. S={Jl}.
Next, we add J2 to the solution vector. i.e. S={J1, J2}.
Next, we add J3 to the solution vector. i.e. S={J1, J2, J3}.
So, highest deadline is 3 and we can arrange maximum three jobs. The sequence of the
jobs are S={J1, J2, J3} or S={J2, J1, J3} or S={J3, J2, J1} or S={J2, J3, J1} and the
maximum profit is 23.
18. What is negative weight-cycle? Write Bellman-Ford algorithm to find single
_ource shortest distance of a directed graph. WBUT 2015]
Answer:
1"Part:
Some instances of the single-source shortest-paths problem may include edges whose
weights are negative. If the graph G=(V,E) contains no negative weight cycles reachable
from the source s, then for all veV, the shortest-path weight S(s, v) remains well
defined, even if it has a negative value. If the graph contains a negative-weight cycle
reachable from s, however, shortest-path weights are not well defined. No path from s to
a vertex on the cycle can be a shortest path. If there is a negative weight cycle on some
path froms to v we define S(s, v) =-o.
2nd Part: Refer to Question No. 11(a) of ShortAnSwer Type Questions.
19. a) Given the four matrices using Pox2030rs03 Rsou,Sulon Find the optional
sequence for the computation of multiplication operation. Write the algorithm also.
b) Consider the following table that consists of some items with weight and cost
values:
Items
Weight 5 10 15 22 25
Cost 30 40 45 77 90
If the knapsack capacity W =60 kg, find optional solution using
write an algorithm for doing so. greedy criteria and
WBUT 2017]
Answer:
a) Refer to Question No. 8 of Long Answer Type Questions.

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

create arrays L[1..n+ 1] and R[1.. n, +1]


for i 1to n,do

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

elements: [WBUT 2018, 2019]


b) Create a Max-Heap containing the following
10,20, 30, 40,50, 60, 70, 80,90, 100
Answer:
Max-Heap containing the following elements:
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
20

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

21. Write short notes on the following:


a) External sorting WBUT 2015]
b) Heap Creation Technique WBUT 2016]
c) 8 queens problem WBUT 2003, 2006, 2013]
d) Hamiltonian cycle WBUT 2017]
e) Heuristic Algorithm WBUT 2018]
)Bellman-Ford Algorithm WBUT 2018]
Answer:
a) External sorting:
Extermal sorting is a term for a class of sorting algorithms that can handle massive
amounts of data External sorting is required when the data being sorted do not fit into the
main memory of a computing device (usually RAM) and instead they must reside in the
slower external memory (usually a hard drive).
Characteristics:
Processing large files, unable to fit into the main memory
Restrictionson the access, depending òn the external storage medium Primary costs -
for input-output
Main concern: minimize the number of times each piece of data is moved between
the external storage and the main memory.
General strategy -Sort-Merge
Break the file into blocks about the size of the internal memory
Sort these blocks
Merge sorted blocks
" Usually several passes are needed, creating larger
sorted
sorted blocks until the whole file

Basic Algorithm
Assumptions:
Four tapes:
Two for input - Tal, Ta2,
DAA-76
DESIGNAND ANALYSIS OF ALGORITHMS

Two for output - Tb1, Tb2.


Initially the file is on Tal.
N records on Tal
M records can fit in the memory

Step 1:Break the file into blocks of size M, NM]+1blocks


Step 2: Sorting the blocks:
read a block, sort, store on Tbl
read a block, sort, store on Tb2,
read a block, sort, store on Tbl,
etc, altenatively writing on Tbl and Tb2
Each sorted block is called a run.
Each output tape willcontain half of the runs
Step 3: Merge:
a. From Tbl, Tb2 to Tal,Ta2.
Merge the first run on Tbl and the first run on Tb2, and store the result on Tal:
Read two records in main memory, compare, and store the smaller on Tal
Read the next record (from Tbl or Tb2 - the tape that contained the record stored
on Tal) compare, store on Tal, etc.
Merge the second run on Tbl and the second run on Tb2, store the result on Ta2,.
Merge the third run on Tbl and the third run on Tb2, store the result on Tal.
Etc, storing the result alternatively on Tal and Ta2.
Now Tal and Ta2 will contain sorted runs twice the size of the previous runs on
Tbland Tb2
b. From Tal, Ta2 to Tbl, Tb2.
Merge the first run on Tal and the first run on Ta2, and store the result on Tbl.
Merge the second run on Tal and the second run on Ta2, store the result on Tb2
Etc, merge and store alternatively on Tal and Ta2.
c. Repeat the process until only one run is obtained. This would be the sorted file
Analysis of two-way merge
The algorithm requires [log(N/M)] passes plus the initial
run-constructing pass.
Each pasS merges runs of length r to obtain runs of length 2*r.
The first runs are of length M. The last run would be of length
Let's assume that N is a multiple of M. N.
Initial situation:
lst tape contains N records = M records * NIM runs
After storing the runs on two tapes,each contains halfof the
2 tapes * M records per run * (1/2XNM) runs runs:
=N records
After merge 1st pass - double the length of the runs, halve
2tapes * 2M records_ per _run * the number of the runs:
After merge 2nd pass : (1/2X1/I2YNIM) runs = N records
2 tapes * 4M records per_run*(1/4X1/2) (NM) uns = N records
DAA-77
POPULAR PUBLICATIONS

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

Max Heap Construction Algorithm min values instead of max


The procedure to create Min Heap is similar but we go for one element at a
values. The technique to derive an algorithn for max heap is to insert
it should be
time. At any point of time, heap must maintain its property. While insertion,
considered that the inserting a node in an already heapified tree.
Step 1-Create a new node at the end of heap.
Step 2- Assign new value to the node.
Step 3 - Compare the value of this child node with its parent.
Step 4 - If value of parent is less than child, then swap them.
Step 5- Repeat step 3 & 4until Heap property holds.
c) 8 queens problem:
8-queens problem is one of the common problems. This happens to be an appropriate
example, where backtracking may be used very effectively. The problem is to set 8
queens in an 8x8 chessboard that notwo queens will attack each other. So, notwo queens
are in the same row or same column or same diagonal. In general, it can be described as
to place n queens in an nxn chessboard in such a way that no two queens attack one
another.
The solution space required is [8(8- 1)(8'- 2)... (8- 7)] to solve this problem. So, in
general we can say that for &-queens problem, the solution space is IK8 -) and similarly,

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

Fig: 1Placing 8 queens in an &x 8checkerboard in two different way


Now, before discussing 8-qucens problem, first we shall study 4-queens problem.
Because it is easy to solve and is possible to draw path of backtracking for better
comprehension.

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

You might also like