每次循环变量更改时的复杂性?

时间:2018-08-06 00:28:48

标签: algorithm complexity-theory amortized-analysis

A(q, cList){                 
    for(i=1;i<q;i++){       // q is the number of keywords in the query
        B(cList[i]);            
    }
}

B(c){
    for(j=1; j<c;j++){  // c specifies how many times the keyword is included, and taken as input
        do something
    }
}

For example: 
A(2, [5, 7])     // 2 keywords are searched, that are included in 5 and 7 documents
A(2, [100, 1500]) // 2 keywords are searched, that are included in 100 and 1500 documents
A(3, [1, 10000, 1500]) // 3 keywords are searched, that are included in 1, 10000 and 1500 documents

外部循环取决于q。但是我不确定B的复杂度吗?

每个关键字的c值更改。 我认为这也取决于c值。

那么,A的复杂度是多少?

1 个答案:

答案 0 :(得分:1)

这里,我们有2个输入,例如,输入A(2,[1,100])增加了复杂性。外循环迭代将由函数A的第一个参数(此处为2)决定,将其称为n,至于函数B中的第二个循环,迭代次数将由函数A的第二个参数,在这种情况下为[1,100]。通过这些,我们将通过O( n * max(cList[]))来计算最坏情况的复杂度,其中max取cList的最长元素的长度。

我本来将这种复杂性分析称为过于悲观,但是当我们渐近地认为您的运行时间将主要受cList中的最大值的影响。

相关问题