self-avoiding walks of a rook on an (m,n) chessboard by: jeffrey j. denecke robert s. goldstein john...
TRANSCRIPT
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
It is not as Simple as it seems ...
• Image => graphical solution?
• Very Time Consuming
• Complexity Increases dramatically as the number of vertices increases
m=n W(n,m)1 12 23 124 1845 85126 12628167 5757805648 7893600532529 326659848698164010 41044208702632400000
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 :
0221
1
)1)(13(
)1)(1(*
)!1(
1)3,(
xm
m
xxxx
xx
dx
d
mmW
• Exact Algebraic Formula by Abbot and Hanson :
m
m
2
21
...)239718173540210.1...(28143193375245.02
133
2
133
132
134
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:
W(1,3) =1, W(2,3)=4, W(3,3) =12, W(4,3)= 38
W(m,3) = 4 * W(m-1,3) + 3* W(m-2,3) + 2* W(m-3,3) + w(m-4,3) for m >=5
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 :
N/M 2 3 4 5 6 72 00:00.00 00:00.00 00:00.00 00:00.00 00:00.01 00:00.013 00:00.00 00:00.00 00:00.01 00:00.03 00:00.10 00:00.344 00:00.00 00:00.01 00:00.04 00:00.25 00:01.44 00:08.105 00:00.00 00:00.03 00:00.25 00:02.27 00:23.47 03:46.196 00:00.01 00:00.10 00:01.44 00:23.47 06:37.08 1:52:09.00
NxN Algorithm Performance
NxN Algorithm Performance
0
100
200
300
400
500
1 2 3 4 5 6N
Tim
e (
se
co
nd
s)
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):
N/M 2 3 4 5 62 0.060 0.160 0.280 0.550 0.5503 0.160 0.220 0.280 0.550 0.7704 0.280 0.280 0.550 0.720 1.0505 0.550 0.550 0.770 0.99 1.9206 0.550 0.770 1.050 1.920 14.230• Sacrifice centiseconds in smaller cases• Gains in seconds for larger cases
NxN Performance
NxN Performance for Java Implementation
0
5
10
15
0 2 4 6 8N
Tim
e
(se
co
nd
s)
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