Parallelization of the Knight`s Tour

Parallelization of the Knight`s Tour Problem Description The Knight's Tour problem uses a single chess knight on a chessboard and attempts to visit ea...
Author: Agnes Williams
0 downloads 3 Views 113KB Size
Parallelization of the Knight`s Tour Problem Description The Knight's Tour problem uses a single chess knight on a chessboard and attempts to visit each square on the board once and only once using the standard knight move (two squares right or left, and from there one square up or down, or, two squares up or down, and from there one square right or left). For travel-minded knights a tour of the entire board can be enlightening. However, in these tough economic times, a knight may not be able to afford travelling to every square. Thus, a knight with some vacation time would like to determine how many different tours of a specific length could be taken from a given starting square and end in the same starting square (a closed tour). Problem: Write a threaded code to calculate the total number of possible closed tours of a chessboard for a chess knight starting at a given square and that visit a set number of squares on the board. The names of the input and output files to be used will be given on the command line. The input file will detail the size and shape of the chessboard to be toured, the starting square of the knight, the length of the tour, and the number of tours to be fully printed in the output file. After execution of the application, the output file should contain the requested number of tours (listing of chessboard squares to visit) and a summary line with the total number of tours possible from the conditions given in the input file.

Resolution Use Case: Input File Format: The input file will contain five lines with the following format: line 1 - an integer specifying the number of columns (files) on the chessboard line 2 - an integer specifying the number of rows (ranks) on the chessboard line 3 - the starting square for the knight in algebraic notation (single lower case letter specifying column and integer corresponding to the row) line 4 - an integer specifying the number of squares to visit in the tour line 5 - an integer specifying the number of full tours to print in the output file Output File Format: Each full tour printed will be a list of board squares in algebraic notation, one square per line, starting and ending with the square given in the input file. If the tour length given is 8, then a printed tour will have 9 squares listed (the start square and the 8 destination squares with the start square as the last destination) on 9 lines. After each printed tour, a blank line or other divider line should be printed. After the requested number of tours is printed, a summary line that gives the total number of tours that qualify should be printed. If a tour with the given specifications cannot be done, the output file should contain a simple summary line that notes this fact.

Input File Example: ------------------8 8 a8 8 2 Output File Example: -------------------a8 b6 d7 f6 h7 f8 e6 c7 a8 ------a8 b6 d5 c3 a4 c5 e6 c7 a8 ------There are 880 possible 8 move tours!

Algorithm Description: It is a search problem. As a NP problem , it has no algorithm run in polynomial time. But we can use some approximate algorithms to do it. Use some reasonable pruning methods, to reduce the run time. I choose backtracking method , search every possible path from eight directions. Among that, make three means to cut of the path branchs : bounding judgment, passed site judgment(visited() function ), pruning skill. When come to the leaf, I store the paths which is necessary. And summary the successful path num.

FAQ 1. Because official mission does not give the data range, it let us difficultly to deal with the problem in the parallel connection. Especially in terms of Atomic Operation & Granularity

Adjustment. So I suggest that It may give a data range for us. 2. I still can not know how many cores the offical machine has. Can the officaln give me an answer to my email: [email protected]. The offical web page says it very fuzzy!

Serial Code See:

KnightRecursive.cpp

 Record a full tour path: vector stake[step]  Record the request step to tour: step  Record the chess table state: table[][] allpath  Record the request output paths’ num:  Record the request output paths` nodes: path[allpath][step]  Record all the successful paths: pathNum  Recursive Backtrack function: Backtrack()  Bounding judgment function: Bound()  Passed site judgment function: Visited()  Pruning function: Constraint() 1.

void Backtrack(int t, int _x, int _y)

2.

{

if (t == 0) {

// put the root node in stack: stake

3.

stake[t] = node(orx ,ory);

4.

table[orx] [ory] = 1;

5.

}

6.

if (t == step-1){

7.

// when go the second-to-last node

if (solution(t, _x, _y)){

8.

if (pathNum < allPath)

9.

{

// check if get a successful path // if not enough request path , record it

for (int i = 0; i remain ) return false; }

Performance Test table m 10 10 10

n 10 10 10

start a10 a10 a10

step 16 14 12

result paths 258822694 11258888 472186

s time 237.7 9.5 0.72

8 8 8

8 8 8

a8 a8 a8

16 14 12

80672058 5302024 316124

75.9 4.7 0.51

50 50 50

trigrams trigrams trigrams

6 6 6

6 6 6

a6 a6 a6

20 18 16

28188934 8650536 1983820

74.5 16.2 3.26

44 44 44

trigrams trigrams trigrams

10 10 10

10 10 10

e5 e5 e5

14 12 10

22783664 978112

13.2 0.75

312 312

trigrams trigrams trigrams

8 8 8

8 8 8

d4 d4 d4

16 14 12

99827588 6608498

55.5 3.4

206 206

trigrams trigrams trigrams

6 6 6

6 6 6

c3 c3 c3

20 18 16

141683336 49808152 13081500

98 29.1 6.8

94 94 94

trigrams trigrams trigrams

threads condition 50 trigrams 50 trigrams 50 trigrams

Configuration: System Type: XP Professional SP3 Processor: Intel core T2350 1.86 GHz, 2 cores Install Physical Memory(Ram): 2 GB Total Virtual Memory: 2 GB SMP: Enabled

Conclusion The problem is see as a Hamiton Loop Problem. I can only optimize the time in a small scale. There is a pair of isomorphic graph in all the paths. We can use it to reduce 1/2 time, but it is not easy to design.

References

算法设计与实验题解>> 王晓东 电子工业出版社

Suggest Documents