Skip to content

Conversation

CrodiYa
Copy link
Contributor

@CrodiYa CrodiYa commented Oct 2, 2025

BFS Flood Fill algorithm

Project already has Flood Fill

  • This project already has a Flood Fill algorithm, but it is DFS.
    For any big "image" DFS version won`t work because of stack overflow.
    The BFS version, on the other hand, handles large "images" without any problems.

  • My first "big" project on Java was simple pixel paint app and that was my first time encountering StackOverflowException and I was very confused: it took me some time to figure out the problem and solution.
    So, it would be useful to have this example of this algorithm here.

  • A similar version of this algorithm can be found here: https://www.geeksforgeeks.org/dsa/flood-fill-algorithm/
    Main difference - this version looks for diagonals too

Technical details

This version uses queue and helper class Point to traverse.
While queue is not empty, the "pixel" is painted and "pixels" around it are added to queue (if coordinates are in boundaries and colour is not the same).
Directions for new "pixels" stored in arrays dx and dy. First four - horizontal and vertical, second four - diagonals.

It is achievable to store coordinates (x, y) in long, using bit mask, but it`s too much and possibly can lead to collision.

Tests were copied from original FloodFill and one test was added to demonstrate that BFS handles "big images".

To be honest, I am not entirely sure in what package to add this, so "others" was picked.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

- nested class Point: helper class, represents point in 2D field (x,y)
- shouldSkipPixel method: helper method to validate point
- floodFill method: iterative version of floodFill, uses Queue to add and poll Points and change it color if allowed
-same tests as for normal floodFill and test for a big image
@codecov-commenter
Copy link

codecov-commenter commented Oct 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.48%. Comparing base (b1aa896) to head (83ac241).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6584      +/-   ##
============================================
+ Coverage     76.45%   76.48%   +0.02%     
- Complexity     5985     6001      +16     
============================================
  Files           716      717       +1     
  Lines         20265    20290      +25     
  Branches       3929     3933       +4     
============================================
+ Hits          15494    15518      +24     
- Misses         4180     4181       +1     
  Partials        591      591              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CrodiYa CrodiYa marked this pull request as ready for review October 2, 2025 08:14
- Move private methods after public methods for better readability
- Add class-level JavaDoc documentation with algorithm description and links to references
@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) October 9, 2025 17:16
@DenizAltunkapan DenizAltunkapan merged commit 16557a9 into TheAlgorithms:master Oct 9, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants