0% found this document useful (0 votes)
30 views28 pages

Ada Unit V

Uploaded by

Sunshine Ratrey
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)
30 views28 pages

Ada Unit V

Uploaded by

Sunshine Ratrey
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/ 28

1. What is the 0/1 knapsack problem?

Answer-

 The 0/1 knapsack problem means that the items are either completely or no items are filled in
a knapsack.

Algorithm-
Step-01:
 Draw a table say ‘T’ with (n+1) number of rows and (w+1) number of columns.
 Fill all the boxes of 0th row and 0th column with zeroes.
Step-02:
 Start filling the table row wise top to bottom from left to right.
 Use the following formula-
T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }

 Here, T(i , j) = maximum value of the selected items if we can take items 1 to i and have
weight restrictions of j.
 This step leads to completely filling the table.
 Then, value of the last box represents the maximum possible value that can be put into the
knapsack.
Step-03:
To identify the items that must be put into the knapsack to obtain that maximum profit,
 Consider the last column of the table.
 Start scanning the entries from bottom to top.
 On encountering an entry whose value is not same as the value stored in the entry
immediately above it, mark the row label of that entry.
 After all the entries are scanned, the marked labels represent the items that must be put into
the knapsack.
Example of 0/1 knapsack problem.

Consider the problem having weights and profits are:


Weights: {3, 4, 6, 5}
Profits: {2, 3, 1, 4}
The weight of the knapsack is 8 kg
The number of items is 4
The above problem can be solved by using the following method:
First, we write the weights in the ascending order and profits according to their weights shown as
below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}

0 1 2 3 4 5 6 7 8
0
1
2
3
4

In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits and weights
of items. Here we have not taken the weight 8 directly, problem is divided into sub-problems, i.e., 0,
1, 2, 3, 4, 5, 6, 7, 8. The solution of the sub-problems would be saved in the cells and answer to the
problem would be stored in the final cell.
If no element is filled, then the possible profit is 0.
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0

For filling the first item in the bag:


0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2

For filling the second item:


0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5

For filling the third item:


0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2 3 4 4 5 6

For filling the fourth item:


0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2 3 4 4 5 6
4 0 0 0 2 3 4 4 5 6

After all the entries are computed and filled in the table, we get the following table-

0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1✔ 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3✔ 0 0 0 2 3 4 4 5 6
4 0 0 0 2 3 4 4 5 6

Time Complexity: O(N * W). where ‘N’ is the number of elements and ‘W’ is capacity.
Auxiliary Space: O(N * W). The use of a 2-D array of size ‘N*W’.
2. Explain Floyd-Warshall Algorithm?
Answer-
 Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs
of vertices in a weighted graph.
 Floyd Warshall Algorithm is an example of dynamic programming approach.
 This algorithm works for both the directed and undirected weighted graphs.
 But, it does not work for the graphs with negative cycles
 Floyd Warshall Algorithm is best suited for dense graphs.
 This is because its complexity depends only on the number of vertices in the given graph.

Algorithm- (W, M, G, Q, ∞)
A weighted graph G with M nodes is maintained in memory by its weight matrix W. This algorithm
finds a matrix Q such that Q[I, J] is the length of a shortest path from node V1 to node Vj . ∞ is a very
large number, and MIN is the minimum value function.

Step-1: Repeat for I, J = 1, 2, 3, …….. M [Initializes Q]


If
W[I, J] = 0
Then
Set Q[I, J] = ∞
Else
Set Q[I, J] = W[I, J]
[End of loop]
Step-2: Repeat steps 3 and 4 for K=1 to M [Updates Q]
Repeat step 4 for I=1 to M
Repeat step 4 for J=1 to M
Set a[I, J] = MIN(Q[I, J], Q[I, K] + Q[K, J])
[End of step 4 loop]
[End of step 3 loop]
[End of step 2 loop]
Step-3: Exit.
Complexity-
 The running time of the Floyd-Warshall algorithm is determined by the triply nested for loops
