Flood Fill Four-Way Algorithm Complexity

后端 未结 4 1324
忘掉有多难
忘掉有多难 2021-01-27 14:14

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?

相关标签:
4条回答
  • 2021-01-27 14:31

    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).

    0 讨论(0)
  • 2021-01-27 14:32

    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/

    0 讨论(0)
  • 2021-01-27 14:35

    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?

    0 讨论(0)
  • 2021-01-27 14:51

    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.

    0 讨论(0)
提交回复
热议问题