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

Unit-3

Dynamic Programming

Dynamic Programming is a technique in computer programming that helps to efficiently solve a


class of problems that have overlapping subproblems and optimal substructure property.
If any problem can be divided into subproblems, which in turn are divided into smaller
subproblems, and if there are overlapping among these subproblems, then the solutions to these
subproblems can be saved for future reference. In this way, efficiency of the CPU can be
enhanced. This method of solving a solution is referred to as dynamic programming.
Without Dynamic Programming

int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
sum = fib(n-1) + fib(n-2);
}
Without Dynamic Programming

Recurrence Relation
T(n)=2T(n-1)+4
Time Complexity=O(2^n)
Dynamic Programming

Dynamic programming can be achieved using two approaches:


1) Top-down approach (Memorization)
2) Bottom-up approach

1) Top-down approach The top-down approach follows the strategy of memorization.


The memorization process is equivalent to adding the recursion and caching steps. The
difference between recursion and caching is that recursion requires calling the function
directly, whereas caching requires preserving the intermediate results.
Top-down approach

static int count = 0; Time Complexity=O(n)


int fib(int
Time n)
Complexity=O(n)
{ if(memo[n]!= -1) Disadvantages
return memo[n];
Disadvantages
count++; The top-down approach uses the
Theif(n==0)
top-down approach uses the recursion technique, which occupies
returntechnique,
recursion 0; which occupies more memory in the call stack
if(n==1)
more memory in the call stack
return 1;
sum = fib(n-1) + fib(n-2);
memo[n] = sum;
}
Syllabus
Contents
• UNIT- III
❑ Fundamentals of IEEE 802.15.4, Zigbee and 6LOWPAN: Importance of IEEE 802.15.4
MAC and IEEE 802.15.4 PHY layer in constrained networks and their header format,
Importance of Zigbee technology and its applications, use of IPv6 in IoT Environments,
Understanding importance of IPv6 and how constrained nodes deal with bigger
headers (IPv6). Understand IPv6 over Low Power WPAN (6LoWPAN) and role of 6LoWPAN
in wireless sensor network. Various routing techniques in constrained network.
Understanding IoT Application Layer Protocols: HTTP, CoAP Message Queuing Telemetry
Transport (MeTT).
Bottom-up approach

It uses the tabulation technique to implement the dynamic programming approach. It


solves the same kind of problems but it removes the recursion. If we remove the
recursion, there is no stack overflow issue and no overhead of the recursive functions.
Time Complexity=O(n)
Bottom-up approach

1.int fib(int n)
2.{
3. int A[];
4. A[0] = 0, A[1] = 1;
5. for( i=2; i<=n; i++)
6. {
7. A[i] = A[i-1] + A[i-2]
8. }
9. return A[n];
10.}
0/1 Knapsack Problem
The 0/1 knapsack problem means that the items are either completely
or no items are filled in a knapsack.
Weights: {3, 4, 6, 5} Profits: {2, 3, 1, 4}
The weight of the knapsack is 8 kg
The number of items is 4
xi = {1, 0, 0, 1}
= {0, 0, 0, 1}
= {0, 1, 0, 1}
Possible combinations= 2^4=16

. 9
0/1 Knapsack Problem

w[i] P[i] w 0 1 2 3 4 5 6 7 8
i
0 0

3 2

4 3

5 4

6 1

c[i,w]=max{c[i−1,w−w[i]]+P[i],c[i-1, w]} w[i]<=w c[i, w] = c[i-1, w] w[i]>w


c[i,w]=0 i=0,w=0
11
0/1 Knapsack Problem

w[i] P[i] w 0 1 2 3 4 5 6 7 8
i
0 0 0 0 0 0 0 0 0 0 0 0

3 2 1 0 0 0 2 2 2 2 2 2

4 3 2 0 0 0 2 3 3 3 5 5

5 4 3 0 0 0 2 3 4 4 5 6

6 1 4 0 0 0 2 3 4 4 5 6

c[i,w]=max{c[i−1,w−w[i]]+P[i],c[i-1, w]} w[i]<=w c[i, w] = c[i-1, w] w[i]>w