of lines 3-6. Each execution of line 6 takes O (1) time.
 The algorithm thus runs in time θ(n3 ).
Example-
 Consider the following weighted graph:
 In this graph, the vertices are represented by letters (A, B, C, D), and the numbers on the
edges represent the weights of those edges.
 To follow the Floyd-Warshall algorithm to this graph, we start by way of initializing a matrix
of distances among every pair of vertices.
 If two vertices are immediately related by using a side, their distance is the load of that edge.
 If there may be no direct edge among vertices, their distance is infinite.

 In the first iteration of the set of rules, we keep in mind the possibility of the usage of vertex 1
(A) as an intermediate vertex in paths among all pairs of vertices.
 If the space from vertex 1 to vertex 2 plus the space from vertex 2 to vertex three is much less
than the present-day distance from vertex 1 to vertex three, then we replace the matrix with
this new distance.
 We try this for each possible pair of vertices.

 In the second iteration, we recollect the possibility to use of vertex 2 (B) as an intermediate
vertex in paths among all pairs of vertices. We replace the matrix in the same manner as
earlier before.

 In the third iteration, we consider the possibility of using vertex 3 (C) as an intermediate
vertex in paths between all pairs of vertices.
 Finally, in the fourth and final iteration, we consider the possibility of using vertex 4 (D) as an
intermediate vertex in paths between all pairs of vertices.

 After the fourth iteration, we have got the shortest path between every pair of vertices in the
graph. For example, the shortest path from vertex A to vertex D is 4, which is the value in the
matrix at row A and column D.

Advantages of the Floyd-Warshall algorithm include:


1. It can discover the shortest direction between all pairs of vertices in a weighted graph, such as
graphs with negative edge weights.
2. It is an easy and smooth-to-put algorithm, making it accessible to developers of all skill
ranges.
3. It is appropriate for both dense and sparse graphs.
4. It has a time complexity of O(N^3), that is relatively efficient for most real-international
applications.
5. It can be used to discover negative weight cycles in a graph.

Disadvantages of the Floyd-Warshall set of rules include:


1. It calls for a matrix of size N^2 to store the intermediate results, which may be prohibitively
large for extremely large graphs.
2. It is not the maximum green set of rules for fixing the all-pairs shortest path hassle in sure
types of graphs, inclusive of sparse graphs or graphs with non-bad part weights.
3. It won't be suitable for real-time packages or packages with strict reminiscence constraints, as
it is able to take a long term to compute the shortest paths in very huge graphs.
4. It can be less intuitive than different algorithms, which include Dijkstra's algorithm, or the
Bellman-Ford set of rules, making it more difficult to understand for some builders.
Applications of Floyd Warshall Algorithm:

The Floyd-Warshall algorithm is a dynamic programming algorithm used for finding the shortest
course among all pairs of vertices in a weighted graph. Here are some of the programs of the Floyd-
Warshall algorithm:
1. Routing Algorithms: The Floyd-Warshall algorithm is broadly utilized in routing algorithms,
along with within the OSPF (Open Shortest Path First) protocol utilized in Internet routing. It
can help decide the shortest route among two nodes in a network and is useful in locating the
least congested path.
2. Airline Networks: The Floyd-Warshall set of rules can also be utilized in airline networks to
locate the shortest path between two cities with the lowest cost. It can assist airways plan their
routes and limit fuel charges.
3. Traffic Networks: The algorithm is used to find the shortest path between points in a visitors'
network. It can help reduce congestion and improve the go with the flow of visitors in city
areas.
4. Computer Networks: The Floyd-Warshall algorithm is likewise utilized in laptop networks
to decide the shortest course between hosts in a network. It can assist in minimizing
community latency and improve community overall performance.
5. Game Development: The set of rules may be used in game development to find the shortest
direction among two items in a sport world. It is beneficial in games in which the participant
desires to navigate through complex surroundings, together with a maze or a metropolis.
3. Explain N Queen problem
Answer-
 We have to place N queens on an N×N chessboard such that no two queens can attack each
