0% found this document useful (0 votes)
47 views19 pages

Unit 4 Important Questions With Answers

The document discusses important algorithmic problems such as the 4 Queens Problem, Hamiltonian Cycle, Graph Coloring, and the Branch and Bound method, providing explanations and solutions for each. It details the backtracking approach for the 4 Queens Problem, the NP-completeness of Hamiltonian Cycle, and the optimization nature of Graph Coloring. Additionally, it outlines the Branch and Bound method's characteristics, applications, advantages, and disadvantages, and concludes with a brief overview of solving the Traveling Salesman Problem using this method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views19 pages

Unit 4 Important Questions With Answers

The document discusses important algorithmic problems such as the 4 Queens Problem, Hamiltonian Cycle, Graph Coloring, and the Branch and Bound method, providing explanations and solutions for each. It details the backtracking approach for the 4 Queens Problem, the NP-completeness of Hamiltonian Cycle, and the optimization nature of Graph Coloring. Additionally, it outlines the Branch and Bound method's characteristics, applications, advantages, and disadvantages, and concludes with a brief overview of solving the Traveling Salesman Problem using this method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

BANSAL GROUP OF INSTITUTES

B. TECH (IV- SEMESTER)


IT 403- ANALYSIS AND DESIGN OF ALGORITHM
UNIT-IV
IMPORTANT QUESTIONS WITH SOLUTIONS

Q.1 Explain 4 queens problem with state space tree? (Most Important)
Answer: The 4 Queens Problem consists in placing four queens on a 4 x 4 chessboard so that no
two queens attack each other. That is, no two queens are allowed to be placed on the same row, the
same column or the same diagonal.

Queens Problem using Backtracking Algorithm:

Place each queen one by one in different rows, starting from the topmost row. While placing a
queen in a row, check for clashes with already placed queens. For any column, if there is no clash
then mark this row and column as part of the solution by placing the queen. In case, if no safe cell
found due to clashes, then backtrack (i.e, undo the placement of recent queen) and return false.

Step 0: Initialize a 4x4 board.


Step 1:

 Put our first Queen (Q1) in the (0,0) cell .

 'x' represents the cells which is not safe i.e. they are under attack by the Queen (Q1).

 After this move to the next row [ 0 -> 1 ].

Step 2:

 Put our next Queen (Q2) in the (1,2) cell .

 After this move to the next row [ 1 -> 2 ].

Step 3:

 At row 2 there is no cell which are safe to place Queen (Q3) .

 So, backtrack and remove queen Q2 queen from cell ( 1, 2 ) .

Step 4:

 There is still a safe cell in the row 1 i.e. cell ( 1, 3 ).

 Put Queen ( Q2 ) at cell ( 1, 3).


Step 5:

 Put queen ( Q3 ) at cell ( 2, 1 ).

Step 6:

 There is no any cell to place Queen ( Q4 ) at row 3.

 Backtrack and remove Queen ( Q3 ) from row 2.

 Again there is no other safe cell in row 2, So backtrack again and remove queen ( Q2 ) from
row 1.

 Queen ( Q1 ) will be remove from cell (0,0) and move to next safe cell i.e. (0 , 1).

Step 7:

 Place Queen Q1 at cell (0 , 1), and move to next row.


Step 8:

 Place Queen Q2 at cell (1 , 3), and move to next row.

Step 9:

 Place Queen Q3 at cell (2 , 0), and move to next row.


Step 10:

 Place Queen Q4 at cell (3 , 2), and move to next row.

 This is one possible configuration of solution

Follow the steps below to implement the idea:

 Make a recursive function that takes the state of the board and the current row number as its
parameter.

 Start in the topmost row.

 If all queens are placed, return true

 For every row.

o Do the following for each column in current row.

o If the queen can be placed safely in this column

o Then mark this [row, column] as part of the solution and recursively
check if placing queen here leads to a solution.

o If placing the queen in [row, column] leads to a solution, return true.

o If placing queen doesn’t lead to a solution then unmark this [row,


column] and track back and try other columns.

 If all columns have been tried and nothing worked, return false to trigger backtracking.
