Unit 4
Backtracking
Backtracking
• Backtracking is a problem-solving algorithmic technique that involves finding a solution
  incrementally by trying different options and undoing them if they lead to a dead end.
• It is commonly used in situations where you need to explore multiple possibilities to solve
  a problem, like searching for a path in a maze or solving puzzles like Sudoku.
• When a dead end is reached, the algorithm backtracks to the previous decision point and
  explores a different path until a solution is found or all possibilities have been exhausted.
• Backtracking can be defined as a general algorithmic technique that considers
  searching every possible combination in order to solve a computational problem.
Backtracking
General structure of a backtracking algorithm :
Backtrack(currentState):
    if currentState is a solution:
       output the solution
       return
   for each option in currentState:
       if option is valid:
          apply the option
          Backtrack(nextState)
          undo the option
Backtracking and Recursion
Types of Backtracking
1. Decision Problems
Decision problems are a class of computational problems that require a simple "yes" or "no"
answer, indicating whether a feasible solution exists or not.
a. N-Queens Problem
• The N-Queens problem involves placing N chess queens on an N×N chessboard in such
  a way that no two queens threaten each other.
• The decision version of this problem asks, "Is it possible to place N queens on the board
  such that no two queens attack each other?"
• Backtracking can systematically explore possible queen placements and return "yes" if a
  valid configuration is found, or "no" if none exists.
b. Graph Coloring Problem
• The Graph Coloring problem involves coloring the vertices of an undirected graph in such
  a way that no two adjacent vertices have the same color.
• The decision variant asks, "Is it possible to color the graph using a maximum of K colors?"
• Backtracking can search for a valid coloring by recursively assigning colors to vertices
  and backtracking when a conflict is encountered.
Types of Backtracking
2. Optimization Problems
Optimization problems are those where you need to find the best solution among a
set of feasible solutions according to a certain criterion or objective function.
a. Traveling Salesman Problem (TSP)
• The TSP involves finding the shortest possible route that visits a given set of
  cities and returns to the starting city, visiting each city exactly once.
• Backtracking can be applied to explore different permutations of the city visitation
  order and calculate the total distance for each permutation to find the optimal
  route.
b. Knapsack Problem
• In the Knapsack problem, you have a set of items, each with a weight and a
  value, and a knapsack with a maximum weight capacity. The goal is to select a
  subset of items to maximize the total value while not exceeding the knapsack's
  weight capacity.
• Backtracking can be employed to explore different item selections and determine
  the combination that yields the highest value within the weight limit.
Types of Backtracking
3. Enumeration Problems
Enumeration problems involve finding and listing all possible solutions to a problem within
certain constraints. Backtracking is particularly suited for these problems because it
systematically explores the entire solution space.
a. Permutations
• The problem of generating all permutations of a set of elements involves listing all
  possible orderings of the elements. Backtracking can be used to explore different orders
  by swapping elements and systematically generating all permutations.
b. Combinations
• Combinations involve selecting a subset of elements from a larger set without regard to
  the order of selection. Backtracking can be employed to enumerate all possible
  combinations by systematically including or excluding elements.
N-Queen Problem using Backtracking
• The N Queen is the problem of placing N chess queens on an N×N chessboard so that no
  two queens attack each other.
• It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n
  =2 and n =3.
• So first we will consider the 4 queens problem and then generate it to n - queens problem.
4-Queen Problem using Backtracking
4-Queen Problem using Backtracking
• Step 0: Initialize a 4×4 board.
4-Queen Problem using Backtracking
• Step 0: Initialize a 4×4 board.
4-Queen Problem using Backtracking
 Step 1:                                           Step 2:
 Put our first Queen (Q1) in the (0,0) cell .      Put our next Queen (Q2) in the (1,2) cell .
 ‘x‘ represents the cells which is not safe i.e.   After this move to the next row [ 1 -> 2 ].
 they are under attack by the Queen (Q1).
 After this move to the next row [ 0 -> 1 ].
4-Queen Problem using Backtracking
Step 3:                                                    Step 5:
At row 2 there is no cell which are safe to place Put queen ( Q3 ) at cell ( 2, 1 ).
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).
4-Queen Problem using Backtracking
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.
4-Queen Problem using Backtracking
Step 8:                                       Step 9:
Place Queen Q2 at cell (1 , 3), and move to   Place Queen Q3 at cell (2 , 0), and move to
next row.                                     next row.
4-Queen Problem using Backtracking
Step 10:
Place Queen Q4 at cell (3 , 2), and move to next row.
This is one possible configuration of solution
4-Queen Problem using Backtracking
8-Queen Problem using Backtracking
The eight queens problem is the problem of placing eight queens on an 8×8 chessboard
such that none of them attack one another (no two are in the same row, column, or
diagonal).
8-Queen Problem using Backtracking
8-Queen Problem using Backtracking
8-Queen Problem using Backtracking
Graph Coloring
• 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.
M-Coloring Problem
Given an undirected graph and a number m, the task is to color the given graph with at
most m colors such that no two adjacent vertices of the graph are colored with the same
color.
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.
Graph 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.
Graph Coloring Algorithm using Backtraking
• 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).
   • 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
   • If any recursive function returns true then break the loop and return true
   • If no recursive function returns true then return false
