在之前的文章中,我们讨论了渐近分析,最坏情况,平均情况和最佳情况,渐近符号和循环分析。
在这篇文章中,讨论了有关算法分析的实践问题。
问题1:找出以下重复发生的复杂度:
{ 3T(n-1), if n>0,
T(n) = { 1, otherwise
解决方案:
Let us solve using substitution.
T(n) = 3T(n-1)
= 3(3T(n-2))
= 32T(n-2)
= 33T(n-3)
...
...
= 3nT(n-n)
= 3nT(0)
= 3n
This clearly shows that the complexity
of this function is O(3n).
问题2:找出重复的复杂度:
{ 2T(n-1) - 1, if n>0,
T(n) = { 1, otherwise
解决方案:
Let us try solving this function with substitution.
T(n) = 2T(n-1) - 1
= 2(2T(n-2)-1)-1
= 22(T(n-2)) - 2 - 1
= 22(2T(n-3)-1) - 2 - 1
= 23T(n-3) - 22 - 21 - 20
.....
.....
= 2nT(n-n) - 2n-1 - 2n-2 - 2n-3
..... 22 - 21 - 20
= 2n - 2n-1 - 2n-2 - 2n-3
..... 22 - 21 - 20
= 2n - (2n-1)
[Note: 2n-1 + 2n-2 + ...... + 20 = 2n - 1]
T(n) = 1
Time Complexity is O(1). Note that while
the recurrence relation looks exponential
the solution to the recurrence relation
here gives a different result.
问题3:查找以下程序的复杂性:
CPP
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
CPP
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
// Inner loop executes only one
// time due to break statement.
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j<=n; j = 2 * j)
for (int k=1; k<=n; k = k * 2)
count++;
}
Java
static void function(int n)
{
int count = 0;
for (int i = n / 2; i <= n; i++)
for (int j = 1; j <= n; j = 2 * j)
for (int k = 1; k <= n; k = k * 2)
count++;
}
// This code is conributed by rutvik_56.
C#
static void function(int n)
{
int count = 0;
for (int i = n / 2; i <= n; i++)
for (int j = 1; j <= n; j = 2 * j)
for (int k = 1; k <= n; k = k * 2)
count++;
}
// This code is contributed by pratham76.
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
// Executes O(Log n) times
for (int j=1; j<=n; j = 2 * j)
// Executes O(Log n) times
for (int k=1; k<=n; k = k * 2)
count++;
}
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = j++)
for (int k=1; k<=n; k = k * 2)
count++;
}
CPP
void function(int n)
{
int count = 0;
// outer loop executes n/2 times
for (int i=n/2; i<=n; i++)
// middle loop executes n/2 times
for (int j=1; j+n/2<=n; j = j++)
// inner loop executes logn times
for (int k=1; k<=n; k = k * 2)
count++;
}
CPP
void function(int n)
{
int i = 1, s =1;
while (s <= n)
{
i++;
s += i;
printf("*");
}
}
CPP
void function(int n)
{
int count = 0;
for (int i=0; i
CPP
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i
解决方案:考虑以下函数的注释。
CPP
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
// Inner loop executes only one
// time due to break statement.
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
以上函数O(n)的时间复杂度。即使内部循环以n为边界,但是由于break语句,它仅执行一次。问题4:查找以下程序的复杂性:
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j<=n; j = 2 * j)
for (int k=1; k<=n; k = k * 2)
count++;
}
Java
static void function(int n)
{
int count = 0;
for (int i = n / 2; i <= n; i++)
for (int j = 1; j <= n; j = 2 * j)
for (int k = 1; k <= n; k = k * 2)
count++;
}
// This code is conributed by rutvik_56.
C#
static void function(int n)
{
int count = 0;
for (int i = n / 2; i <= n; i++)
for (int j = 1; j <= n; j = 2 * j)
for (int k = 1; k <= n; k = k * 2)
count++;
}
// This code is contributed by pratham76.
解决方案:考虑以下函数的注释。
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
// Executes O(Log n) times
for (int j=1; j<=n; j = 2 * j)
// Executes O(Log n) times
for (int k=1; k<=n; k = k * 2)
count++;
}
以上函数O(n log 2 n)的时间复杂度。问题5:查找以下程序的复杂性:
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = j++)
for (int k=1; k<=n; k = k * 2)
count++;
}
解决方案:考虑以下函数的注释。
CPP
void function(int n)
{
int count = 0;
// outer loop executes n/2 times
for (int i=n/2; i<=n; i++)
// middle loop executes n/2 times
for (int j=1; j+n/2<=n; j = j++)
// inner loop executes logn times
for (int k=1; k<=n; k = k * 2)
count++;
}
以上函数O(n 2 logn)的时间复杂度。问题6:查找以下程序的复杂性:
CPP
void function(int n)
{
int i = 1, s =1;
while (s <= n)
{
i++;
s += i;
printf("*");
}
}
解决方案:我们可以根据关系s i = s i-1 + i定义术语“ s”。每次迭代,“ i”的值增加一。第i次迭代中“ s”中包含的值是第一个“ i”个正整数的总和。如果k是程序执行的迭代总数,则在以下情况下while循环终止:1 + 2 + 3…。+ k = [k(k + 1)/ 2]> n So k = O(√n)。
以上函数O(√n)的时间复杂度。问题7:在以下程序的复杂性上找到一个严格的上限:
CPP
void function(int n)
{
int count = 0;
for (int i=0; i
解决方案:考虑以下函数的注释。
CPP
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i
以上函数O(n 5 )的时间复杂度。