How does flood fill work?

Поділитися
Вставка
  • Опубліковано 1 сер 2020
  • Algorithm Archive: www.algorithm-archive.org/con...
    Source code: In chapter
    Github sponsors (Patreon for code): github.com/sponsors/leios
    Twitch: / leioslabs
    Discord: / discord
    Github: github.com/leios
    Music: www.joshwoodward.com/
  • Наука та технологія

КОМЕНТАРІ • 81

  • @cout970
    @cout970 3 роки тому +23

    Flood fill a.k.a. the inefficient path finder

  • @leanobajio
    @leanobajio 3 роки тому +39

    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  3 роки тому +6

      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 3 роки тому +2

      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 3 роки тому +3

      @@busTedOaS Or a hashset

  • @3100500
    @3100500 3 роки тому +13

    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 3 роки тому +2

      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.

  • @dotanon
    @dotanon Рік тому +1

    I like how you explain things, very easy to follow and very relateable!

  • @idjles
    @idjles 3 роки тому +11

    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 3 роки тому +1

      That sounds really interesting! I'll have to play this through in my head.

    • @timh.6872
      @timh.6872 3 роки тому

      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 3 роки тому +3

      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 3 роки тому

      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 3 роки тому

      @@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?

  • @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.

  • @50paisaGyan
    @50paisaGyan 3 роки тому +4

    The way you explain is wonderful.

  • @TheKirkster96
    @TheKirkster96 3 роки тому +2

    I like this video! I've never seen the graph abstraction applied to pixels before, it makes sense!

    • @LeiosLabs
      @LeiosLabs  3 роки тому +2

      Yeah. We'll be doing more graph-specific stuff soon!

  • @cabbageman
    @cabbageman 3 роки тому +1

    This was great, I would love more on cellular graphical algorithms

    • @LeiosLabs
      @LeiosLabs  3 роки тому

      Haha, there will be more! I just like to hop around different topics.

  • @catmium7974
    @catmium7974 Рік тому

    Really good and quickly explained.

  • @pau1320
    @pau1320 2 місяці тому

    Just wanted to say this is a phenomenal video.

  • @2false637
    @2false637 3 роки тому +2

    Fascinating! Thank you for this.

    • @LeiosLabs
      @LeiosLabs  3 роки тому

      I'm glad you liked it!

  • @abinayanrajendran4716
    @abinayanrajendran4716 Рік тому

    A nice explanation of flood fill

  • @ProjectPhysX
    @ProjectPhysX 3 роки тому +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  3 роки тому +1

      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 3 роки тому +1

    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  3 роки тому +1

      I use Blender for video editing. There aren't too many good linux options ^^

    • @heliumhydride
      @heliumhydride 3 роки тому +1

      @@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  3 роки тому

      @@heliumhydride Thanks!

  • @iminni3459
    @iminni3459 3 роки тому +2

    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.

  • @TenderBug
    @TenderBug 3 роки тому +1

    Wow awesome video. Thanks. using Julia is great too

    • @LeiosLabs
      @LeiosLabs  3 роки тому +1

      Yeah, I use Julia for everything nowadays

  • @abdelrahmanhelaly1808
    @abdelrahmanhelaly1808 Рік тому

    Thank you

  • @thomaslikesgames5934
    @thomaslikesgames5934 3 роки тому

    Which coding program and software do you use? I think Lua myself. But it doesn't wuite seem like it. Thanks

  • @laviekolchinsky9441
    @laviekolchinsky9441 3 роки тому +2

    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  3 роки тому

      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 3 роки тому

      @@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  3 роки тому +1

      @@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 3 роки тому +1

      @@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  3 роки тому

      @@laviekolchinsky9441 I am honestly not sure. I haven't really done this stuff before. Sorry!

  • @carbylamine2230
    @carbylamine2230 3 роки тому +2

    What are those lines of code above you on the screen

    • @graicc
      @graicc 3 роки тому +1

      CarbylAmine twitch chat

    •  3 роки тому +1

      Looks like an IRC client.

    • @carbylamine2230
      @carbylamine2230 3 роки тому +1

      Lol, I'm new to this world.

    • @LeiosLabs
      @LeiosLabs  3 роки тому +2

      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.

  • @k-dramagoodmorningseoul
    @k-dramagoodmorningseoul 3 роки тому

    How have you been? / Time has passed this year. I hope you take care of the cold weather and health based on Korea. ^O^

  • @empireempire3545
    @empireempire3545 Рік тому

    I will never understand why some people think that recursion is somehow easier or more natural.

  • @HebaruSan
    @HebaruSan 3 роки тому +1

    I was curious about your terminal game, but the specterm.jl
    repo just has a README and a LICENSE

    • @LeiosLabs
      @LeiosLabs  3 роки тому

      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 3 роки тому

      @@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 3 роки тому +3

    wow, what a beard!

  • @yonatanbeer3475
    @yonatanbeer3475 3 роки тому +1

    Does Go even count as a video game?

    • @LeiosLabs
      @LeiosLabs  3 роки тому

      Ok, you are right. It's not a "video" game, but it was a game I played online.

  • @nothingspecial7399
    @nothingspecial7399 3 роки тому +1

    😄

  • @ELCEKAID
    @ELCEKAID 2 місяці тому

    for pixel in areatofill:
    fill(pixel);
    is just that easy kappa

  • @jojojorisjhjosef
    @jojojorisjhjosef 3 роки тому +4

    Thankfully that 6:33 didn't turn into a swastika.

    • @LeiosLabs
      @LeiosLabs  3 роки тому

      I hadn't even considered that as a possibility!

  • @r75shell
    @r75shell 3 роки тому

    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  3 роки тому +1

      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 3 роки тому

      @@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  3 роки тому

      @@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 3 роки тому

      @@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  3 роки тому

      ​@@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 2 місяці тому

    Hmm, i just feel it's isn't right.
    Like you're comparing low level algorithm vs high level algorithm