Hamiltonian Cycle
• A Hamiltonian Cycle or Circuit is a path in a graph that visits every vertex exactly once
  and returns to the starting vertex, forming a closed loop.
• 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.
• A graph is said to be a Hamiltonian graph only when it contains a hamiltonian cycle,
  otherwise, it is called non-Hamiltonian graph.
• 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.
• Hamiltonian Path in a graph G is a path that visits every vertex of G exactly once and
  Hamiltonian Path doesn’t have to return to the starting vertex. It’s an open path.
• Hamiltonian Paths have applications in various fields, such as finding optimal routes in
  transportation networks, circuit design, and graph theory research.
Hamiltonian Cycle
Example:
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}}
find out the Hamiltonian cycle for the following graph:
Start with the node 0 .
Hamiltonian Cycle
• When base case reach (i.e. total no of node traversed == V (total
  vertex)):
   •   Check whether current node is a neighbour of starting node.
   •   As node 2 and node 0 are not neighbours of each other so return from it.
   •   As cycle is not found in path {0, 3, 1, 4, 2}.
   •   So, return from node 2, node 4.
Hamiltonian Cycle
Hamiltonian Cycle
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.
Branch and Bound
• 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.
Travelling Salesman Problem
• The traveling salesman problem (TSP) is an algorithmic problem tasked with finding the
  shortest route between a set of points and locations that must be visited.
• In the problem statement, the points are the cities a salesperson might visit.
• The salesman‘s goal is to keep both the travel costs and the distance traveled as low as
  possible.
Travelling Salesman Problem
• Solution: The cost- adjacency matrix of graph G is as follows:
• costij =
• The tour starts from area H1 and then select the minimum cost area reachable from H1.
Travelling Salesman Problem
Travelling Salesman Problem
Travelling Salesman Problem
Travelling Salesman Problem
4       3 2 4 3 21 6
H1 → H6 → H7 → H8 → H5 → H2 → H3 → H4 → H1.
Thus, the minimum travel cost = 4 + 3 + 2 + 4 + 3 + 2 + 1 + 6 = 25
Travelling Salesman Problem – Dynamic Programming
• Let us consider a graph G = (V, E), where V is a set of cities and E is a set of weighted
  edges.
• An edge e(u, v) represents that vertices u and v are connected. Distance between
  vertex u and v is d(u, v), which should be non-negative.
• We have started in the city i and need to visit all the S cities once and returns to the same
  starting city i. So, we need to know which is the shortest path to visit all the city.
• Suppose we have started in city 1 and after visiting some cities now we are in the city k.
  since this will determine which cities are most convenient to visit next. We also need to
  know all the cities visited so far, so that we don’t repeat any of them.
• Let us define a term g(i, S) be the cost of the minimum cost path visiting each city in set S
  exactly once, starting at 1 and ending at i.
• We start with all subsets of size 2 and calculate g(i, S) for all subsets where S is the
  subset, then we calculate g(i, S) for all subsets S of size 3 and so on. Note that 1 must be
  present in every subset.
Travelling Salesman Problem – Dynamic Programming
• We need to start at 1 and end at k. We should select the next city in such a way that
• g{i,S} = min{Cik + g{k,S-{k}}} where k Є S
• i is a Starting point of a tour and S a subset of cities. Using this formula we are going to
  solve a problem. let see how to slove.
Travelling Salesman Problem – Dynamic Programming
Solution
g(2, Φ ) = C21 = 5
g(3, Φ ) = C31 = 6
g(4, Φ ) = C41 = 8
K = 1 , consider set of one element :
Set {2}:
g(3,{2}) = c32 + g(2, Φ ) = c32 + c21 = 13 + 5 = 18
g(4,{2}) = c42 + g(2, Φ ) = c42 + c21 = 8+ 5 = 13
Set {3} :
g(2,{3}) = c23 + g(3, Φ ) = c23 + c31 = 9 + 6 = 15
g(4,{3}) = c43 + g(3, Φ ) = c43 + c31 = 9+ 6 = 15
Set {4} :
g(2,{4}) = c24 + g(4, Φ ) = c24 + c41 = 10 + 8 = 18
g(3,{4}) = c34 + g(4, Φ ) = c34 + c41 = 12 + 8 = 20
Travelling Salesman Problem – Dynamic Programming
K = 2 , consider set of two element :
Set {3,4} :
g {2,{3,4}} = min {c23 + g(3,{4}) , c24 + g(4,{3})} = min { 9 + 20 , 10 + 15} = min { 29, 25} = 25
Set {2,4} :
g {3,{2,4}} = min {c32 + g(2,{4}), c34 + g(4,{2})} = min { 13+ 18, 12 + 13} = min { 31, 25} = 25
Set {2,3} :
g(4,{2,3}) = min {c42 + g(2,{3}), c43 + g(3,{2})} = min { 8 + 15 , 9 + 18} = min { 23, 27} = 23
Travelling Salesman Problem – Dynamic Programming
Length of an optimal tour,
g { 1, {2,3,4}} = min{ c12 + g(2,{3,4}), c13 + g(3,{2,4}), c14 + g(4,{2,3})}
= min { (25 + 10 ) , (25 + 15) , (23 + 20) }
= min { ( 35), (40), (43)}
= 35
The minimum cost path is 35.
The optimal tour route is, 1 -> 2 -> 4 -> 3 -> 1 .