other.
 We cannot place two queens in the same row or same column or same diagnol in order to
avoid the attack.
Algorithm-
i. Start in the leftmost column
ii. If all queens are placed return true
iii. Try all rows in the current column. Do following for every tried row.
a. If the queen can be placed safely in this row then mark this [row, column] as part of
the solution and recursively check if placing queen here leads to a solution.
b. If placing queen in [row, column] leads to solution then return true.
c. If placing queen doesn't lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.
iv. If all rows have been tried and nothing worked, return false to trigger backtracking.

Time Complexity:

 O(N!), where ‘N’ is the number of queens.


 For the first column, we check ‘N’ rows, for the second column, we check 'N - 1 rows and so
on.
 Hence time complexity will be N * (N-1) * (N-2) …. i.e N!

Space Complexity:

 O(N^2), where ‘N’ is the number of queens.


 As we are using a 2-D array of size N rows and N columns, and also we will be using a
recursion stack which will take linear space.

Example- 4-queen problem.

the implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
4. Explain Subset Sum Problem
Solution-

 In the sum of subsets problem, there is a given set with some nonnegative integer elements.
 And another sum value is also provided, our task is to find all possible subsets of the given set
whose sum is the same as the given sum value.

Follow the below steps to solve subset sum problem using the backtracking approach −
 First, take an empty subset.
 Include the next element, which is at index 0 to the empty set.
 If the subset is equal to the sum value, mark it as a part of the solution.
 If the subset is not a solution and it is less than the sum value, add next element to the subset
until a valid solution is found.
 Now, move to the next element in the set and check for another solution until all
combinations have been tried.

Complexity analysis:
 Time Complexity: O(2 ) The above solution may try all subsets of the given set in the worst
n

case. Therefore time complexity of the above solution is exponential.


 Auxiliary Space: O(n) where n is recursion stack space.

Example-

Suppose the given set and sum value is −


Set = {1, 9, 7, 5, 18, 12, 20, 15}
sum value = 35

All possible subsets of the given set, where sum of each element for every subset is the same as the
given sum value are given below −
{1 9 7 18}
{1 9 5 20}
{5 18 12}
5. Explain Graph Coloring Problem
Solution-
 We are given a graph, we need to assign colors to the vertices of the graph.
 In the graph coloring problem, we have a graph and m colors, we need to find a way to color
the vertices of the graph using the m colors such that any two adjacent vertices are not having
the same color.

To solve m-coloring problem using the backtracking approach, follow the below steps −
 Starting from vertex 0, we will try to assign colors one by one to different nodes.
 However, before assigning, we have to check whether the color is safe or not. Color is not
safe when adjacent vertices contain the same color.
 Next, we will check if is there any color assignment that satisfies the constraint. If it does, we
mark that assignment as a solution to the m-coloring problem.

Complexity Analysis

 In the backtracking approach of the graph coloring problem, we are generating


total mv combinations of the color.
 So, it also requires exponential time.

Time Complexity

 In the backtracking approach of the graph coloring problem, the time complexity is O(mv ).
 In the backtracking approach, the average time complexity is less than O(mv ).

Space Complexity

 In the backtracking approach to the graph coloring problem, we are not using any extra space
but we are using the recursive stack for the recursive function call.
 So, the space complexity is O(V).

Given:
 A graph with 4 vertices.
 Colours {Red, Blue, Green}
6. Hamiltonian Cycle Using Backtracking
Solution-
 Let G = (V, E) be a connected graph with n vertices.
 A Hamiltonian cycle is a round-trip path along n edges of G that visits every vertex once
and returns to its starting position.
 Given a graph G = (V, E) we have to find the Hamiltonian Circuit using Backtracking