State Space Tree for 4 Queens Problem
Q.2 Explain Hamiltonian cycle problem with suitable example?
Answer: Hamiltonian Cycle or Circuit in a graph G is a cycle that visits every vertex of G exactly
once and returns to the starting vertex.

If a graph contains a Hamiltonian cycle, it is called Hamiltonian graph otherwise it is non-


Hamiltonian.

Finding a Hamiltonian Cycle in a graph is a well-known NP-complete problem, which means that
there's no known efficient algorithm to solve it for all types of graphs. However, it can be solved for
small or specific types of graphs.

The Hamiltonian Cycle problem has practical applications in various fields, such as logistics,
network design, and computer science.

Hamiltonian Path in a graph G is a path that visits every vertex of G exactly once and it doesn't
have to return to the starting vertex. It's an open path.

Similar to the Hamiltonian Cycle problem, finding a Hamiltonian Path in a general graph is also
NP-complete and can be challenging. However, it is often a more easier problem than finding a
Hamiltonian Cycle.

Hamiltonian Paths have applications in various fields, such as finding optimal routes in
transportation networks, circuit design, and graph theory research.

Problem Statement

Given an undirected graph, the task is to determine whether the graph contains a Hamiltonian cycle
or not. If it contains, then print the path, else print "Solution does not exist".

Examples:

Input: graph [] [] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 1], [0, 1, 1, 1, 0]]

Output: 0 1 2 4 3 0

Input: graph [] [] = [[0, 1, 0, 1, 0], [1, 0, 1, 1, 1], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0]]
The idea is to use backtracking to determine whether the given graph contains a Hamiltonian Cycle.
Initialize an empty path array and place the starting vertex (e.g., vertex 0) at the first position.
Recursively try to add the remaining vertices one by one. Before placing a vertex, ensure it is
adjacent to the previously added vertex and has not already been used in the path. If a valid vertex
is found, proceed forward; otherwise, backtrack and try another option.

Let's find out the Hamiltonian cycle for the following graph:

 Start with the node 0.

 Apply DFS for finding the Hamiltonian path.

 When base case reach (i.e. total no of node traversed == n (total vertex)):

o Check whether current node is a neighbour of starting node.

o As node 2 and node 0 are not neighbours of each other so return from it.
Starting from start node 0 calling DFS

 As cycle is not found in path {0, 3, 1, 4, 2}. So, return from node 2, node 4.

 Now, explore another option for node 1 (i.e node 2)

 When it hits the base condition again check for Hamiltonian cycle

 As node 4 is not the neighbour of node 0, again cycle is not found then return.

 Return from node 4, node 2, node 1.


 Now, explore other options for node 3.

Hamiltonian Cycle

 In the Hamiltonian path {0,3,4,2,1,0} we get cycle as node 1 is the neighbour of node 0.

 So, print this cyclic path.

 This is our Hamiltonian cycle.


Q.3 Explain Graph coloring problem with example?
Answer: Graph coloring refers to the problem of coloring vertices of a graph in such a way that no
two adjacent vertices have the same color. This is also called the vertex coloring problem. If
coloring is done using at most m colors, it is called m-coloring.

Chromatic Number:

The minimum number of colors needed to color a graph is called its chromatic number. For
example, the following can be colored a minimum of 2 colors.
The problem of finding a chromatic number of a given graph is NP-complete.

Graph coloring problem is both, a decision problem as well as an optimization problem.

A decision problem is stated as, "With given M colors and graph G, whether a such color scheme is
possible or not?".

The optimization problem is stated as, "Given M colors and graph G, find the minimum number of
colors required for graph coloring."

Algorithm of Graph Coloring using Backtracking:

Assign colors one by one to different vertices, starting from vertex 0. Before assigning a color,
check if the adjacent vertices have the same color or not. If there is any color assignment that does
not violate the conditions, mark the color assignment as part of the solution. If no assignment of
color is possible then backtrack and return false.

Follow the given steps to solve the problem:

 Create a recursive function that takes the graph, current index, number of vertices, and color
