ADS Unit 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

UNIT-4

Advanced Data Structures & Algorithm Analysis


(23ACS05)
Backtracking
• Its problem-solving algorithmic technique
• Its involves finding a solution 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 Graph.
• 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.
Types of Backtracking Problems
Backtracking can be categorized into 3 categories:

• Decision Problems: Here, we search for a feasible solution.


• Optimization Problems: For this type, we search for the best
solution.
• Enumeration Problems: We find set of all possible feasible
solutions to the problems of this type.
How does Backtracking works?
How does Backtracking works?
• Backtracking algorithm explores each and every possible
path in order to find a valid solution.
• “IS” represents the Initial State
• C : it represents different Checkpoints for recursive calls.
• TN: represents the Terminal Nodes where no further recursive
calls can be made and we determine whether the current
solution is valid or not at this state.
• For example in the above image TN1…TN5 are the terminal
node where the solution is not acceptable, while TN6 is the
state where we found a valid solution.
8 Queens Problem using Backtracking

The eight queens problem is the problem of placing 8 queens


on an 8×8 chessboard….

 Each Row & Column should have One Queen


 N Queens problem N x N Chessboard
 Not 2 Queen Should be in same Row | Column | Diagonally.
Sum of Subsets Problem
• In the sum of subsets problem, there is a given set with
some non-negative integer elements.
• Another sum value is also provided, our task is to find all
possible subsets of the given set whose sum is the same
as the given sum value.
Introduction
Example
Example:
Another Example:
Another Method:
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.
• The minimum number of colors needed to color a graph is
called its chromatic number.
Objectives:
Method to Color a Graph
Goal of Graph Coloring
Goal of Graph Coloring
Algorithm of Graph Coloring
Algorithm of Graph Coloring
Branch and Bound: The General Method
• Branch and Bound is a method used to find the optimization
solution.
• Similar to Backtracking ie. DFS is used Depth.
• The solution space is too large for exhaustive search.
• A feasible solution space needs exploration with optimal
selection.
• Problems require exact solutions rather than approximate
answers.
• B&B is often applied in Integer Linear Programming (ILP), the
Traveling Salesman Problem (TSP), and Knapsack problems.
Core Concepts of B&B
Branching:
• Divides the problem into smaller subproblems.
• Each subproblem represents a subset of the overall solution
space.
• The process is visualized as Tree Structure, with nodes
representing the subproblems.

Bounding:
• Computes bounds (upper or lower) on the best possible solution
in each branch.
• If a branch's bound is worse than best known solution, it is
discarded or “pruned.”
Core Concepts of B&B
Pruning:
• Eliminates subproblems (nodes) that cannot yield a better
solution than the current best.
• This step reduces the search space considerably.
• Making the algorithm more efficient.

Search Strategies:
• B&B relies on specific search techniques to choose which
branches to explore, including depth-first, breadth-first, and
best-bound strategies.
Steps in the B & B Algorithm

1.Initialization: Start with the root node representing the entire


problem. If available, set an initial solution as the best known
solution.
2.Branching: Break the current problem (node) into subproblems
by making a decision (e.g., assign or exclude a variable, restrict
constraints).
3.Bounding: For each subproblem, calculate a bound:
a. If the bound is worse than the current best solution, prune
this subproblem.
b. If the bound is better, keep it for further exploration.
Steps in the B & B Algorithm

4. Pruning: Prune nodes based on bounding results, which


significantly reduces the number of nodes to explore.

5. Node Selection: Choose the next node to explore based on a


specific strategy (e.g., best-bound, depth-first, or breadth-first).

6. Termination: When no nodes are left for exploration, the best


solution obtained is declared optimal.
0/1 Knapsack using Branch and Bound
• The backtracking based solution works better than brute
force by ignoring infeasible solutions.
• We can do better (than backtracking) if we know a bound on
best possible solution subtree rooted with every node.
• If the best in subtree is worse than current best, we can
simply ignore this node and its subtrees.
• So we compute bound (best solution) for every node and
compare the bound with current best solution before
exploring the node.
How to find bound for every node for 0/1 Knapsack?
• The idea is to use the fact that the Greedy approach
provides the best solution for Fractional Knapsack problem.
• To check if a particular node can give us a better solution or
not, we compute the optimal solution (through the node)
using Greedy approach.
• If the solution computed by Greedy approach itself is more
than the best so far, then we can’t get a better solution
through the node.
Follow the steps to implement the above idea:
• Sort all items in decreasing order of ratio of value per unit weight so
that an upper bound can be computed using Greedy Approach.

• Initialize maximum profit, maxProfit = 0, create an empty queue, Q,


and create a dummy node of decision tree and enqueue it to Q.
Profit and weight of dummy node are 0.

• Do following while Q is not empty.


Extract an item from Q. Let the extracted item be u.
Compute profit of next level node. If the profit is more
than maxProfit, then update maxProfit.
Compute bound of next level node. If bound is more
than maxProfit, then add next level node to Q.
Consider the case when next level node is not considered as
part of solution and add a node to queue with level as next, but
Branch and Bound

You might also like