approach.
 We start our search from any arbitrary vertex say 'a.'
 This vertex 'a' becomes the root of our implicit tree.
 The first element of our partial solution is the first intermediate vertex of the Hamiltonian
Cycle that is to be constructed.
 The next adjacent vertex is selected by alphabetical order.
 If at any stage any arbitrary vertex makes a cycle with any vertex other than vertex 'a' then we
say that dead end is reached.
 In this case, we backtrack one step, and again the search begins by selecting another vertex
and backtrack the element from the partial; solution must be removed.
 The search using backtracking is successful if a Hamiltonian Cycle is obtained.

Example: Consider a graph G = (V, E) shown in fig. we have to find a Hamiltonian circuit using
Backtracking method.

Solution: Firstly, we start our search with vertex 'a.' this vertex 'a' becomes the root of our implicit
tree.

Next, we choose vertex 'b' adjacent to 'a' as it comes first in lexicographical order (b, c, d).

Next, we select 'c' adjacent to 'b.'


Next, we select 'd' adjacent to 'c.'

Next, we select 'e' adjacent to 'd.'


Next, we select vertex 'f' adjacent to 'e.' The vertex adjacent to 'f' is d and e, but they have already
visited. Thus, we get the dead end, and we backtrack one step and remove the vertex 'f' from partial
solution.

From backtracking, the vertex adjacent to 'e' is b, c, d, and f from which vertex 'f' has already been
checked, and b, c, d have already visited. So, again we backtrack one step. Now, the vertex adjacent to
d are e, f from which e has already been checked, and adjacent of 'f' are d and e. If 'e' vertex, revisited
them we get a dead state. So again we backtrack one step.

Now, adjacent to c is 'e' and adjacent to 'e' is 'f' and adjacent to 'f' is 'd' and adjacent to 'd' is 'a.' Here,
we get the Hamiltonian Cycle as all the vertex other than the start vertex 'a' is visited only once. (a - b
- c - e - f -d - a).
Again Backtrack

 Time Complexity

Time Complexity for finding the Hamiltonian Cycle using the Backtracking approach
is O(N!), where N is the number of vertices in the graph.

 Space Complexity

Since we are not using any auxiliary space, the space complexity for finding the Hamiltonian
Cycle using the Backtracking approach is O(1).
7. Rat in Maze Problem

Solution-

 The rat in a maze problem is a path finding puzzle in which our objective is to find an
optimal path from a starting point to an exit point.
 In this puzzle, there is a rat which is trapped inside a maze represented by a square matrix.
 The maze contains different cells through which that rat can travel in order to reach the exit of
maze.
Rat in a Maze Problem using Backtracking Approach

 Suppose the maze is of size NxN, where cells can either be marked as 1 or 0.
 A cell marked as 1 indicates a valid path, whereas a cell marked as 0 indicates a wall or
blocked cell.

To solve the rat in a maze problem using the backtracking approach, follow the below steps −
 First, mark the starting cell as visited.
 Next, explore all directions to check if a valid cell exists or not.
 If there is a valid and unvisited cell is available, move to that cell and mark it as visited.
 If no valid cell is found, backtrack and check other cells until the exit point is reached.

The following is the binary representation of the mentioned maze.


1. {0, 1, 1, 1}
2. {0, 0, 1, 0}
3. {1, 0, 1, 1}
4. {0, 0, 0, 0}

Here, 0 means that the cell is open, and the rat can enter. 1 means the cell is blocked, and the rat
cannot enter.

The following diagram shows the path that the rat can follow to reach the destination.
Time Complexity: O(3^(m*n)), because on every cell we have to try 3 different directions.
Auxiliary Space: O(m*n), Maximum Depth of the recursion tree(auxiliary space).
8. Explain Travelling Salesman Problem.
Answer-
 You are given-
o A set of some cities
o 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.
 The solution that is needed to be found for this problem is the shortest possible route in which
