Flood Fill Four-Way Algorithm Complexity

限于喜欢 提交于 2019-12-24 22:08:34

问题


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?


回答1:


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?




回答2:


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/




回答3:


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.



来源:https://stackoverflow.com/questions/29160614/flood-fill-four-way-algorithm-complexity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!