Knight's Tour Speedup.

Reply

Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Knight's Tour Speedup.

 
0
  #1
Jun 11th, 2008
Hiya fellas,

I just made this Knight's Tour program, and wish to speed it up without making it non-brute force. So it shouldn't have any intelligence.

I did most I could, and was wondering if you guys know more ways to speed this program up. First thing I thought about is changing the recursive "solveit" to an iterative function, but will this really speed the program up, since it's maximum "call depth" is 64?

The code is attached as well as below. Any other comments are welcome as well.

Thanks in Advance,
Nick

PS:
Code is attached and below:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5.  
  6. #define NODEBUG
  7.  
  8. #define TIMEELAPSED {printf("\tTime elapsed: %lu\n", GetTickCount() - startTime);}
  9.  
  10. const int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
  11. const int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
  12.  
  13. unsigned int boardsSolved = 0;
  14. unsigned char board[8*8] = {1};
  15. unsigned int startTime;
  16.  
  17. void printBoard(const unsigned char *board){
  18. unsigned int i, j;
  19.  
  20. for(i = 0; i < 8; i++){
  21. for(j = 0; j < 8; j++){
  22. printf("%02u ", board[i*8+j]);
  23. }
  24. putchar('\n');
  25. }
  26. putchar('\n');
  27.  
  28. return;
  29. }
  30.  
  31. int solveit(const unsigned int *knightPos, const unsigned int steps){
  32. if(steps == 64){
  33. printf("Board (%u) solved:\n", ++boardsSolved);
  34. printBoard(board);
  35. TIMEELAPSED
  36. return 0;
  37. }
  38.  
  39. /*Algo:
  40.   calc xpos
  41.   if xpos is valid:
  42.   calc ypos
  43.   if ypos is valid:
  44.   if board[x][y] is not stepped on
  45.   set board[x][y] to steps
  46.   call self with updated position and board
  47.   */
  48.  
  49. unsigned int i;
  50. unsigned int curPos[2];
  51.  
  52. for(i = 0; i < 8; i++){
  53. curPos[0] = knightPos[0] + horizontal[i];
  54. if(curPos[0] > 7){
  55. continue;
  56. }
  57. else{
  58. curPos[1] = knightPos[1] + vertical[i];
  59. if(curPos[1] > 7){
  60. continue;
  61. }
  62. else{
  63. if(board[curPos[0] + curPos[1]*8] == 0){
  64. board[curPos[0] + curPos[1]*8] = steps+1;
  65.  
  66. #ifdef DEBUG
  67. printf("Board after %u steps: \n", steps);
  68. printBoard(board);
  69. #endif
  70. solveit(curPos, steps+1);
  71. board[curPos[0] + curPos[1]*8] = 0;
  72. }
  73. else{
  74. continue;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. #ifdef DEBUG
  81. else{
  82. printf("All possibilities tried, returning.\n");
  83. }
  84. #endif
  85.  
  86. return -1;
  87. }
  88.  
  89.  
  90. int main(){
  91. printf("Knights Tour Finder v0.1\n");
  92. startTime = GetTickCount();
  93.  
  94. unsigned int startPos[2] = {0};
  95.  
  96. printf("Starting with this board: \n");
  97. printBoard(board);
  98.  
  99. solveit(startPos, 1);
  100.  
  101. printf("Done");
  102. return 0;
  103. }
Attached Files
File Type: c KnightsTourVijf.c (2.1 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: Knight's Tour Speedup.

 
0
  #2
Jun 11th, 2008
Recalculating the time in every recursive call, get's quite expensive. Perhaps depth mod 8 == 0 would be enough?

Also, you re-draw the board for every move. You only need to redraw two squares after any move - the from square, and the to square. It also gets rid of any possible screen flicker.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Knight's Tour Speedup.

 
0
  #3
Jun 11th, 2008
That's not entirely correct I'm afraid.

I only display the board when it is solved, that's also when I calculate the difference in time.

Notice the #ifdef DEBUG in front of the "print the board after every move" line. That line only gets compiled when DEBUG is defined, which it isn't. It was there for, naturally, debugging purposes.
Last edited by Clockowl; Jun 11th, 2008 at 5:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: Knight's Tour Speedup.

 
0
  #4
Jun 11th, 2008
Originally Posted by Clockowl View Post
That's not entirely correct I'm afraid.

I only display the board when it is solved, that's also when I calculate the difference in time.

Notice the #ifdef DEBUG in front of the "print the board after every move" line. That line only gets compiled when DEBUG is defined, which it isn't. It was there for, naturally, debugging purposes.
Then you need to start bringing the facts forward, and start including data: How long does your program take to run, and on what hardware?

Have you profiled it yet, and what were the results?

What compiler are you using, and with what flags?

I'm sure you could google around and find other versions of this program. How does your program compare to them, when run on the same hardware and compiler?

That will give you real insight into the performance of your program.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Knight's Tour Speedup.

 
0
  #5
Jun 11th, 2008
It's doesn't matter how long it takes to run, I want it to run faster. Improving this algorithm.

And relax, I was only saying you made a mistake in your previous post and explained it.

I'm using GCC with the -o3 and -Wall flag. That's all.
Last edited by Clockowl; Jun 11th, 2008 at 8:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knight's Tour Speedup.

 
0
  #6
Jun 12th, 2008
before i go any further, i want to know what kind of speeds you're currently getting. time per solution.

also, do you require closed solutions, or are open solutions acceptable?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Knight's Tour Speedup.

 
0
  #7
Jun 12th, 2008
Why does it matter what speeds I'm getting? I want it to be faster. You can't meassure this speed very well since it will "hang" on the same solution for quite a long time and then sometimes spawn 500 solutions in 2 seconds. It gets to 3498th solution in about 24 minutes on my system.

Open solutions are acceptable.

There's a good benchmark at solution nr. 823. It'll get there in about 70 seconds here and then hang for some time.
Last edited by Clockowl; Jun 12th, 2008 at 8:23 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knight's Tour Speedup.

 
0
  #8
Jun 12th, 2008
Why does it matter what speeds I'm getting?
why so hostile?

i want to know if its worth my time to post my solution. if you're getting speeds as fast as mine, i wont bother.

anyhow, the Warnsdorff algorithm, as ive coded it, solves each of the 64 positions on the board in 15 milliseconds or less. This is a 1.8 GHz machine.

mind you, this is just "solving" any given position. I could keep running it at that rate indefinitely, but if i had to keep track of unique solutions, it would add some overhead.
Last edited by jephthah; Jun 12th, 2008 at 11:51 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Knight's Tour Speedup.

 
0
  #9
Jun 12th, 2008
Sorry if that sounded hostile, I was honestly wondering why it mattered.

Are you saying that for any given position you find *all* the solutions in 15ms? Warnsdorff only finds a set, no? If so, please post your implementation so I can study it. Thank you.
Last edited by Clockowl; Jun 12th, 2008 at 11:57 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Knight's Tour Speedup.

 
0
  #10
Jun 12th, 2008
i dont have my code available to me here at work, its on my laptop at home.

all i did was write code to implement the Warnsdorff Algorithm. its a couple hundred years old, and it's no secret.

you could implement it yourself and get the same results i did.

http://web.telia.com/~u85905224/knight/eWarnsd.htm

the pros of this method is that it's accurate (~98%) and its FAST. when i said 15 milliseconds or less, that was the maximum total time to find one solution per square, for ALL of the 64 squares in succession. so, approximately 250 microseconds per solution, or less. (and actually that was on my laptop, an AMD Athelon X2 (2.something GHz per core)

the real flaw with it is that it won't find all possible solutions, which may be unacceptable depending on your requirements. it can find multiple solutions per square, some closed and some open, but it wont find them all.




.
Last edited by jephthah; Jun 12th, 2008 at 1:50 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC