I\'ve searched but I can\'t seem to find the complexity of the Flood Fill Algorithm (Four-way ver.). What exactly is the complexity in big O notation?
The time complexity of the Flood Fill algorithm should be O(m×n) , where m= no. of rows and n=no. of columns in the given matrix. Note that every element of matrix is processed at most three times. So, it boils down to O(3×mn), which eventually is same as O(mn).
In worst-case, all cells of the matrix will be covered.
In terms of complexity time, this algorithm will be equals the recursive one: O(N×M)O(N×M), where N and M are the dimensions of the input matrix. The key idea is that in both algorithms each node is processed at most once.
Please refer below link for better understaning and more cases:
https://www.hackerearth.com/practice/algorithms/graphs/flood-fill-algorithm/tutorial/
The complexity of the flood fill algorithm is proportional to the number of pixels in the filled area. So, if you have e.g. a square, and M is the number of pixels in the square and N is the length of the side of a square, then M = N^2 and the complexity is O(M) = O(N^2).
By the way, this question has already been answered in a comment in Stackoverflow. See How can I improve the performance of my flood-fill routine?
The time complexity would be O(4*mn)=(mn) because each cell of matrix is processed at most 4 times. For Example, a particular cell can be called by its top, down, left or right cell.