More Queens, More Random

Lets dig into this n-queens problem more deeply. What would it be when n is getting very large?

In last entry, a poly-algo is used for n sized up about to 300. That code is O(n^2) time complexity. Now lets imagine n can be very large, like 10,000, 100,000 or even 1,000,000. The algo would not scale very well.

The same author of the last paper proposed an almost-linear algorithm. Here is the resulting code adapted. As you can see, the content has not changed much from last code. The main difference is, in the old code we systematically check each and every pair of queens whereas in the new code pairs are chosen rather randomly. But the result changes dramatically.

Talk is cheap; show me the numbers!

$ time (echo 1; echo 30000) |./old >/dev/null

real    1m11.808s
user    1m8.447s
sys     0m0.579s

$ time (echo 1; echo 30000) |./new >/dev/null

real    0m0.436s
user    0m0.422s
sys     0m0.004s

And lets run an extreme case: 1,000,000. The old code may take way too long. But the new code:

$ time (echo 1; echo 1000000) |./new >/dev/null

real    0m31.707s
user    0m30.089s
sys     0m0.300s

Way kool.


Leave a comment

Recent Entries

  • Running Spaz on WebOS Emulator

    > You got root, you got everything. Yeah, Palm Pre is hot these days. Now with the [webos emulator](http://www.geektang.com/2009/06/linuxwebos.html) and [ssh](http://www.geektang.com/2009/06/sshwebos.html) available, you can have...

  • Haskell Platform

    >The Haskell Platform is a blessed library and tool suite for Haskell distilled from Hackage, along with installers for a wide variety of systems. IOW,...

  • When MT Meets iPhone/iPod Touch

    Just found [iMT](http://plugins.movabletype.org/imt/). It's the iphone / ipod touch interface for mt4. So now you can add/edit blog entries / comments on your favorite device...

  • All Project Euler Problems Solved

    [Project Euler](http://projecteuler.net) continues to be fun. First reached 100% when there were 240 problems. And have successfully guarded that honor since =). (As of this...

  • Million Digits of E, Sqrt(2), Pi

    > More puzzles, more fun. Besides [acm oj](http://acm.tju.edu.cn/toj/ranklist.html) and [projecteuler](http://projecteuler.net), i also like to solve the puzzles on [spoj](http://spoj.pl). The ACM/ICPC rules are rather limited...

Close