How does flood fill work?

  Рет қаралды 28,569

Leios Labs

Leios Labs

Күн бұрын

Пікірлер: 81
@cout970
@cout970 4 жыл бұрын
Flood fill a.k.a. the inefficient path finder
@leanobajio
@leanobajio 4 жыл бұрын
Or, you could mark a vertex as visited, which is more or less the same as coloring it as you go in the bf traversal. Nice video!
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Yeah. I was planning on using that as a way to talk about more general graph traversal. We just hadn't talked about it yet and I didn't want to confuse anyone!
@busTedOaS
@busTedOaS 4 жыл бұрын
That requires mutable node state though. Some would call it less elegant from a programmer's point of view. Could create trouble with multithreading and stuff.
@leanobajio
@leanobajio 4 жыл бұрын
@@busTedOaS Or a hashset
@Pavel-wj7gy
@Pavel-wj7gy 2 жыл бұрын
Awesome explanation! It helped me a lot when I tried to create an Etch-a-Sketch game. I have watched lots of other explanations but yours is the best hands down. I really cannot express enough how grateful I am for your video and a respective article on the archive.
@idjles
@idjles 4 жыл бұрын
I solved this in 1996 by pushing the points onto a stack and popping them randomly. Random popping keeps the stack size minimized without slowing down the search - this is better than either depth first (which can make the stack blow up very large) and breadth first (which can delay leaks into new regions)
@dragoncurveenthusiast
@dragoncurveenthusiast 4 жыл бұрын
That sounds really interesting! I'll have to play this through in my head.
@timh.6872
@timh.6872 4 жыл бұрын
Random popping from a stack adds the overhead of removing nodes from the middle of the stack, which either means a linked list traversal or an array copy. Does it really help enough to offset that?
@idjles
@idjles 4 жыл бұрын
Tim H. I used array copy - moving the last element to the random „popped“ position - fast and much cheaper than testing a step. It’s amazing - the surface of the floodfill spreads out like a circle - irrespective of the complexity and „mazed-ness“ of the shape - it’s beautiful to watch - randomness is truly the most efficient approach in terms of stack size, elegant and simple.
@idjles
@idjles 4 жыл бұрын
Tim H. It massively helps as the stack size stays always the size of the circumference of the expanding circle - If you go depth first through a maze every point on Each side of the path goes on the stack, meaning the stack can easily be double the number of points searched. With „random stack“ the stack size never grows much larger than sqrt(area/pi)
@timh.6872
@timh.6872 4 жыл бұрын
@@idjles Ah, that makes sense. Swap the term to be popped with a random one further down the stack and use that one instead. If we're going to be popping in a random order, then we don't have to preserve insertion order. I'll have to play with that some time, the nondeterminism makes the algorithmic analysis much more interesting. I'm assuming it's a uniform distribution over the size of the stack?
@3100500
@3100500 4 жыл бұрын
One more thing to mention is that recursive version of flood fill can get stack owerflow exception if you try to flood fill big maze. I got this exception with maze image 4000x4000.
@joshbone9600
@joshbone9600 4 жыл бұрын
This is an issue with recursion in general. Memory overhead gets huge.
@noli-timere-crede-tantum
@noli-timere-crede-tantum 2 жыл бұрын
Not if you're writing it in a language that can TCO
@dotanon
@dotanon 2 жыл бұрын
I like how you explain things, very easy to follow and very relateable!
@50paisaGyan
@50paisaGyan 4 жыл бұрын
The way you explain is wonderful.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Thanks!
@TheKirkster96
@TheKirkster96 4 жыл бұрын
I like this video! I've never seen the graph abstraction applied to pixels before, it makes sense!
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Yeah. We'll be doing more graph-specific stuff soon!
@pau1320
@pau1320 9 ай бұрын
Just wanted to say this is a phenomenal video.
@abinayanrajendran4716
@abinayanrajendran4716 Жыл бұрын
A nice explanation of flood fill
@KasperFrandsen
@KasperFrandsen 3 жыл бұрын
Thanks a lot. I'd never implemented flood fill before and this was a great introduction to it. Had it working in no time.
@cabbageman
@cabbageman 4 жыл бұрын
This was great, I would love more on cellular graphical algorithms
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Haha, there will be more! I just like to hop around different topics.
@catmium7974
@catmium7974 2 жыл бұрын
Really good and quickly explained.
@2false637
@2false637 4 жыл бұрын
Fascinating! Thank you for this.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I'm glad you liked it!
@ProjectPhysX
@ProjectPhysX 4 жыл бұрын
Thanks for the video! Flood fill is easy to understand, but implementing it efficiently is quite difficult. I'm currently using a Hoshen-Kopelman implementation for domain labeling on a 3D lattice, but performance still isn't ideal.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Oh yeah, it gets really tricky really quickly! I wanted to introduce it as a way to do more graph-y methods on the channel. At this point, we've only really done tree traversal.
@heliumhydride
@heliumhydride 4 жыл бұрын
Great video as always! I mainly got into coding because of desiring to simulate and visualize or plot concepts from physics and mathematics. I do have a question, what software do you use to edit the videos?
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I use Blender for video editing. There aren't too many good linux options ^^
@heliumhydride
@heliumhydride 4 жыл бұрын
@@LeiosLabs thanks from the prompt reply! Well it seems to work great because they are really well edited, keep up the great content.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
@@heliumhydride Thanks!
@iminni3459
@iminni3459 4 жыл бұрын
Are you going to add a chapter to the algorithm archive on maze generation at any point? There's like 14 different algorithms and some of them are really interesting.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I really want to!
@jojojorisjhjosef
@jojojorisjhjosef 4 жыл бұрын
Thankfully that 6:33 didn't turn into a swastika.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I hadn't even considered that as a possibility!
@laviekolchinsky9441
@laviekolchinsky9441 4 жыл бұрын
Awesome video. This is pretty unrelated, but is it possible to have a software that converts mp3 into a sum of sine functions and then express that function algebraically? For example, since we can visualise sound in visualiser software or simply by putting for example sand on a speaker, we can express the process that's going on. A lot of sound visualisation methods rely on Fourier methods and I know you have experience with Fourier-stuff so if you have any ideas that'd be cool.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I don't know of any software, specifically, but a Discrete Sine Transform will do precisely what you want. Each element in the output array will just be the the "amount" of that frequency in the music. I was thinking about this recently as well when considering algorithmic composition (music with code), but didn't really get anywhere. Sorry I couldn't me more helpful!
@laviekolchinsky9441
@laviekolchinsky9441 4 жыл бұрын
@@LeiosLabs I was also thinking of using the Discrete Sine Transform but wasn't sure how to implement it (in an algorithmic sense), especially with my poor programming skills. I just have an ongoing joke where my friends and I graph Fourier series and pretend it's some amazing music. I'll keep looking into it because it's definitely possible. People have done it for musical instruments before so it'll work by the same principles.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
@@laviekolchinsky9441 I just don't know how to load music in as an array. This is probably different depending on what language you want to use.
@laviekolchinsky9441
@laviekolchinsky9441 4 жыл бұрын
@@LeiosLabs I'm not entirely sure how that would work either, but you could use a visualisation software for the wave (these are readily available), then write a program to find the amplitudes, after which you do a Fourier transform to get the sine waves. Is that a valid method in general?
@LeiosLabs
@LeiosLabs 4 жыл бұрын
@@laviekolchinsky9441 I am honestly not sure. I haven't really done this stuff before. Sorry!
@TenderBug
@TenderBug 4 жыл бұрын
Wow awesome video. Thanks. using Julia is great too
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Yeah, I use Julia for everything nowadays
@carbylamine2230
@carbylamine2230 4 жыл бұрын
What are those lines of code above you on the screen
@graicc
@graicc 4 жыл бұрын
CarbylAmine twitch chat
4 жыл бұрын
Looks like an IRC client.
@carbylamine2230
@carbylamine2230 4 жыл бұрын
Lol, I'm new to this world.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Yeah, like everyone said, it's twitch chat. I'm trying something new with my most recent videos by showing the chat and doing minimal editing from my twitch stream. This helps me get content out quicker, allows me to keep my face on screen the whole time, and advertises the stream a bit.
@thomaslikesgames5934
@thomaslikesgames5934 3 жыл бұрын
Which coding program and software do you use? I think Lua myself. But it doesn't wuite seem like it. Thanks
@LeiosLabs
@LeiosLabs 3 жыл бұрын
This was all Julia
@empireempire3545
@empireempire3545 2 жыл бұрын
I will never understand why some people think that recursion is somehow easier or more natural.
@abdelrahmanhelaly1808
@abdelrahmanhelaly1808 2 жыл бұрын
Thank you
@HebaruSan
@HebaruSan 4 жыл бұрын
I was curious about your terminal game, but the specterm.jl repo just has a README and a LICENSE
@LeiosLabs
@LeiosLabs 4 жыл бұрын
I put everything in a development branch until it's ready. Lots of work left to do, so it's still in the development branch.
@HebaruSan
@HebaruSan 4 жыл бұрын
@@LeiosLabs Doh! I should know by now to check for alternate branches. Thanks!
@thepersonabovemeisafool8418
@thepersonabovemeisafool8418 4 жыл бұрын
Dude your vdo was awsome but one q where did you get the idea of naming your channel so unique ''Leios os" it's kinda weird and cool...
@mossylikescake
@mossylikescake 4 жыл бұрын
wow, what a beard!
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Thanks, you too!
@k-dramagoodmorningseoul
@k-dramagoodmorningseoul 4 жыл бұрын
How have you been? / Time has passed this year. I hope you take care of the cold weather and health based on Korea. ^O^
@yonatanbeer3475
@yonatanbeer3475 4 жыл бұрын
Does Go even count as a video game?
@LeiosLabs
@LeiosLabs 4 жыл бұрын
Ok, you are right. It's not a "video" game, but it was a game I played online.
@ELCEKAID
@ELCEKAID 9 ай бұрын
for pixel in areatofill: fill(pixel); is just that easy kappa
@r75shell
@r75shell 4 жыл бұрын
regarding floodfill using dfs you said that you can do it by tree traversal and didn't mention that it's not tree and normal tree traversal is not applicable.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
At that stage, we were treating it as a tree, where each point has 3 children, so tree traversal was appropriate. We'll do more graph-like stuff later
@r75shell
@r75shell 4 жыл бұрын
@@LeiosLabs 3:35 tree drawn, and after few seconds you fill picture, which is not a tree. Reduction to tree is discussed at 6:00
@LeiosLabs
@LeiosLabs 4 жыл бұрын
@@r75shell I believe you have it backwards. You are right that I should have covered graph traversal in a previous video, but up until 6:00, we are traversing through trees. It is a graph, in principle, but we are using tree tactics. At 6:00, we start treating it like a graph to some extent, but even then we still treat it as a tree with some nodes no longer connected. I recognize that treating these as a true graph is the more traditional approach, and that will be covered soon-ish.
@r75shell
@r75shell 4 жыл бұрын
@@LeiosLabs Ha! It's funny. You actually have *thing* in code in chapter that handle it, but none of text is mention it. In chapter it's not mentioned, like if it's not so important as in bfs. Similarly in video here your pseudocode doesn't mention it, nor your words. It's when you return from recursion if color is already equal to color you are filling. It's similar thing to how you're not enqueue twice in bfs. If you remove this code, even for 4 (four) pixels for fill, you'll get infinite recursion, because tree traversal doesn't check for visited status, because it's intended for trees. But here you may have not a tree. By the way if tree is not "rooted" if you run same tree traversal without check for visit status, you'll also get infinite recursion because you may "oscilate" between two nodes back and forth.
@LeiosLabs
@LeiosLabs 4 жыл бұрын
​@@r75shell Correct, this is not an n-ary tree, but it is still a tree in this approximation. Each node has a parent and up to 3 children. I understand that this can also be represented as a graph and I mention that it *is* in-fact a graph for later, but by showing it in a more tree-like way, it allows me to work off of what we already have and is slightly more intuitive in my mind. We could have an argument about what is most intuitive, and that's fair... Also why there will be subsequent videos.
@nothingspecial7399
@nothingspecial7399 4 жыл бұрын
😄
@anonymous83728
@anonymous83728 9 ай бұрын
Hmm, i just feel it's isn't right. Like you're comparing low level algorithm vs high level algorithm
Lazy Flood Fill | Procedural Generation | Game Development Tutorial
13:32
Implementing a Flood Fill Algorithm From Scratch
9:20
conaticus
Рет қаралды 9 М.
Hilarious FAKE TONGUE Prank by WEDNESDAY😏🖤
0:39
La La Life Shorts
Рет қаралды 44 МЛН
The purest coding style, where bugs are near impossible
10:25
Coderized
Рет қаралды 1 МЛН
What are Diffusion Models?
15:28
Ari Seff
Рет қаралды 240 М.
Flood Fill - LeetCode 733 - JavaScript
7:23
AlgoJS
Рет қаралды 1,3 М.
The Jump Flood Algorithm | Visualized and Explained
6:04
Benjamin Douglas
Рет қаралды 23 М.
A Comparison of Pathfinding Algorithms
7:54
John Song
Рет қаралды 723 М.
This is how Paint's bucket fill works (Flood fill algorithm)
8:54
The Problem with Research Software Engineering
9:56
Leios Labs
Рет қаралды 21 М.
TLS Handshake Explained - Computerphile
16:59
Computerphile
Рет қаралды 571 М.
Breadth First Search (BFS): Visualized and Explained
10:41
Reducible
Рет қаралды 229 М.
LeetCode 733. Flood Fill (Algorithm Explained)
10:03
Nick White
Рет қаралды 59 М.