Dynamic Programming solution:
WRITING JOURNAL
• Problem Definition
• Theory (covering Concept of Backtracking) • Algorithms
• Analysis of above for time and space complexity • Program code
• Output for different i/p • Conclusion
Backtracking Strategy for N-Queens and Assignment Problem
Backtracking is used to solve problems with tree structures. Even problems seemingly remote to trees such as a walking a maze are actually trees when the decision 'back-left- straight-right' is considered a node in a tree.
Backtracking is the approach to find a path in a tree. There are several different aims to be achieved :
• just a path • all paths
• the shortest path
depending on the algorithm and the problem, 'just a path' is the first solution.The shortest path can only be determined when all paths are known, hence the order of the listing.
Backtracking Algorithm
As usual in a recursion, the recursive function has to contain all the knowledge. The standard implementaion is :
1. check if the goal is achieved REPEAT
2. check if the next step is possible at all
3. check if the next step leads to a known position - prevent circles 4. do this next step
So the function has to return the success, hence the use of a function. A prototype may look like :
function step(..):boolean; // true if goal reached begin
result:=false;
if goal(..) then // is this the position of the goal ? result:=true
else begin
// check all possible next steps if (step_possible(next_A))and
(step_new(next_A)) then result:=step(next_A) if (not result) and
(step_possible(next_B))and
(step_new(next_B)) then result:=step(next_B) if (not result) and
(step_possible(next_C))and
(step_new(next_C)) then result:=step(next_C) if (not result) and
(step_possible(next_D))and
(step_new(next_D)) then result:=step(next_D) ..
.. end;
N-Queens Problem
The n-queens problem consists in placing n non-attacking queens on an n-by-n chess board. A queen can attack another queen vertically, horizontally, or diagonally. E.g. placing a queen on a central square of the board blocks the row and column where it is placed, as well as the two diagonals (rising and falling) at whose intersection the queen was placed.
The algorithm to solve this problem uses backtracking, but we will unroll the recursion. The basic idea is to place queens column by column, starting at the left. New queens must not be attacked by the ones to the left that have already been placed on the board. We place another queen in the next column if a consistent position is found. All rows in the current column are checked. We have found a solution if we placed a queen in the rightmost column.
Basic Idea
A possible position on the grid is set by the pair of pointers (i,j) where 1<i,j<n , and i stands for the number of column and j stands for the number of row.Up to this point,for the same i there are n valid values for j.For a candidate solution though,only one queen
can be on each column,that is only one value j=V(i).Therefor the solutions are represented with the n values of the matrix V=[V(1),...V(n)].All the solutions for which V(i)=V(j) are rejected because 2 queens can not be on the same row.Now the solutions are the permutes of n pointers,which is n! ,still a forbiddingly big number.Out of all these solution the correct one is the one which satisfies the last requirement:2 queens will not belong in the same diagonal,which is:
V(j)-V(i)<>±(i-j) for i<>j. (5.8-1)
A backtracking algorithm or this problem constructs the permutes [V(1),....V(n)] of the {1,...,n} pointers,and examines them as to the property (5.8-1).For example there are (n- 2)! permutes in the shape of [3,4....].These will not be produced and examined if the systematically construction of them has already ordered them in a sub-tree with root [3,4] which will be rejected by the 5.8-1 condition,and will also reject all the (n-2)! permutes.On the contrary ,the same way of producing-examining will go even further to the examination of more permutes in the shape of p={1,4,2,...} since, so far the condition is satisfied.The next node to be inserted that is j:=V(4) must also satisfies these:j-1<>3,j- 4<>2,j-4<>-2,j-2<>1,j-2<>-1.All the j pointers satisfying these requirements produce the following permutes:[1,4,2,j,...] which connect to the tree as children of p.Meanwhile large sets of permutes such as [1,4,2,6,...] have already been rejected.
A typical declaration of this algorithm:The root of all solutions,has as children n nodes [1],...,[n],where [j] represents all the permutes starting with j(and whose number is (n-1)! for every j).Inductive if a node includes the k nodes {j1,...jk} we attempt to increase it
with another node { j1,...,jk,jk+1} so that the condition (5.8-1) is fulfilled.
For n=4 this method produces the indirect graph of the following picture,and does not produce the 4!=24 leaves of all the candidate solutions.
This problem considers assigning n jobs to n workers such that the cost of carrying out the job is the minimum. It is done by using a branch and bound technique in order to obtain an optimal solution where we fix values of some variables making sure that the constraints are satisfied and go on exploring the tree.
Test Input
Enter Number of Jobs: 5
For Job No. 1 Enter Cost of Assigning... Person No.1 : 10
Person No.2 : 13 Person No.3 : 17 Person No.4 : 15 Person No.5 : 21
For Job No. 2 Enter Cost of Assigning... Person No.1 : 11
Person No.2 : 16 Person No.3 : 21 Person No.4 : 23 Person No.5 : 32
For Job No. 3 Enter Cost of Assigning... Person No.1 : 17
Person No.2 : 21 Person No.3 : 31 Person No.4 : 12 Person No.5 : 16
For Job No. 4 Enter Cost of Assigning... Person No.1 : 21
Person No.2 : 22 Person No.3 : 23 Person No.4 : 24 Person No.5 : 25
For Job No. 5 Enter Cost of Assigning... Person No.1 : 12 Person No.2 : 13 Person No.3 : 14 Person No.4 : 15 Person No.5 : 16 Test Output
Minimum Cost of Job Assignment: 75 Job No. 1 Will be given to Person : 2 Job No. 2 Will be given to Person : 1 Job No. 3 Will be given to Person : 4 Job No. 4 Will be given to Person : 3 Job No. 5 Will be given to Person : 5
Test Input for N-Queens Enter Number of Queens: 4 Test Output Solution No.1 : 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0
Do You Want to See More Solutions?( [Y]es/[N]o )y Solution No.2 :
0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0
Do You Want to See More Solutions?( [Y]es/[N]o )y
Revised On: 22/06/2009 TITLE BRANCH AND BOUND
PROBLEM STATEMENT /DEFINITION
Implement following using Branch and Bound Traveling salesperson problem
OBJECTIVE • To understand Branch and Bound algorithmic strategy • To implement above problem using Branch and Bound • Analyze the above algorithms
S/W PACKAGES AND HARDWARE APPARATUS USED
Windows 2000, Turbo C++, PC with the configuration as
Pentium IV 1.7 GHz. 128M.B RAM, 40 G.B HDD, 15’’Color Monitor, Keyboard, Mouse
REFERENCES • Fundamental of Algorithms by Bressard
• Fundamentals of algorithms by Horowitz / Sahani Galgotia • Introduction to Algorithms by Cormen / Charles PHI
STEPS Refer to student activity flow chart, theory, algorithm, test input, test output INSTRUCTIONS FOR WRITING • Title • Problem Definition
JOURNAL • Theory (Covering Concept of Branch and Bound ) • State space tree formulation
• Algorithms
• Analysis of above for time and space complexity • Program code
• Output for different i/p • Conclusion.