Flood Fill Four-Way Algorithm Complexity

2019-09-19 14:05发布

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?

3条回答
姐就是有狂的资本
2楼-- · 2019-09-19 14:24

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楼-- · 2019-09-19 14:27

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.

查看更多
干净又极端
4楼-- · 2019-09-19 14:36

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?

查看更多
登录 后发表回答