array.

 If the current index is equal to the number of vertices. Print the color configuration in the
color array.

 Assign a color to a vertex from the range (1 to m).

o For every assigned color, check if the configuration is safe, (i.e. check if the adjacent
vertices do not have the same color) and recursively call the function with the next
index and number of vertices else return false

o If any recursive function returns true then break the loop and return true

o If no recursive function returns true then return false


Q.4 Explain how Branch and Bound method can be used to solve any problem?
Answer: The Branch and Bound Algorithm is a method used in combinatorial optimization
problems to systematically search for the best solution. It works by dividing the problem into
smaller subproblems, or branches, and then eliminating certain branches based on bounds on the
optimal solution. This process continues until the best solution is found or all branches have been
explored. Branch and Bound is commonly used in problems like the traveling salesman and job
scheduling.

Branch and bound is an algorithmic technique used in computer science to solve optimization
problems. Branch and bound is a systematic way of exploring all possible solutions to a problem by
dividing the problem space into smaller sub-problems and then applying bounds or constraints to
eliminate certain subproblems from consideration.

Characteristics of Branch and Bound:

 Optimal solution: The algorithm is designed to find the optimal solution to an optimization
problem by searching the solution space in a systematic way.

 Upper and lower bounds: The algorithm uses upper and lower bounds to reduce the size of
the search space and eliminate subproblems that cannot contain the optimal solution.

 Pruning: The algorithm prunes the search tree by eliminating subproblems that cannot
contain the optimal solution or are not worth exploring further.

 Backtracking: The algorithm uses backtracking to return to a previous node in the search
tree when a dead end is reached or when a better solution is found.

Applications of Branch and Bound:

 Traveling salesman problem: Branch and bound is used to solve the traveling salesman
problem, which involves finding the shortest possible path that visits a set of cities and
returns from where it started to visit the set of cities.

 Knapsack problem: Branch and bound is used to solve the knapsack problem, which
involves finding the optimized combination of items to pack into a knapsack of limited
capacity.

 Resource allocation: Branch and bound is used to solve resource allocation problems, like
scheduling work on machines or assigning work to workers.

 Network optimization: Branch and bound is used to solve network optimization problems,
it helps in finding the optimized path or flow through a network.

 Game playing: Branch and bound is used in some of the game-playing algorithms, like
chess or tic-tac or 16 puzzle problem, to explore the various possible moves and find the
optimized strategies.

Advantages of Branch and Bound:

 Optimal solution: Branch and bound algorithm is created to find the best answer to an
optimization issue by methodically searching the solution space.

 Reduces search space: The algorithm uses lower and upper bounds to cut down on the size
of the search area and get rid of sub-problems that cannot have the best answer.
 Proven performance: The branch and bound approach has been used extensively in
numerous applications and has been shown to be successful in locating the best solutions to
challenging optimization problems.

 Incremental improvement: The algorithm starts with an initial lower bound and iterations
improve it until an optimized solution is found.

Disadvantages of Branch and Bound:

 Exponential time complexity: The branch and bound algorithm's worst-case time
complexity is exponential in the size of the input, making it unsuitable for handling complex
optimization issues.

 Memory-intensive: To store the search tree and the current best answer, the method needs a
lot of memory. When dealing with numerous instances of the issue, this may become a
problem.

 Sensitivity to problem-specific parameters: The quality of the problem-specific


constraints utilized determines how well the method performs, and sometimes it might be
challenging to discover good bounds.

 Limited scalability: Due to the size of the search tree which expands exponentially with the
size of the problem, the Branch and Bound technique may not scale effectively for problems
with huge search spaces.
Q.5 Solve the following travelling salesman problem using branch and bound method

Answer: Given a set of cities and the distance between every pair of cities, the problem is to find
the shortest possible route that visits every city exactly once and returns to the starting point.

Reduced Matrix: This approach is similar to the Branch and Bound approach. The difference here is
that the cost of the path and the bound is decided based on the method of matrix reduction. The
following are the assumptions for a reduced matrix:

