Posted: Fri Dec 30, 2011 4:22 pm Post subject: Re: Flood fill algorithm without recursion?
how would i use an array to queue?
Tony
Posted: Fri Dec 30, 2011 10:37 pm Post subject: RE:Flood fill algorithm without recursion?
array is just a datastore. You can also keep data in a linked list, file on disk, or somewhere in the cloud. It doesn't really matter, as you're supposed to put in an API layer in between. That is, implement the interface of Queue<E>. With arrays -- item goes into the queue == item gets added to the end of the array. Item leaves the queue == item gets removed from the front of the array.
Posted: Sat Dec 31, 2011 12:10 pm Post subject: RE:Flood fill algorithm without recursion?
The recursive algorithm seems like it will call itself infinite times. For instance the first call with then call one with the node to the left, which will then call it with the node to the right (ie the starting node) and it will go back and forth forever. You need some way of figuring out if you've calculated that square before.
Tony
Posted: Sat Dec 31, 2011 2:29 pm Post subject: Re: Flood fill algorithm without recursion?
Tony @ Fri Dec 30, 2011 3:30 pm wrote:
In specific, you likely just have an infinite loop somewhere.
Posted: Sat Dec 31, 2011 7:15 pm Post subject: RE:Flood fill algorithm without recursion?
Yeah I know that you mentioned it tony, but it was obvious that he didn't see it, and he'd be going about implementing some queueing algorithms only to end up with the same problem.
tyuo9980
Posted: Sat Dec 31, 2011 10:03 pm Post subject: Re: Flood fill algorithm without recursion?
there is no infinite loop. the function works very well.
Posted: Sun Jan 01, 2012 12:04 am Post subject: RE:Flood fill algorithm without recursion?
Okay, well then there is no reason why it wouldn't work, as the call stack for java is larger then how large your thing.
tyuo9980
Posted: Sun Jan 01, 2012 12:08 am Post subject: Re: Flood fill algorithm without recursion?
how large exactly is java's stack size?
Because this flood fill will reach a maximum of 197400 stacks which is ~5.3mb of space. How would I be able to reduce the usage?
Tony
Posted: Sun Jan 01, 2012 12:08 pm Post subject: RE:Flood fill algorithm without recursion?
looking into this further, Java does limit the stack size, as it assumes that multiple threads (each with its own stack) will be running. StackOverflow vs. OutOfMemory... Since you have just a single thread, it could safely crank that limit up to be over 9000, but presumably "Ready To Program" is not cool with JVM flags.
You can reduce the stack usage by making less non-terminating method calls.
e.g., filling open areas in larger blocks. If you know you've got a chunk of 10x10 available to you, you can fill the inner 9x9 block and terminate, leaving just the outer perimeter to contribute to the stack-size growth. This would grow the stack at 19% of the previous levels (though, of course, require more computation to figure out if such "optimization" moves are even available).
Alternatively you could just throw the points onto the heap, and not make recursive calls. Which is basically the Queue approach (just, using any other datastore that's available).