• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • COMPLEXITY ANALYSIS

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 2

COMPLEXITY ANALYSIS - all with Video Answers

Educators


Chapter Questions

01:27

Problem 1

Explain the meaning of the following expressions:
a. $f(n)$ is $O(1)$.
b. $f(n)$ is $\Theta(1)$.
c. $f(n)$ is $n^{O(1)}$.

Allison Tribble
Allison Tribble
Numerade Educator

Problem 2

Assuming that $f_1(n)$ is $O\left(g_1(n)\right)$ and $f_2(n)$ is $O\left(g_2(n)\right)$, prove the following statements:
a. $f_1(n)+f_2(n)$ is $O\left(\max \left(g_1(n), g_2(n)\right)\right)$.
b. If a number $k$ can be determined such that for all $n>k, g_1(n) \leq g_2(n)$, then $O\left(g_l(n)\right)+O\left(g_2(n)\right)$ is $O\left(g_2(n)\right)$.
c. $f_1(n)^* f_2(n)$ is $O\left(g_1(n)^* g_2(n)\right)$ (rule of product).
d. $O(cg(n))$ is $O(g(n))$.
e. $c$ is $O(1)$.

Check back soon!
08:11

Problem 3

Prove the following statements:
a. $\sum_{i=1}^n i^2$ is $O\left(n^3\right)$ and more generally, $\sum_{i=1}^n i^k$ is $O\left(n^{k+1}\right)$.
b. $a n^k / \lg n$ is $O\left(n^k\right)$ but $a n^k / \lg n$ is not $\Theta\left(n^k\right)$.
c. $n^{1.1}+n \lg n$ is $\Theta\left(n^{1.1}\right)$.
d. $2^n$ is $O(n !)$ and $n !$ is not $O\left(2^n\right)$.
e. $2^{n+a}$ is $O\left(2^n\right)$.
f. $2^{2 n+a}$ is not $O\left(2^n\right)$.
g. $2^{\sqrt{\lg n}}$ is $O\left(n^a\right)$.

Julian Wong
Julian Wong
Numerade Educator
03:00

Problem 4

Make the same assumptions as in Exercise 2 and, by finding counterexamples, refute the following statements:
a. $f_1(n)-f_2(n)$ is $O\left(g_1(n)-g_2(n)\right)$.
b. $f_1(n) / f_2(n)$ is $O\left(g_1(n) / g_2(n)\right)$.

Carlos Pinilla
Carlos Pinilla
Numerade Educator
01:07

Problem 5

Find functions $f_1$ and $f_2$ such that both $f_1(n)$ and $f_2(n)$ are $O(g(n))$, but $f_1(n)$ is not $O\left(f_2\right)$.

Ma. Theresa  Alin
Ma. Theresa Alin
Numerade Educator
06:32

Problem 6

Is it true that
a. if $f(n)$ is $\Theta(g(n))$, then $2^{f(n)}$ is $\Theta\left(2^{g(n)}\right)$ ?
b. $f(n)+g(n)$ is $\Theta(\min (f(n), g(n)))$ ?
c. $2^{n a}$ is $O\left(2^n\right)$ ?

Aayush Gupta
Aayush Gupta
Numerade Educator

Problem 7

The algorithm presented in this chapter for finding the length of the longest subarray with the numbers in increasing order is inefficient, since there is no need to continue to search for another array if the length already found is greater than the length of the subarray to be analyzed. Thus, if the entire array is already in order, we can discontinue the search right away, converting the worst case into the best. The change needed is in the outer loop, which now has one more test:
$$
\text { for }(i=0 \text {, length }=1 ; i<n-1 \text { \& \& length }<n==i ; i++)
$$
What is the worst case now? Is the efficiency of the worst case still $O\left(n^2\right)$ ?

Check back soon!
02:07

Problem 8

Find the complexity of the function used to find the $k$ th integer in an unordered array of integers
int selectkth(int $a[]$, int $k$, int $n)\{$
int $i, j, \min i$, tmp;
for $(i=0 ; i<k ; i++)\{$
$\min i=i$;
for $(j=i+1 ; j<n ; j++)$
if $(a[j]<a[\operatorname{mini}])$
$\min i=j$;
$\operatorname{tmp}=a[i] ;$
$a[i]=a[\min i]$;
$a[\operatorname{mini}]=\mathrm{tmp}$;
}
return a[k-1];
}

Lucas Gagne
Lucas Gagne
Numerade Educator
View

Problem 9

Determine the complexity of the following implementations of the algorithms for adding, multiplying, and transposing $n \times n$ matrices:
for $(i=0 ; i<n ; i++)$
for $(j=0 ; j<n ; j++)$
$a[i][j]=b[i][j]+c[i][j] ;$
for( $i=0 ; i<n ; i++)$
for $(j=0 ; j<n ; j++)$
for $(k=a[i][j]=0 ; k<n ; k++)$
$a[i][j]+=b[i][k] \star c[k][j] ;$
for( $i=0 ; i<n-1 ; i++)$
for $(j=i+1 ; j<n ; j++)\{$
$\operatorname{tmp}=a[i][j] ;$
$a[i][j]=a[j][i] ;$
$a[j][i]=$ tmp;
}

Victor Salazar
Victor Salazar
Numerade Educator
01:59

Problem 10

Find the computational complexity for the following four loops:
a. $$
\begin{gathered}
\text { for }(\text { cnt } 1=0, i=1 ; i<=n ; i++) \\
\text { for }(j=1 ; j<=n ; j++) \\
\text { cnt } 1++;
\end{gathered}
$$
b.
$$
\begin{gathered}
\text { for (cnt2 }=0, i=1 ; i<=n ; i++) \\
\text { for }(j=1 ; j<=i ; j++) \\
\text { cnt } 2++;
\end{gathered}
$$
c.
$$
\begin{aligned}
\text { for }(\operatorname{cnt} 3 & =0, i=1 ; i<=n ; i *=2) \\
\text { for } & (j=1 ; j<=n ; j++) \\
& \text { cnt } 3++;
\end{aligned}
$$
d.
$$
\begin{aligned}
& \text { for }(\operatorname{cnt} 4=0, i=1 ; i<=n ; i *=2) \\
& \text { for }(j=1 ; j<=i ; j++) \\
& \text { cnt } 4++;
\end{aligned}
$$

James Kiss
James Kiss
Numerade Educator

Problem 11

Find the average case complexity of sequential search in an array if the probability of accessing the last cell equals $\frac{1}{2}$, the probability of the next to last cell equals $\frac{1}{4}$, and the probability of locating a number in any of the remaining cells is the same and equal to $\frac{1}{4(n-2)}$.

Check back soon!
02:50

Problem 12

Consider a process of incrementing a binary $n$-bit counter. An increment causes some bits to be flipped: Some $0 \mathrm{~s}$ are changed to $1 \mathrm{~s}$, some $1 \mathrm{~s}$ to $0 \mathrm{~s}$. In the best case, counting involves only one bit switch; for example, when 000 is changed to 001 , sometimes all the bits are changed, as when incrementing 011 to 100 .
$$
\begin{array}{cc}
\text { Number } & \text { Flipped Bits } \\
000 & \\
001 & 1 \\
010 & 2 \\
011 & 1 \\
100 & 3 \\
101 & 2 \\
110 & 2 \\ 111 & 1
\end{array}
$$
Using worst case assessment, we may conclude that the cost of executing $m=2^n-1$ increments is $O(m n)$. Use amortized analysis to show that the cost of executing $m$ increments is $O(\mathrm{~m})$.

Clarissa Noh
Clarissa Noh
Numerade Educator