Lec26_BackTracking

Lec26_BackTracking - Backtracking Sum of Subsets and...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Backtracking Sum of Subsets and Knapsack
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking Two versions of backtracking algorithms Solution needs only to be feasible (satisfy problem’s constraints) sum of subsets Solution needs also to be optimal knapsack
Background image of page 2
The backtracking method A given problem has a set of constraints and possibly an objective function The solution optimizes an objective function, and/or is feasible. We can represent the solution space for the problem using a state space tree The root of the tree represents 0 choices , Nodes at depth 1 represent first choice Nodes at depth 2 represent the second choice , etc. In this tree a path from a root to a leaf represents a candidate solution
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sum of subsets Problem : Given n positive integers w 1, ... w n and a positive integer S. Find all subsets of w 1, ... w n that sum to S. Example : n=3, S=6, and w 1 =2, w 2 =4, w 3 =6 Solutions : {2,4} and {6}
Background image of page 4
Sum of subsets We will assume a binary state space tree . The nodes at depth 1 are for including ( yes, no ) item 1, the nodes at depth 2 are for item 2, etc. The left branch includes w i , and the right branch excludes w i . The nodes contain the sum of the weights included so far
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sum of subset Problem: State SpaceTree for 3 items: w 1 = 2, w 2 = 4, w 3 = 6 and S = 6 i 1 i 2 i 3 yes no 0 0 0 0 2 2 2 6 6 12 8 4 4 10 6 yes yes no no no no no no The sum of the included integers is stored at the node. yes yes yes yes
Background image of page 6
A Depth First Search solution Problems can be solved using depth first search of the (implicit) state space tree. Each node will save its depth and its (possibly partial) current solution DFS can check whether node v is a leaf. If it is a leaf then check if the current solution satisfies the constraints Code can be added to find the optimal solution
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A DFS solution Such a DFS algorithm will be very slow. It does not check for every solution state ( node ) whether a solution has been reached, or whether a partial solution can lead to a feasible solution Is there a more efficient solution?
Background image of page 8
Backtracking Definition: We call a node nonpromising if it cannot lead to a feasible (or optimal) solution, otherwise it is promising Main idea : Backtracking consists of doing a DFS of the state space tree, checking whether each node is promising and if the node is nonpromising backtracking to the node’s parent
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking The state space tree consisting of expanded nodes only (called the pruned state space tree ) The following slide shows the pruned state space tree for the sum of subsets example There are only 15 nodes in the pruned state space tree The full state space tree has 31 nodes
Background image of page 10
A Pruned State Space Tree (find all solutions) w 1 = 3, w 2 = 4, w 3 = 5, w 4 = 6; S = 13 0 0 0 3 3 3 7 7 12 8 4 4 9 5 3 4 4 0 0 0 5 5 0 0 0 0 6 13 7 Sum of subsets problem
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Backtracking algorithm void checknode (node v ) { node u if ( promising ( v )) if ( aSolutionAt ( v )) write the solution else //expand the node for ( each child u of v ) checknode ( u ) }
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 42

Lec26_BackTracking - Backtracking Sum of Subsets and...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online