How to measure the time complexity of an
algorithm?
Identify an important operation in the algorithm...
How to measure the time complexity of an
algorithm?
Identify an important operation in the algorithm that is
executed most frequently.
Express the number of times it is executed as a function of
N.
Convert this expression into the Big-O notation.
A. For each of the three fragments of code, what is its
worst-case time complexity, in the form "O(…)". (Use the
given solution to the first problem as a
model)
//----------------- This is a sample problem – solved
------...
2. Design a deterministic algorithm to solve the following
problem.
input: An array A[1...n] of n...
2. Design a deterministic algorithm to solve the following
problem.
input: An array A[1...n] of n integers.
output: Two different indices i and j such that
A[i] = A[j], if such indices exist.
Otherwise, return NONE.
Your algorithm must take O(n log(n)) time. You must describe your
algorithm in plain English (no pseudocode) and you must explain why
the running time of your algorithm is O(n log(n)).
Below is C code and Python code for an algorithm.
C code:
void foo( int n,...
Below is C code and Python code for an algorithm.
C code:
void foo( int n, int A, int B, int C ) {
if( n==1 ) {
printf("%d to %d\n",A,B);
return;
}
foo( n-1, A, C, B );
printf("%d to %d\n",A,B);
foo( n-1, B, C, A );
Python code:
def foo(n , A, B, C):
if n==1:
print A, "to", B
return
foo(n-1, A, C, B)
print A, "to", B
foo(n-1, B, C, A)
Let Hn be the number...
Given the following algorithm, matching each statement to the
correct sequence for complexity analysis.
procedure Alg3(A):...
Given the following algorithm, matching each statement to the
correct sequence for complexity analysis.
procedure Alg3(A): A is a list of n integers
1 for i = 1 to n-1 do
2 x=aix=ai
3 j = i − 1
4 while (j ≥≥ 0) do
5 if x≥ajx≥aj then
6 break
7 end if
8 aj+1=ajaj+1=aj
9 j = j − 1
a end while
b aj+1=xaj+1=x
c end for
d return A
The complexity of this algorithm is O(n2)O(n2)...
Consider the following function:
01: void cpUniq (const int* source, int N, vector<int>& dest)
02: {...
Consider the following function:
01: void cpUniq (const int* source, int N, vector<int>& dest)
02: {
03: list<int> L;
04: for (int i = 0; i < N; ++i)
05: {
06: copy (source, source + i, front_inserter<int>(L));
07: }
08: for (int j: L)
09: {
10: if (find(dest.begin(), dest.end(), j) != dest.end())
11: dest.push_back(j);
12: }
13: }
and the following list of complexity values:
A: O(1), B: O(log N), C: O(N), D: O(N log N), E:
O(N2),...