How to calculate time complexity of backtracking algorithm?

血红的双手。 提交于 2019-12-02 20:32:10
Vikram Bhat

In short:

  1. Hamiltonian cycle : O(N!) in the worst case
  2. WordBreak and StringSegment : O(2^N)
  3. NQueens : O(N!)

Note: For WordBreak there is an O(N^2) dynamic programming solution.


More details:

  1. In Hamiltonian cycle, in each recursive call one of the remaining vertices is selected in the worst case. In each recursive call the branch factor decreases by 1. Recursion in this case can be thought of as n nested loops where in each loop the number of iterations decreases by one. Hence the time complexity is given by:

    T(N) = N*(T(N-1) + O(1))
    T(N) = N*(N-1)*(N-2).. = O(N!)

  2. Similarly in NQueens, each time the branching factor decreases by 1 or more, but not much, hence the upper bound of O(N!)

  3. For WordBreak it is more complicated but I can give you an approximate idea. In WordBreak each character of the string has two choices in the worst case, either to be the last letter in the previous word, or to be the first letter of a new word, hence the branching factor is 2. Therefore for both WordBreak & SegmentString T(N) = O(2^N)

Backtracking algo:

n-queen problem:O(n!)

graph coloring problem:O(nm^n)//where n=no. of vertex,m=no. of color used

hamilton cycle:O(N!)

WordBreak and StringSegment:O(2^N)

subset sum problem:O(nW)

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