Design & Analysis of algorithm- 5
Design & Analysis of algorithm- 5
The following ways to fill the color into the cylinder are −
1. Blue, Orange, Yellow
2. Blue, Yellow, Orange
3. Orange, Blue, Yellow
4. Orange, Yellow, Blue
5. Yellow, Blue, Orange
6. Yellow, Orange, Blue
● Depending upon the constraints we solve the solution based on the constraints. For example,
we remove the orange from the end of every node and this shows the reduction of time
complexity of the problem.
○ It completely searches the state space tree by targeting the optimal solution.
Nodes This is a state space tree where the node This explored the optimization
explored the depth-first search. problem.
Traverse It traverses the tree by depth-first search It traverses the tree by breadth-first
search
Solves Backtracking can solve the game of chess and It doesn’t solve any game problem.
sudoku.
1 2 3 4 5
1 ∞ 20 30 10 11
2 15 ∞ 16 4 2
3 3 5 ∞ 2 4
4 19 6 18 ∞ 3
5 16 4 7 16 ∞
As we can observe now that the second row Since all the rows and columns have atleast one
contains one zero value. zero value. Therefore, we can say that this matrix is
already reduced. So, there would be no reduction
The cost of reduction of node 5 is c(1, 5) + r + r` = 1 cost. The cost of reduction of node 2 is c(4, 2) + r +
+ 25 + 5 = 31 r` = 3 + 25 + 0 = 28
Since the node 4 has the minimum cost, i.e., 25. So
we will explore the node 4 first. From the vertex 4,
we can go either to the vertex 2, 3 or 5 as shown in
the below image:
Now we have to calculate the cost of the path from
the vertex 4 to 2, vertex 4 to 3, and vertex 4 to 5.
Here, we will use the matrix of node 4 to find the
● In this graph,
● No two adjacent vertices are colored with the same color.
● Therefore, it is a properly colored graph.
Illustration:
● To color the graph, color each node one by one.
● To color the first node there are 3 choices of colors Red, Green and Blue, so lets take the red
color for first node.
● After Red color for first node is fixed then we have made choice for second node in similar
manner as we did for first node, then for 3rd node and so on.
● There is one important point to remember. while choosing color for the node, it should not
be same as the color of the adjacent node.
● int main() {
● int colors = 3; // Number of colors
● checkSolution (colors);
● }
| / \ |
|/ \|
(3)-------(4)
Output: [1,2],[2,1]
Output: []
● In the above example, we have taken an array, and the empty array named result that
stores all the values whose resultant sum is equal to 9.
● First element in an array is 3. There are two scenarios:
● First scenario is select. The sum is equal to the target sum - value of first element, i.e., 9 -
3 = 6 and the first element, i.e., 3 gets stored in the result array, i.e., result[].
● Second scenario is reject. The array arr contains the elements 4, 5, 2, i.e., arr = [4, 5, 2]
and sum would be same as 9 as we are rejecting the element 3. The result[] array would
remain empty.
Hands on
Doubts/ Tools Exercise
Questions
A Welcome Contacts
Demonstration Break