Ada Unit V
Ada Unit V
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.
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
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.
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.
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:
Space Complexity:
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
Example-
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
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).
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.
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:
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-
Column Reduction-
Cost(1)
= Sum of all reduction elements
=4+5+6+2+1
= 18
Step-02:
Row Reduction-
Column Reduction-
Cost(2)
= Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36
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-
Cost(3)
= Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25
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.
Column Reduction-
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)
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
Now,
We reduce this matrix.
Then, we find out the cost of node-5.
Row Reduction-
Column Reduction-
Cost(5)
= cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞
Row Reduction-
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 ∞.
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)
Cost(6) = 25
Now,
We reduce this matrix.
Then, we find out the cost of node-7.
Row Reduction-
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).