OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 17, 2024 11:55 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: 8 queens problem
PostPosted: Sun May 18, 2014 12:21 pm 
Offline
Member
Member
User avatar

Joined: Wed Dec 26, 2007 3:37 am
Posts: 117
Location: France
One thing interests me,
I tried to solve the 8 queens problem all by myself, without using a known solution.
My algo only achieved to place 7 queens.
Do you consider that this problem is too easy or quite hard?

Solutions in various languages are here: http://rosettacode.org/wiki/N-queens_problem

_________________
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Sun May 18, 2014 1:17 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
Pick the right tool for the problem and it should be trivial. I would pick Prolog.


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 2:26 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 26, 2007 3:37 am
Posts: 117
Location: France
I used Python but I'm interested to know how OSdevers rate the complexity of this problem.
I know that one can tell me that one must use recursion and it becomes trivial but when you must figure it out by yourself it becomes a bit trickier than watching an existing solution and saying "I understood".

_________________
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 3:06 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
It's a fairly typical constraint satisfaction problem you can solve with textbook algorithms like DFS.

It's also a problem you can optimise by looking at the problem in particular: you don't need to try all 64 places for each queen, (or the subset that remains available at that point), but only a subset of eight for any particular queen as there is exactly one on each row.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 3:28 am 
Offline
Member
Member
User avatar

Joined: Fri Jul 03, 2009 6:21 am
Posts: 359
narke wrote:
I used Python but I'm interested to know how OSdevers rate the complexity of this problem.
I know that one can tell me that one must use recursion and it becomes trivial


It is trivial, hence the simplicity of the Prolog "generate and test" type of solution, i.e., find a permutation of N queens (generalised from 8 to N queens) on a chess board [generate], now test to see if none attack each other, upon failure backtrack to find a new permutation. Eventually all permutations will have been attempted. Imperative/functional programmers will use loops/recursion for a simple solution instead of backtracking.

Obviously this takes time and after a while the programmer starts to think about heuristics to reduce the search space [see Combuster's answer].

narke wrote:
but when you must figure it out by yourself it becomes a bit trickier than watching an existing solution and saying "I understood".

Problems like this are the bread and butter of the Prolog programmer and are used as homework exercises at universities around the world. It only seems tricky when you haven't done a few of them. Give it a proper try and don't feel ashamed if it takes a loooong time to solve. The next problem of this type you attempt will see the answer come more quickly. See http://dtai.cs.kuleuven.be/ppcbook/ for a free book chock full of these types of problems [you don't need to solve them with Prolog].

_________________
Every universe of discourse has its logical structure --- S. K. Langer.


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 9:02 am 
Offline
Member
Member
User avatar

Joined: Thu Dec 19, 2013 1:40 am
Posts: 100
Location: Asia, Singapore
If I remember this well it was covered in competitive programming. (the book title is competitive programming). If I am not wrong we use...uh...brute force (aka backtracking), but it can be optimized to prevent using every possibilities.

@combustor: how do you use DFS? I don't see any way...

_________________
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 9:14 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
You need to place 8 queens. You can view that as a tree where the root is an empty board and every step away from the root adds a single queen (in an available position). The application of DFS easily follows from there.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 9:29 am 
Offline
Member
Member
User avatar

Joined: Thu Dec 19, 2013 1:40 am
Posts: 100
Location: Asia, Singapore
Hm...it seems fine but as a competitive programmer, I wonder performance wise which would be better (I know usually DFS would be better but you will never know). Also looking at it I can see a slight possible problem where the programmer totally forgets about the root. Btw, I am wondering based on that logic would it be BFS or DFS, since we are going a step from the root at one time BFS sounds like a good solution too.

_________________
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 9:36 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Breadth-first only works if the solution is close to the starting position. In the typical case and this case in particular it's a bad idea because you need to find (and keep in memory) all of the 7-queen solutions before you are even allowed by the algorithm to find the 8-queen solutions.

If you need all solutions, DFS finds everything in the same time as BFS, with less memory use. If you need one solution, BFS is a gross horror.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: 8 queens problem
PostPosted: Mon May 19, 2014 8:55 pm 
Offline
Member
Member
User avatar

Joined: Mon Jun 05, 2006 11:00 pm
Posts: 2293
Location: USA (and Australia)
I would probably do a brute force algorithm, but rather than working out all ~4 billion permutations, you could do an early termination (mark all cells queen 1 can see as invalid, only iterate queen 2 over valid cells, mark all queen 1 & 2 see as invalid), also you can cut the permutations down significantly by only placing higher queens in front of lower queens (to avoid repeating old permutations).

_________________
My OS is Perception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 56 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group