1. A row or column of the cost adjacency matrix is said to be reduced if and only if it
contains at least one zero element and all remaining entries in that row or column ≥
0.

2. If all rows and columns are reduced then only the matrix is reduced matrix.

3. Tour length (new) = Tour length (old) – Total value reduced.

4. We first rewrite the original cost adjacency matrix by replacing all diagonal
elements from 0 to Infinity

The basic idea behind solving the problem is:

1. The cost to reduce the matrix initially is the minimum possible cost for the travelling
salesman problem.

2. Now in each step, we need to decide the minimum possible cost if that path is taken i.e., a
path from vertex u to v is followed.

3. We can do that by replacing uth row and vth column cost by infinity and then further
reducing the matrix and adding the extra cost for reduction and cost of edge (u, v) with the
already calculated minimum path cost.

4. Once at least one path cost is found, that is then used as upper bound of cost to apply the
branch and bound method on the other paths and the upper bound is updated accordingly
when a path with lower cost is found.
Following are the steps to implement the above procedure:

Step1: Create a class (Node) that can store the reduced matrix, cost, current city number, level
(number of cities visited so far), and path visited till now.

Step2: Create a priority queue to store the live nodes with the minimum cost at the top.

Step3: Initialize the start index with level = 0 and reduce the matrix. Calculate the cost of the given
matrix by reducing the row and then the column. The cost is calculated in the following way:

Row reduction - Find the min value for each row and store it. After finding the min element from
each row, subtract it from all the elements in that specific row.

Column reduction - Find the min value for each column and store it. After finding the min element
from each column, subtract it from all the elements in that specific column. Now the matrix is
reduced.

Now add all the minimum elements in the row and column found earlier to get the cost.

Step4: Push the element with all information required by Node into the Priority Queue.

Step5: Now, perform the following operations till the priority queue gets empty.

Pop the element with the min value from the priority queue.

For each pop operation check whether the level of the current node is equal to the number of
nodes/cities or not.

If yes then print the path and return the minimum cost.

If no then, for each and every child node of the current node calculate the cost by using the
formula-

Child->Cost = parent_matrix_cost + cost_from_parentTochild + Child_reducedMatrix_cost.

The cost of a reduced Matrix can be calculated by converting all the values of its rows and column
to infinity and also making the index Matrix [Col][row] = infinity.

Then again push the current node into the priority queue.

Step6: Repeat Step5 till we don't reach the level = Number of nodes - 1.

Initially the cost matrix looks like:

row/col
1 2 3 4
no

1 - 10 15 20

2 10 - 35 25

3 15 35 - 30
row/col
1 2 3 4
no

4 20 25 30 -

After row and column reduction the matrix will be:

row/col
1 2 3 4
no

1 - 0 5 10

2 0 - 25 15

3 0 20 - 15

4 0 5 10 -

and row minimums are 10, 10, 15 and 20.

row/col
1 2 3 4
no

1 - 0 0 0

2 0 - 20 5

3 0 20 - 5

4 0 5 5 -

and the column minimums are 0, 0, 5 and 10.


So the cost reduction of the matrix is (10 + 10 + 15 + 20 + 5 + 10) = 70

Now let us consider movement from 1 to 2: Initially after substituting the 1st row and 2nd
column to infinity, the matrix will be:
row/col
1 2 3 4
no

1 - - - -

2 - - 20 5

3 0 - - 5

4 0 - 5 -

 After the matrix is reduced the row minimums will be 5, 0, 0

row/col
1 2 3 4
no

1 - - - -

2 - - 15 0

3 0 - - 5

4 0 - 5 -

 and the column minimum will be 0, 5, 0

row/col
1 2 3 4
no

1 - - - -

2 - - 10 0

3 0 - - 5

4 0 - 0 -
row/col
1 2 3 4
no

 So, the cost will be 70 + cost (1, 2) + 5 + 5 = 70 + 0 + 5 + 5 = 80.

Continue this process till the traversal is complete and find the minimum cost.

Given below the structure of the recursion tree along with the bounds:

You might also like