在循环中嵌套的后退函数的复杂性

时间:2017-10-26 07:25:34

标签: recursion time-complexity complexity-theory

我试图计算循环内递归调用的复杂性:

Calculate(n,m){
for(i=1; i<n; i++){
    if(n==1) return 1;
    Calculate(n-1,m);
 }
for(j=1; i<m; i++){
    Calculate(n,m-1);
    if(m==1) return 1;
} 
}

这是我做的计算:

C(m,n) = 1 + C(m-1,n) + C(m-2,n) + .... + C(1,n) 
           + C(m,n-1) + C(m,n-2) + .... + C(m,1)

是复杂度= 2 ^(m + n)??

1 个答案:

答案 0 :(得分:1)

你是对的:要将import csv import requests from bs4 import BeautifulSoup diction_page = ['http://www.bloomberg.com/quote/SPX:IND','http://www.bloomberg.com/quote/CCMP:IND'] for link in diction_page: res = requests.get(link).text soup = BeautifulSoup(res,'lxml') title = soup.select_one('.name').text.strip() price = soup.select_one('.price').text print(title,price) with open('item.csv','a',newline='') as outfile: writer = csv.writer(outfile) writer.writerow(["Title","Price"]) writer.writerow([title, price]) m减少1,你需要拨2个电话;因为你需要将它们都减少到零,所以你需要经历n'级别'。

因此,通话次数为O(m+n)