DAA Unit 4 Part 2
DAA Unit 4 Part 2
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.
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 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}}
• 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.
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 .