DAA - Module 2 - Part2
DAA - Module 2 - Part2
FUNDAMENTAL ALGORITHMIC
STRATEGIES
Contents:
Algorithm strategies
- Divide & Conquer
- Brute-Force
- Greedy
- Dynamic programming
- Backtracking
- Branch-and-Bound
Problem Solving
- Bin Picking
- Knapsack
- Travelling Salesperson Problem
Heuristics – characteristics & their application Domain.
Backtracking
Contents:
The General Method.
Control Method: Recursive & Iterative .
Sum of Subsets.
8- Queens Problem.
Graph Colouring.
0/1 Knapsack problem.
Hamiltonian Cycle.
Backtracking
Introduction :
Backtracking is a algorithm design strategy.
The solution can be represented in the form of a tree(solution tree/state space tree).
Backtracking
B1 B2 G1 B1 B2 G1
B2 G1 B1 G1 B1 B2 B2 G1 B1 G1 B1 B2
B B
G1 B2 G1 B1 B2 B1 G1 G1 B2 B1
Solution:
Fig 4: Portion of state space tree showing Ist subset some 1) x[1:6] = x(1, 1, 0, 0, 1, 0)
to 30 2) x[1:6] = x(1, 0, 1, 1, 0, 0)
3) x[1:6] = x(0, 0, 1, 0, 0, 1)
Backtracking: Sum of Subsets
Sum of Subsets: Algorithm
Fig. 5 Possible State Space tree organization for sum of subsets (Variable-size).
1 2 3 4
1
2
3
4
Let G be a graph and m be a given positive number. We want to discover whether the
Nodes of G can be colored in such a way that no two adjacent nodes have the same
color yet only m colors are used. This is termed as m-colorability decision problem.
The m-colorability optimization problem asks for the smallest integer m for which
the graph G can be colored. This integer is referred to as the chromatic number of
the graph.
A graph is said to be planar iff it can be drawn in a plane in such a way that no two
edges cross each other.
Backtracking: Graph Coloring
Graph Coloring : Problem
Fig 9. State space tree for mColoring when n=3 & m=3
Backtracking: Graph Coloring
Graph Coloring : Problem
Find all m- coloring combinations for the following graph using Backtracking
approach(SST) that selects color R for vertex 1. Three colors available are {R, G, B}.
Contents:
The Method.
- FIFO Branch-and Bound
- LIFO Branch-and Bound
- LC Branch-and-Bound
But unlike Backtracking, Branch & Bound design strategy is useful for solving
optimization (only minimization not maximization) problems.
But any way, we can covert maximization problems into minimization problem
and then we can solve it branch & bound.
Branch And Bound
Branch & Bound:The Method
Example : Let n=4, (p1 , p2 , p3 ) = (100, 10, 15, 27) and (d1 , d2 , d3 ) = (2, 1, 2, 1).
Find all feasible solutions and their values .
Solution : FIFO (First-in-First-Out) B & B Variable size
Fig. 1 FIFO B&B State space tree for variable size tuple Q 2 3 4 5 6 7 9
Branch And Bound
Branch & Bound:The Method
Example : Let n=4, (p1 , p2 , p3 ) = (100, 10, 15, 27) and (d1 , d2 , d3 ) = (2, 1, 2, 1).
Find all feasible solutions and their values .
Solution : LIFO (Last-in-First-Out) B & B Variable size
5 6
4
3
2
S
Fig. 2 LIFO B&B State space tree for variable size tuple
Branch And Bound
Branch & Bound:The Method
Example : Let n=4, (p1 , p2 , p3 ) = (100, 10, 15, 27) and (d1 , d2 , d3 ) = (2, 1, 2, 1).
Find all feasible solutions and their values .
Solution : LC (Least Cost) B & B Variable size
Fig. 3 Least Cost B&B State space tree for variable size tuple
Branch And Bound
Control Abstraction : LC Search
For any job i profit pi is earned iff the job is completed by its deadline. To complete
a job, one has to process the job for one given unit of time ti > 0.Only one machine
is available for processing the jobs.
B&B: JSWD
Job Sequencing With Deadlines :
Problem : : Let n=4, (p1 , p2 , p3 , p4 ) = (5, 10, 6, 3), (d1 , d2 , d3 , d4 ) = (1, 3, 2, 1) and
(t1 , t2 , t3 , t4 ) = (1, 2, 1,1). Find optimal solution using variable tuple size FIFO B&B.
Solution:
Jobs -> 1 2 3 4
Profit Penalty 5 10 6 3
deadline 1 3 2 1
time 1 2 1 1
B&B: JSWD
Job Sequencing With Deadlines : 1 2 3 4
Jobs ->
Solution: Profit Penalty 5 10 6 3
deadline 1 3 2 1
time 1 2 1 1
Solution:
x[] = x[J2, J3]
Penalty = 10+ 6 = 16
x[] = x[J1, J4]
Penalty = 5 + 3 = 8
Fig. 3 FIFO B&B State space tree for variable size of problem solution
B&B: 0/1 Knapsack
0/1 Knapsack problem: LC B & B :
Need to find upper bound & cost of every node in the state space tree.
Similarly all upper bound & cost values are also negative.
B&B: 0/1 Knapsack
0/1 Knapsack problem: Solve the 0/1 Knapsack problem using LC B & B
approach for the instance : n =4, (w1 , w2 , w3 , w4) = (2, 4, 6, 9), (p1 , p2 , p3 , p4) =
(10, 10, 12, 18) , & m= 15.
Solution :
Solution:
x[1:4] = x[1, 1, 0, 1]
Profit = 10+ 10 + 0 + 18 = 38
Cost/Knp wt = 2+4+0+9 = 15
Fig. 3 LC branch-and bound state space tree for the problem (Fixed size tuple)
B&B: 0/1 Knapsack
0/1 Knapsack problem: Solve the 0/1 Knapsack problem using LC B & B
approach for the instance : n =4, (w1 , w2 , w3 , w4) = (2, 4, 6, 9), (p1 , p2 , p3 , p4) =
(10, 10, 12, 18) , & m= 15.
Solution :
Fig. 3 FIFO branch-and bound state space tree for the problem
B&B: 0/1 Knapsack
Let c(.) be a cost function and be a cost function with reduced matrix.
A row(column) of a matrix is said to be reduced iff it contains at least one zero
& all other entries are non-negative.
A matrix is said to be reduced iff every row & column is reduced.
For upper bound function u, we can use u(R) = ∞ for all nodes R.
That’s it.