300 likes | 581 Views
Self-Avoiding Walks of a Rook On an (M,N) Chessboard. By: Jeffrey J. Denecke Robert S. Goldstein John L. Marcantonio Polytechnic University Department of Computer Science Farmingdale, NY 11735 as prepared for Professor Alan R. Davis Instructor of Advanced Algorithms.
E N D
Self-Avoiding Walks of a RookOn an (M,N) Chessboard By: Jeffrey J. Denecke Robert S. Goldstein John L. Marcantonio Polytechnic University Department of Computer Science Farmingdale, NY 11735 as prepared for Professor Alan R. Davis Instructor of Advanced Algorithms
Some Preliminary Notes • GNU C/C++ Compiler • SMS JDK 1.1 Compiler • No code optimization based on hardware specific flags • No File Accesses • Minutes:Seconds.Fractions of a Second • ANSI C++, fork( ), pid_t
Abstract • Methods to determine the number of self-avoiding walks a rook can make on an N X M chessboard. • Two Fundamental Components: • Algebraically Derived • Algorithmically Derived • Recursive Algorithm will produce reliable results
What is a S.A.W. ? • NOT a Tool to Cut Wood! • A Walk which starts at a fixed origin • It passes through each vertex at most once
What is a S.A.W on an NxM Chessboard? • NxM two dimensional square lattice • Move rook from lower left to upper right • NO duplication of visits to a vertex • Must follow movement of a rook • Horizontal or Vertical • No Distance limitations
A Look at a Simple Solution • 3x3 Lattice => 12 Distinct Pathways • Simple Right?
It is not as Simple as it seems ... • Image => graphical solution? • Very Time Consuming • Complexity Increases dramatically as the number of vertices increases
Algebraic Solution for N=1 For N=1, (M,N)=1; For All M>0
Algebraic Solution for N=2 • For N=2, (M,N)=2m-1; For All M>0
Algebraic Solution at N=3 • Unlike its close relatives at N=1 and N=2 • No Clear Cut Answer • Function Type Formula Derived by H.L. Abbott and D. Hanson : • Exact Algebraic Formula by Abbot and Hanson :
The Derivation of the Unknown • Purely algebraic solutions become too complex as N increases beyond 2 • We must use an algorithmic approach • We have base cases for when N=1 and N=2 • Where do we go from here?
Recursion at N=3 • H.L. Abbot and D. Hanson show a recursive solution when N=3 which is: Why not apply recursion to the project as a whole?
Recursion and the S.A.W. • What is the difficulty in finding a solution to this problem? • Every possible path must be examined • In light of this, recursion is an ideal choice • Recursion allows: • Traversal of a given path • Spawn off all instances at a choice juncture • Stop when destination is reached
Performance Anticipated • Recursion algorithm appears simple, but is very costly in time and system resources • Algorithms of this kind are O(Nx) (np hard) • Where is the cost in our algorithm?
Cost in the Main Program • Nested for loops • Pseudocode: for(I=0 and < # of Columns, increment I) for(J=0 and < # of Rows, increment J) if( J >=1) traverse()
Cost Inside the Traversal Function • Multiple recursive function calls • Worst Case: 4 calls to itself • Best Case: 1 call to itself • Let’s See the pseudocode ….
PseudoCode for Traversal If( Xpos !=0 AND VertexLeft !Touched) Touch Vertex AND Traverse from Left If( Xpos !=M AND VertexRight !Touched) Touch Vertex AND Traverse from Right If( Ypos !=0 AND VertexUp !Touched) Touch Vertex AND Traverse from Up If( Ypos !=N AND VertexDown !Touched) Touch Vertex AND Traverse from Down
What does this all mean? • When N=1,2, or 3 => time is close to zero • If N=M: • Time in main consumed by nested for loops is about N2 • Time consumed by traversals is at worst a factor of 4 • Therefore we can hypothesize that for all NxM chessboards, time is O(N2)
Time Results • Acquired using UNIX system call time :
Conclusions from First Algorithm • Time Complexity is heck of a lot more than O(N2) • Time is exponential => O(NM) • Due to this time complexity, answers can only be reached by limiting the domain • Limit the domain to 6x6 maximum
Forking Processes • Call UNIX function fork() to spawn a new child process • Force the computer to compute problem in parallel rather than sequentially • Fork before traversal • forces new process for each fork to execute traversal and allow code to continue
Forking PseudoCode For( I=0 AND < # of Columns, Increment I) For( J=0 AND < # of Rows, Increment J) if(J>=1) fork() AND then traverse()
Problems With Forking • What problems can a fork cause? • Creates copy of memory • Creates copy of stack space • This eats up system resources • What about in our problem? • Computational intensive recursion creates a massive number of processes • This cripples the system
Java Implementation Using Threads • Time improved (gathered using a timing class and synchronized functions): • Sacrifice centiseconds in smaller cases • Gains in seconds for larger cases
NxN Performance We can see a distinct improvement !!!
Conclusions • A S.A.W. of a chessboard with a rook is possible to determine for limited dimensions • For larger cases, better hardware support is needed • Fork() failed because it was done in Linux on a PC as opposed to a SunSparc
Let’s Take a Look at the Java! Self-Avoiding Walks of a Rookon an (M,N) Chessboard in Java