我怎样才能找到这种递归算法的时间复杂度?

时间:2016-09-30 19:08:19

标签: algorithm time complexity-theory

我正试图找出这个作业问题,但我很困惑。我需要找到这个算法ClosestPair的时间复杂度,但我无法弄清楚如何计算它运行的次数。这是我到目前为止所做的:

1. ClosestPair(S, p, r)
2.  if p == r 
3.      return ∞
4.  if r - p == 1
5.      return Distance(S[p], S[p+1])
6.  else
7.      m = floor ((p+r)/2) //m is the index of the median (the x-coordinate is the vertical dividing line)
8.      d1 = ClosestPair(S, p, m)
9.      d2 = ClosestPair(S, m+1, r)
10.     d = min(d1, d2)
11.     SPrime = points in S within distance d from S[m].x sorted by y.
12.     dPrime = BandClosestPair(Sprime, d)
13.     return min( dPrime, d)

到目前为止,这是我的时间复杂度

1. 0
2. 1
3. 1
4. 1
5. 1
6. 1
7. 1
8. 
9.
10. 1 (It's not actually 1 but it will be a constant so it won't affect the algorithm's time complexity)
11. 
12.
13. 1

任何帮助都会很棒!我不希望你为我做一些功课,只是帮助搞清楚:)

1 个答案:

答案 0 :(得分:-1)

这可能就是你要找的东西。 https://en.wikipedia.org/wiki/Master_theorem

Tim Roughgarden还就Coursera这个主题进行了很好的讲座。 https://www.coursera.org/learn/algorithm-design-analysis

相关问题