c[i,w]=0 i=0,w=0 12
0/1 Knapsack Problem

}
int knapsack(int W, int wt[], int val[], else {
int n){ K[i][w] = K[i-1][w];
int K[n+1][W+1]; }
for(int i = 0; i<=n; i++) { }
for(int w = 0; w<=W; w++) { }
if(i == 0 || w == 0) { return K[n][W];
K[i][w] = 0; }
} else if(wt[i-1] <= w) { Time Complexity=O(n^2)
K[i][w] = find_Max(val[i-1] + Space Complexity=O(n^2)
K[i-1][w-wt[i-1]], K[i-1][w]);

12
0/1 Knapsack Problem

Let us consider that the capacity of the knapsack is W = 8 and the


items are as shown in the following table.

Item A B C D
Profit 2 4 7 10
Weight 1 3 5 7

13
Floyd Warshall Algorithm

Floyd-Warshall Algorithm is an algorithm for finding the shortest path


between all the pairs of vertices in a weighted graph. This algorithm works
for both the directed and undirected weighted graphs. But, it does not
work for the graphs with negative cycles (where the sum of the edges in
a cycle is negative).
This algorithm follows the dynamic programming approach to find the
shortest paths.

14
Floyd Warshall Algorithm

14
Floyd Warshall Algorithm

1 2 3 4
A0 1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0

1 2 3 4

A1 1 0 3 INF 5
2 2 0 INF 4
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
3 INF 1 0 INF
A1 [2,3]=min(A0 [2,3],A0 [2,1]+A0 [1,3])
4 INF INF 2 0
14
Floyd Warshall Algorithm

A1 1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0

1 2 3
A2 1 0 3 INF
2 2 0 INF
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
A2 [1,4]=min(A1 [1,4],A1 [1,2]+A1 [2,4]) 3 3 1 0
4 INF INF 2
14
Floyd Warshall Algorithm

1 2 3 4
2
A
1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 INF INF 2 0

1 2 3 4
A3 1 0 3 INF 5

Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j]) 2 2 0 INF 4


3 3 1 0 5
4 5 3 2 0
14
Floyd Warshall Algorithm

1 2 3 4
A3 1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 5 3 2 0

1 2 3 4

A4 1 0 3 7 5

Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j]) 2 2 0 6 4


3 3 1 0 5
4 5 3 2 0
14
Floyd Warshall Algorithm
n = no of vertices
A = matrix of dimension n*n Time Complexity= O(n3)
for k = 1 to n
Space Complexity= O(n2)
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k,
j])
return A

15
.
Longest Common Subsequence

Longest means that the subsequence should be the biggest one. The common
means that some of the characters are common between the two strings. The
subsequence means that some of the characters are taken from the string that is
written in increasing order to form a subsequence.
Example 1
W1 = abcd
W2= bcd
Longest common subsequence=bcd

16
Longest Common Subsequence
Example 2
S1 = {BCDAACD}
S2 = {ACDBAC}
Then, common subsequences are {BC}, {CDAC}, {DAC}, {AAC},
{AC}, {CD}. CDAC is the longest common subsequence.

17
.
Longest Common Subsequence

C B D A
j→ 0 1 2 3 4
i↓
0
A 1
C 2
A 3
D 4
B 5

18
Longest Common Subsequence

j→ 0 1 2 3 4
i↓
0
1
2
3
4
5

18
Longest Common Subsequence

j→ 0 1 2 3 4
i↓
0

1
2
3

18
Longest Common Subsequence

In order to find the longest common subsequence, start from the last element and follow the
direction of the arrow. 18
Longest Common Subsequence

Thus, the longest common subsequence is CA.


18
Longest Common Subsequence Using
Dynamic Programming
int lcs(char* X, char* Y, int m, int n){ Time Complexity=O(N2)
int L[m + 1][n + 1]; Space Complexity=O(N2)
int i, j, index;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1]) {
L[i][j] = L[i - 1][j - 1] + 1;
} else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
19
Backtracking

Backtracking is an algorithmic technique where the goal is to get all


solutions to a problem using the brute force approach. It consists of
building a set of all the solutions incrementally. Since a problem
would have constraints, the solutions that fail to satisfy them will be
removed.
It uses recursive calling to find a solution set by building a solution
step by step, increasing levels with time. In order to find these
solutions, a search tree named state-space tree is used. In a
state-space tree, each branch is a variable, and each level
represents a solution.

20
Backtracking

A backtracking algorithm uses the depth-first search method. When


it starts exploring the solutions, a bounding function is applied so
that the algorithm can check if the so-far built solution satisfies the
constraints. If it does, it continues searching. If it doesn’t, the
branch would be eliminated, and the algorithm goes back to the
level before.

20
Backtracking

Backtracking Algorithm
Based on depth-first recursive search
Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recursive
c) If recursion returns a solution, return it
4. If no choices remain, return failure
Some times called “search tree”

20
Backtracking

20
Brute force method

We want to arrange the three letters a, b, c in such a way that c cannot be beside a.
we’ll build a
state-space tree.
(a,b,c) and
(c,b,a) in the
final solution set

20
Backtracking

