Backtracking - N-Queens Problem, Sum of Subsets, Graph Colouring, Hamiltonian Cycle
UNIT - V : BACKTRACKING
General Method – 8-Queens Problem – Sum Of Subsets – Graph Coloring – Hamiltonian Cycles
General Method
Backtracking is a problem-solving technique used in algorithms to find solutions by exploring all possible options. In backtracking, a state-space tree is a tree-like structure that represents all possible states (solutions or non-solutions) of a problem. Backtracking algorithms explore this tree using a depth-first search approach, systematically checking each path until a solution is found or all possibilities have been exhausted. Here’s how it works, step-by-step:
- Start with a choice: Make a decision and move
forward.
- Check feasibility: If this choice leads to a
solution or partial solution, continue. If not, stop exploring this path.
- Go back and try a different path: If you reach a dead end, go back to the last point where you made a choice. Try a different path from there.
- Repeat until you find a solution
or exhaust all possibilities.
Backtracking is commonly used in puzzles and problems where you need to explore multiple paths, like mazes, Sudoku, and the N-Queens problem.
The N-Queens Problem is a classic puzzle where the goal is to place N queens on an N×N chessboard so that no two queens can attack each other.
In simple terms:
- Goal: Place N queens on the board, with one queen in each row and each column.
- Rules: Queens should not be able to attack each other, which means no two queens can be in the same row, column, or diagonal.
- Backtracking: Place queens one by one, checking each time if it’s safe. If you can’t place a queen without conflict, "backtrack" by removing the last placed queen and trying a new position.
The time complexity of the N-Queens problem using backtracking is O(N!).
The N-Queens Problem demonstrates backtracking because you keep trying different placements and undo choices when conflicts arise.
Sum of Subsets
The Sum
of Subsets problem in backtracking involves finding subsets of a given set
of numbers that add up to a specific target sum.
- Goal: Find combinations of
numbers in a set that add up exactly to a target sum.
- Process: Start with an empty
subset, add numbers one by one, and keep checking the sum.
- Backtrack if needed: If the sum becomes too
large, "backtrack" by removing the last number and trying the
next one.
- Repeat until you’ve checked all
possible combinations of numbers in the set.
Backtracking is helpful here because it lets us stop exploring combinations early if they exceed the target sum, making the process faster.
Example:
Consider the sum-of-subset problem, n = 4, Set S={3,5,6,7} and Sum = 15. Show the state-space tree leading to the solution. Also, number the nodes in the tree in the order of recursion calls.
Graph Colouring
Graph colouring is a way of assigning colours to
the nodes (or vertices) of a graph so that no two connected nodes have the same
color.
- Goal: Color each node in a graph
with the minimum number of colours so that no two connected nodes share
the same color.
- Rules: If two nodes are connected
by an edge, they must be different colours.
- Backtracking (optional): In some cases,
backtracking is used to try colours and go back if they lead to a
conflict, ensuring a correct colouring.
Graph
colouring is often used in scheduling problems, like assigning time slots to
exams so no two exams with overlapping students are scheduled at the same time.
The time complexity of the graph colouring problem using backtracking is O(m^V), where
- m: Number of colors available
- V: Number of vertices in the graph
Hamiltonian Cycle
A Hamiltonian
Cycle is a path in a graph that visits every node (or vertex) exactly once
and then returns to the starting node, forming a cycle.
In simple
terms:
- Goal: Find a cycle that visits
every node once and returns to the starting point.
- Backtracking: Start at a node and try to
visit each neighbouring node one by one, marking nodes as visited.
- Undo if necessary: If a path doesn’t lead to
a cycle, "backtrack" by unmarking the last node and trying a
different neighbour.
- Repeat until you either find a
Hamiltonian Cycle or exhaust all possible paths.
Backtracking
is useful here because it allows you to explore all possible routes
efficiently, discarding paths that can’t complete the cycle early.
The time complexity of the Hamiltonian Cycle problem using backtracking is O(N!) in the worst case, where N is the number of vertices in the graph.
Comments
Post a Comment