the salesman visits all the cities and returns to the origin city.
Problem-

Solve Travelling Salesman Problem using Branch and Bound Algorithm in the following graph-

Solution-

Step-01:

Write the initial cost matrix and reduce it-


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.

Row Reduction-

 Consider the rows of above matrix one by one.


 If the row already contains an entry ‘0’, then-
o There is no need to reduce that row.
 If the row does not contains an entry ‘0’, then-
o Reduce that particular row.
o Select the least value element from that row.
o Subtract that element from each element of that row.
o This will create an entry ‘0’ in that row, thus reducing that row.

Following this, we have-


 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.

Performing this, we obtain the following row-reduced matrix-

Column Reduction-

Consider the columns of above row-reduced matrix one by one.

 If the column already contains an entry ‘0’, then-


o There is no need to reduce that column.
 If the column does not contains an entry ‘0’, then-
o Reduce that particular column.
o Select the least value element from that column.
o Subtract that element from each element of that column.
o This will create an entry ‘0’ in that column, thus reducing that column.

Following this, we have-


 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.

Performing this, we obtain the following column-reduced matrix-

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

Step-02:

 We consider all other vertices one by one.


 We select the best vertex where we can land upon to minimize the tour cost.

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-


Now,

 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.

Performing this, we obtain the following row-reduced matrix-

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.
Performing this, we obtain the following column-reduced matrix-

Finally, the matrix is completely reduced.


Now, we calculate the cost of node-2.

Cost(2)
= Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36

Choosing To 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] = ∞
Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-03.

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

Column Reduction-

 There is no need to reduce column-1.


 There is no need to reduce column-2.
 We can not reduce column-3 as all its elements are ∞.
 There is no need to reduce column-4.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-3.

Cost(3)
= Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25

Choosing To 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] = ∞

Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-04.

Row Reduction-
 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.

Performing this, we obtain the following row-reduced matrix-

Column Reduction-

 There is no need to reduce column-1.


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

Thus, the matrix is already column-reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-4.

Cost(4)
= Cost(1) + Sum of reduction elements + M[A,D]
= 18 + 5 + 3
= 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)

We choose the node with the lowest cost.


Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e. path A → C.

Step-03:
We explore the vertices B and D from node-3.
We now start from the cost matrix at node-3 which is-

Cost(3) = 25

Choosing To 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] = ∞

Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-5.

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.

Performing this, we obtain the following row-reduced matrix-

Column Reduction-

 There is no need to reduce column-1.


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

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-5.

Cost(5)
= cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞

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

 From the reduced matrix of step-02, M[C,D] = ∞


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

Now, resulting cost matrix is-


Now,
 We reduce this matrix.
 Then, we find out the cost of node-6.

Row Reduction-

 We can not reduce row-1 as all its elements are ∞.


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

Thus, the matrix is already row reduced.

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

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-6.

Cost(6)
= cost(3) + Sum of reduction elements + M[C,D]
= 25 + 0 + 0
= 25
Thus, we have-
 Cost(5) = ∞ (for Path A → C → B)
 Cost(6) = 25 (for Path A → C → 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.
Step-04:

We explore vertex B from node-6.


We start with the cost matrix at node-6 which is-

Cost(6) = 25

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] = ∞

Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-7.

Row Reduction-

 We can not reduce row-1 as all its elements are ∞.


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

 We can not reduce column-1 as all its elements are ∞.


 We can not reduce column-2 as all its elements are ∞.
 We can not reduce column-3 as all its elements are ∞.
 We can not reduce column-4 as all its elements are ∞.

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
All the entries have become ∞.
Now, we calculate the cost of node-7.
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

Time Complexity
 The time complexity of the dynamic programming solution is O(2^n*n^2), where n is the
number of cities.
 This is because there are 2^n possible subsets of the cities, and each subset can be filled in
with a time complexity of O(n^2).

You might also like