We want to arrange the three letters x, y, z in such a way that z cannot be beside x.
we’ll build a
state-space tree.
(x,y,z) and
(z,y,x) in the
final solution set

20
Difference between the Backtracking and Dynamic Programming
Difference bwtween Backtracking and Branch and Bound
Advantage, Disadvantage and Application of Backtracking and Branch and Bound

Applications of Backtracking
N-queen problem
Sum of subset problem
Graph coloring
Hamiliton cycle

21
N-Queens Problem
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.

. 22
N-Queens Problem

. 22
N-Queens Problem

. 22
N-Queens Problem

. 22
N-Queens Problem

. 22
N-Queens Problem

Do it own 8-queen problem.

. 22
Branch and bound
Branch and bound is one of the techniques used for problem solving. It is similar to the backtracking since
it also uses the state space tree. It is used for solving the optimization problems and minimization
problems. If we have given a maximization problem then we can convert it using the Branch and bound
technique by simply converting the problem into a maximization problem.We want to arrange the three
letters x, y, z in such a way that z cannot be beside x.
we’ll build a state-space tree.

23
.
Travelling Salesman Problem
You are given-
A set of some cities
Distance between every pair of cities

Travelling Salesman Problem states-


A salesman has to visit every city exactly once.
He has to come back to the city from where he starts his journey.
What is the shortest possible route that the salesman must follow to complete his tour

23
.
Travelling Salesman Problem

23
.
Travelling Salesman Problem

Rules
To reduce a matrix, perform the row
reduction and column reduction of the
matrix separately.
A row or a column is said to be
reduced if it contains at least one entry
‘0’ in it.

23
.
Travelling Salesman Problem

Step I
Row Reduction-
Reduce the elements of row-1 by 4.
Reduce the elements of row-2 by 5.
Reduce the elements of row-3 by 6.
Reduce the elements of row-4 by 2.

23
.
Travelling Salesman Problem

Column Reduction-
There is no need to reduce column-1.
There is no need to reduce column-2.
Reduce the elements of column-3 by 1.
There is no need to reduce column-4.

23
.
Travelling Salesman Problem

Finally, the initial distance matrix is


completely reduced.
Now, we calculate the cost of node-1
by adding all the reduction elements.

Cost(1)= Sum of all reduction elements

=4+5+6+2+1

= 18

23
.
Travelling Salesman Problem

Step 2
Choosing To Go To Vertex-B: Node-2
(Path A → B)
From the reduced matrix of step-01,
M[A,B] = 0
Set row-A and column-B to ∞
Set M[B,A] = ∞

Now, resulting cost matrix is-

23
.
Travelling Salesman Problem

Now,
Row Reduction
We reduce this matrix.
Then, we find out the cost of node-02.
Row Reduction-
We can not reduce row-1 as all its
elements are ∞.
Reduce all the elements of row-2 by
13.
There is no need to reduce row-3.
There is no need to reduce row-4.

23
.
Travelling Salesman Problem

Column Reduction-
Column Reduction

Reduce the elements of column-1 by 5.


We can not reduce column-2 as all its
elements are ∞.
There is no need to reduce column-3.
There is no need to reduce column-4.

23
.
Travelling Salesman Problem

Cost(2)

= Cost(1) + Sum of reduction elements


+ M[A,B]

= 18 + (13 + 5) + 0

= 36

23
.
Travelling Salesman Problem

Go To Vertex-C: Node-3 (Path A → C)


From the reduced matrix of step-01,
M[A,C] = 7
Set row-A and column-C to ∞
Set M[C,A] = ∞

23
.
Travelling Salesman Problem

Row Reduction-

We can not reduce row-1 as all its


elements are ∞.
Reduce all the elements of row-2 by
13.
There is no need to reduce row-3.
There is no need to reduce row-4.

23
.
Travelling Salesman Problem

Column Reduction-

Reduce the elements of column-1 by 5.


We can not reduce column-2 as all its
elements are ∞.
There is no need to reduce column-3.
There is no need to reduce column-4.

23
.
Travelling Salesman Problem

Cost(2)

= Cost(1) + Sum of reduction elements


+ M[A,B]

= 18 + (13 + 5) + 0

= 36

23
.
Travelling Salesman Problem

Go To Vertex-C: Node-3 (Path A → C)

From the reduced matrix of step-01,


M[A,C] = 7
Set row-A and column-C to ∞
Set M[C,A] = ∞

23
.
Travelling Salesman Problem

Column Reduction-
Row Reduction-
There is no need to reduce column-1.
We can not reduce row-1 as all its There is no need to reduce column-2.
elements are ∞. We can not reduce column-3 as all its
There is no need to reduce row-2. elements are ∞.
There is no need to reduce row-3. There is no need to reduce column-4.
There is no need to reduce row-4.
Thus, the matrix is already column
reduced.
Thus, the matrix is already
row-reduced.

