Тёмный

How does flood fill work? 

Leios Labs
Подписаться 96 тыс.
Просмотров 28 тыс.
50% 1

Algorithm Archive: www.algorithm-...
Source code: In chapter
Github sponsors (Patreon for code): github.com/spo...
Twitch: / leioslabs
Discord: / discord
Github: github.com/leios
Music: www.joshwoodwa...

Опубликовано:

 

4 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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
@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
@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?
@dotanon
@dotanon Год назад
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 6 месяцев назад
Just wanted to say this is a phenomenal video.
@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.
@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.
@abinayanrajendran4716
@abinayanrajendran4716 Год назад
A nice explanation of flood fill
@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.
@2false637
@2false637 4 года назад
Fascinating! Thank you for this.
@LeiosLabs
@LeiosLabs 4 года назад
I'm glad you liked it!
@catmium7974
@catmium7974 Год назад
Really good and quickly explained.
@TenderBug
@TenderBug 4 года назад
Wow awesome video. Thanks. using Julia is great too
@LeiosLabs
@LeiosLabs 4 года назад
Yeah, I use Julia for everything nowadays
@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!
@empireempire3545
@empireempire3545 2 года назад
I will never understand why some people think that recursion is somehow easier or more natural.
@abdelrahmanhelaly1808
@abdelrahmanhelaly1808 2 года назад
Thank you
@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!
@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!
@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 3 года назад
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^
@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
@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.
@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 7 месяцев назад
for pixel in areatofill: fill(pixel); is just that easy kappa
@nothingspecial7399
@nothingspecial7399 4 года назад
😄
@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.
@anonymous83728
@anonymous83728 6 месяцев назад
Hmm, i just feel it's isn't right. Like you're comparing low level algorithm vs high level algorithm
Далее
Implementing a Flood Fill Algorithm From Scratch
9:20
How to color complex functions [Domain Coloring]
6:44
这位大哥以后恐怕都不敢再插队了吧…
00:16
БАГ ЕЩЕ РАБОТАЕТ?
00:26
Просмотров 236 тыс.
Сколько стоит ПП?
00:57
Просмотров 338 тыс.
How algorithms evolve (Genetic Algorithms)
4:45
Просмотров 116 тыс.
The Problem with Research Software Engineering
9:56
Просмотров 21 тыс.
A Comparison of Pathfinding Algorithms
7:54
Просмотров 717 тыс.
Why you’re so tired
19:52
Просмотров 1,3 млн
Christianity's most important algorithm
7:09
Просмотров 12 тыс.
Breadth First Search (BFS): Visualized and Explained
10:41
这位大哥以后恐怕都不敢再插队了吧…
00:16