0% found this document useful (0 votes)
12 views16 pages

Daaunit4 IT3

Uploaded by

Shubham Shukla
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)
12 views16 pages

Daaunit4 IT3

Uploaded by

Shubham Shukla
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/ 16

DESIGN AND ANALYSIS PF

ALGORITHMS
UNIT-4

1/11/202 [email protected] 1
4
CONTENTS
 Dynamic programming
 Warshal’s and floyd’s algorithm
 Resource allocation problem
 Backtracking
 Branch and bound
 Graph and colouring
 N-Queen problem
 Hamiltonian cycles
 Sum of subsets

1/11/202 [email protected] 2
4
Dynamic programming
Dynamic programming is a technique for solving problem similar to divide
and conquer by dividing a problem into sub problems whose solutions may viewed as
the result of a sequence of decisions.
Dynamic programming is a bottom-up approach and it is used when sub
problems are not independent.

Dynamic programming solution to 0-1 knapsack problem:


0 if i=0,or w=0
c[i,w]= c[i-1,w] if wi>w
max{(c[i-1,w],vi+c[i-1,w-wi]) w>=wi

1/11/202 [email protected] 3
4
Cont…..

Dynamic 0-1 knapsack (v,w,n,w)

1. for w=0 to W
2. do c[0,w]=0
3. for i=1 to n
4. do c[i,0]=0
5. for w=1 to W
6. do if wi=<w
7. then if vi+c[i-1,w-wi]
8. then c[i,w]=vi+c[i-1,w-wi]
9. else c[i,w]=c[i-1,w]
10. else
11. c[i,w]=c[i-1,w]

1/11/202 [email protected] 4
4
Matrix-Chain Multiplication

1. n← length[p]-1
2. for i ← 1 to n
3. do m[i,i] ←0
4. for i ←2 to n
5. l is the chain length
6.do for i ←1 to n-1+1
7. do j ←i+1-1
8. m[i,j] ←∞
9. for k ←i to j-1
10. do q ←m[i,k]+m[k+1,j]+pi-1pkpj
11. if q<m[i,j]
12. then m[i,j] ←q
13. s[i,j] ←k
14. return m and s

1/11/202 [email protected] 5
4
Warshal’s and floyd’s algorithm

Floyd-Warshal algorithm is an algorithm which is used to find the shortest


paths among all the pairs of nodes in a graph, which does not contain any
cycles of negative length .The main advantage is its simplicity.
1. n  rows[w]
2. dw
3. for k=1 to n
4. do for i  1 to n
5. do for j  1 to n
6. do for dik  min (dij,dik(k-1)+dkj(K-1))
7. return Do
dij= wij if k=0
min[dij(k-1),dik(K-1)+d kj (K-1)]

1/11/202 [email protected] 6
4
1 1 8
4 2
4 2
9 1
3

08∞ 1 08∞ 1 089 1


Do = ∞ 0 1 ∞ ∞ 0 1∞ ∞ 0 1∞
4 ∞ 0∞ D1 = 4 12 0 5 D2 = 4 12 0 5
∞29 0 ∞ 2 90 ∞ 2 30

089 1 034 1
501 ∞ 501 ∞
D3 = D4 =
4 12 0 5 47 0 5
723 0 72 3 0

1/11/202 [email protected] 7
4
N1N 1 N1N 1 N12 1
∏0 = N N 2 N NN2N NN2N
∏1 = ∏2 =
4 N N N 31N 1 31N 1
N44 N N44 N N 4 2N

N 1 2 1 N 4 4 1
∏3 = 3 N 2 3 ∏4 = 3 N 2 3
3 1 N 1 3 4 N 1
3 4 2 N 3 4 2 N

1/11/202 [email protected] 8
4
Backtracking

Backtracking is a recursive process where we start with one possible move out of
many available moves and try to solve the problem if we are able to solve the problem with
The selected move then we will print the solution else we will backtrack and select some
Other move and try to solve it. If none if the moves work out we will claim that there is no
solution for the problem.

1/11/202 [email protected] 9
4
Branch and bound

It is also called as best-first search. Branch and bound is a systematic


method for solving optimization problems. When backtracking and greedy method
fails branch and bound is applied. In this process, we used to calculate the bound at
each stage and check whether it is able to give answer or not.

1/11/202 [email protected] 10
4
Graph and Coloring
Coloring all the vertices of a graph with colors such that no two adjacent
vertices have the same color is called as graph coloring.

1/11/202 [email protected] 11
4
N-Queen problem
In n-Queen problem, we have to place n-Queen in an n x n chessboard so that no
queen attack each other i.e., no two queen are placed on the same row, column or diagonal.
Place(k,i):
1. For j  1 to k-1
2. do if (x(j)=i) or abs(x[j]-i)=(abs(j-k))
3. Then return false

n-Queen(k,n):
1. for i  1 to n
2. do if place(k,i)
3. Then x[k]  i
4. if k=n, then print x[1…….N]
5. else n-Queen(k=1,n)
q1
Suppose we have 4 queens i.e, q1,q2,q3,q4 Step1.

1/11/202 [email protected] 12
4
Steps 2. Steps 3. Steps 4.
q1 q1 q1
q2 q2 q2
q3 q3
q4

It is impossible for q4 to <2,4,1,3>


placed in the 4th row since
the other queen attack the
4th queen so we use the
concept of backtracking.

1/11/202 [email protected] 13
4
Hamiltonian cycles

A Hamiltonian cycle is also known as Hamiltonian circuit. A


Hamiltonian cycle is a Hamiltonian Path such that there exit an edge in
graph from the last vertex to the first vertex of the Hamiltonian Path.
Whether the graph contains Hamiltonian Cycle or not. If it contains
Hamiltonian Cycle, then path is done.
For example, a Hamiltonian Cycle for the graph{0, 1, 2, 4, 3, 0}.
There are more Hamiltonian Cycles in the graph like {0, 3, 4, 2, 1, 0}
0 1 2

3 4

1/11/202 [email protected] 14
4
Sum of subsets

And the following graph doesn’t contain any Hamiltonian Cycle.


0 1 2

3 4

1/11/202 [email protected] 15
4
Sum of subsets

In this problem, we have to find the subset of the given set S where the elements in
the set are n positive interferes in such a manner that s` belongs to S and the sum of elements
of subset s` is equal to some positive integer X. The subset problem can be calculated

1/11/202 [email protected] 16
4

You might also like