23
.
Travelling Salesman Problem

we calculate the cost of node-3.

Cost(3)

= Cost(1) + Sum of reduction elements


+ M[A,C]

= 18 + 0 + 7

= 25
.

23
.
Travelling Salesman Problem

Go To Vertex-D: Node-4 (Path A → D)

From the reduced matrix of step-01,


M[A,D] = 3
Set row-A and column-D to ∞
Set M[D,A] = ∞

23
.
Travelling Salesman Problem

Performing this, we obtain the following


Row Reduction- row-reduced matrix-

We can not reduce row-1 as all its


elements are ∞.
There is no need to reduce row-2.
Reduce all the elements of row-3 by 5.
There is no need to reduce row-4.

23
.
Travelling Salesman Problem

calculate the cost of node-4.


Column Reduction-
Cost(4)

There is no need to reduce column-1. = Cost(1) + Sum of reduction elements +


There is no need to reduce column-2. M[A,D]
There is no need to reduce column-3.
We can not reduce column-4 as all its = 18 + 5 + 3
elements are ∞.
= 26
Thus, we have-

Cost(2) = 36 (for Path A → B)


Cost(3) = 25 (for Path A → C)
Cost(4) = 26 (for Path A → D) 23
.
Travelling Salesman Problem

we choose node-3 i.e. path A → C.

23
.
Travelling Salesman Problem

Step 3
Go To Vertex-B: Node-5 (Path A → C →
B)

From the reduced matrix of step-02,


M[C,B] = ∞
Set row-C and column-B to ∞
Set M[B,A] = ∞

23
.
Travelling Salesman Problem

Row Reduction-

We can not reduce row-1 as all its


elements are ∞.
Reduce all the elements of row-2 by 13.
We can not reduce row-3 as all its
elements are ∞.
Reduce all the elements of row-4 by 8.

23
.
Travelling Salesman Problem

Column Reduction-
we calculate the cost of node-5.

There is no need to reduce column-1. Cost(5)

We can not reduce column-2 as all its = cost(3) + Sum of reduction


elements are ∞. elements + M[C,B]
We can not reduce column-3 as all its = 25 + (13 + 8) + ∞
elements are ∞.
There is no need to reduce column-4. =∞

23
.
Travelling Salesman Problem

Go To Vertex-D: Node-6 (Path A → C →


D)

From the reduced matrix of step-02,


M[C,D] = 0
Set row-C and column-D to ∞
Set M[D,A] = ∞

23
.
Travelling Salesman Problem

Row Reduction-
Column Reduction-
We can not reduce row-1 as all its
elements are ∞.
There is no need to reduce
There is no need to reduce row-2. column-1.
We can not reduce row-3 as all its We can not reduce column-2 as all
its elements are ∞.
elements are ∞. We can not reduce column-3 as all
We can not reduce row-4 as all its its elements are ∞.
elements are ∞. We can not reduce column-4 as all
its elements are ∞.

Thus, the matrix is already row


reduced. 23
.
Travelling Salesman Problem
Cost(6)

= cost(3) + Sum of reduction Thus, we have-


elements + M[C,D]
Cost(5) = ∞ (for Path A → C →
= 25 + 0 + 0 B)
Cost(6) = 25 (for Path A → C →
= 25 D)

We choose the node with the


lowest cost.

Since cost for node-6 is lowest,


so we prefer to visit node-6.

Thus, we choose node-6 i.e.


path C → D. 23
.
Travelling Salesman Problem

Step-04: Choosing To Go To Vertex-B: Node-7


(Path A → C → D → B)
From the reduced matrix of step-03,
M[D,B] = 0
Set row-D and column-B to ∞
Set M[B,A] = ∞

23
.
Travelling Salesman Problem

Row Reduction-

Column Reduction-
We can not reduce row-1 as
all its elements are ∞. We can not reduce column-1 as
We can not reduce row-2 as all its elements are ∞.
We can not reduce column-2 as
all its elements are ∞. all its elements are ∞.
We can not reduce row-3 as We can not reduce column-3 as
all its elements are ∞. all its elements are ∞.
We can not reduce column-4 as
We can not reduce row-4 as all its elements are ∞
all its elements are ∞.

23
.
Travelling Salesman Problem

Cost(7)

= cost(6) + Sum of reduction elements + M[D,B]

= 25 + 0 + 0

= 25

Thus,

Optimal path is: A → C → D → B → A


Cost of Optimal path = 25 units

23
.

You might also like