Sie sind auf Seite 1von 3

Data Structures & Algorithms Sheet #1 - Solution

Cairo University
Faculty of Engineering
Computer Engineering Department
Data Structures and Algorithms
Sheet #1 - Solution
Algorithms Efficiency
Important points:

• Algebraic functions grow faster than logarithmic functions, but slower than exponential
functions. Commented [M. Ismail1]:
• When comparing two functions, discard common factors. For example, when comparing N log N Students know the order given by the book:
and N log2 N we are basically comparing 1 to log N. log N is greater. 1 < Log n < n < n log n < n2 < n3 <… nk <2n < n!
So on this scale they should locate other functions given here
• In general: Given exact no. of iterations: f(n), the Big-O notation can be derived from f (n) using - log N2 = 2 log N = O( log N)
the following steps: =====================
- log2 N:
1- In each term, set the coefficient of the term to 1. log2 N > logN
2- Keep the largest term in the function and discard the others. log2 N < N (by numbers, use large N)
For example, ========================
- N log2 N:
F(n) = 3 n2 + 10 log n Nlog2 N >N logN
Step 1: F(n) = O(n2 + log n) N log2 N ? N2
Step 2: F(n) = O(n2) As log2 N< N(as seen above)  N log2N<N2
===================
- N2 log N:
1. Order the following functions by growth rate: N2 log N > N2
N2 log N < N3 (because logN<N)
N, N2, log N, N log N, log(N2), log2 N, N log2 N, 2, 2N, 37, N2 log N, 5logN,
N3 , 10N log N2
Indicate which functions grow at the same rate. Order should be:
I put those with same growth rate in square brackets
2, 37 , [log N, 5logN, logN2], log2N ,N ,[ N log N, 10N log N2], N log2
2. For each of the following program fragments, estimate the order of time complexity. N, N2, N2 log N, N3 , 2N
(a) Commented [M. Ismail2]: Linear Loops
count=0; O(n)
for(i=0; i<n; i++) Commented [E3]: Linear Loops
count++; Number of iters = F(n) = n/2
O(n)
Commented [E4]: Logarithmic Loops
(b) O(log n)
count=0; Prove it to them by making this table:
for(i=0; i<n; i+=2) iter | value of i
1|1
count++; 2|4
3|8
4 | 16
(c) Iter | 2^iter
count=0; Value of last i = n (approximately)
for(i=1; i<n; i*=2) 2^iter = n
By taking: log base 2
count++; Iter = log n (base 2)
= log n / log 2
(d) = O (log n)
Tell them if the i step is: i*=5 or any other const.
count=0; The complexity will be still O(logn)
for(i=1; i<n; i*=2) The same if i/=5
count++; Commented [E5]: O(n)
for(i=0; i<n; i+=2) F(n) = log n + n/2 = O(n)
count++;

CMP 102 & CMP N102 1/3 Spring 2018


Data Structures & Algorithms Sheet #1 - Solution

(e) Commented [M. Ismail6]: Nested Loops: Quadratic


count=0; O(n2)

for(i=0; i<n; i++)


for(j=0; j<n; j++)
count++;

(f) Commented [M. Ismail7]: Nested Loops: Linear Logarithmic


count=0; O(n log n)

for(i=0; i<n; i++)


for(j=0; j<n; j*=2)
count++;

(g) Commented [M. Ismail8]: Nested Loops: Dependent


Quadratic
count=0; O(n2)
for(i=0; i<n; i++) Inner loop is executed 1+2+3+4+….+n times
for(j=0; j<i; j++) Sum (i), i from 1 to n = n(n+1)/2
Which is n(n+1)/2  O(n2)
count++;

(h) Commented [M. Ismail9]: Nested Loops: Polynomial


count=0; O(n3)

for(i=0; i<n; i++)


for(j=0; j<n*n; j++)
count++;

(i) Commented [E10]:


count1=0; count2=0; count3=0; O( n log n + n) = O (n log n)

for(i=0; i<n; i++)


for(j=1; j<n; j*=2)
{ count1++;
count2++;
count3++;
}
for(i=0; i<n; i++)
{ count1++;
count2++;
count3++;
} Commented [M. Ismail11]:
Should be left as an exercise for students

(j) O(n5)
count=0; Because j’s loop executed up to i*i
J’s loop is executed 1+4+9+16+….+n2
for(i=0; i<n; i++) = 12+22+32+42+…….n2
for(j=0; j<i*i; j++) = sum of first perfect squares
= n(n+1)(2n+1)/6 = O(n3)
for(k=0; k<j; k++) Then relation between k’s loop and outer loops is as shown in
count++; previous problem
i.e quadratic relation “O(n2)”
So the full loops will be O(n3 * n2) = O(n5)

CMP 102 & CMP N102 2/3 Spring 2018


Data Structures & Algorithms Sheet #1 - Solution

3. In each of the following problems, you are given two algorithms that do the same
job. You are required to indicate which of the two algorithms is “better” and why. Commented [M. Ismail12]: Discussion about meaning of
“better” should be held
a. The following two algorithms count the number of values that are above the
average of a given set of values.

Algorithms: AboveAvg1 & AboveAvg2


Input L: List of integer values, N: size of the List
Output C: number of values above average of all values

AboveAvg1 (L, N) AboveAvg2 (L, N) Commented [M. Ismail13]: Two separate loops, each
executes N times
1. Set Avg = 0, count = 0, C = 1. Set Avg = 0, C = 0 = 2N = O(N)
0 2. For J = 0 to N - 1
Commented [M. Ismail14]: Two nested loops = O(N2)
2. For I = 0 to N - 1 1- set count = 0
1- set count = count + L[I] 2- For I 0 to N - 1 Of course the problem here is that the algo calculates the average
3. Set Avg = count/N 1- set count = count + every time it wants to check if a value above avg or not which is
unnecessary as avg will not change and should be calculated once
4. For I = 0 to N - 1 L[I]
1- IF ( L[I] > Avg ) 3- Set Avg = count/N C++ code is given with this sheet
The code prints the run time of both algorithms
1- Set C = C + 1 4- IF ( L[J] > Avg ) So after solving the problem, the instructor can run the code and
5. Return C 1- Set C = C + 1 with increasing values for N to show students how algo efficiency is
3. Return C effective for large values of N

b. The following two algorithms take a list L and output another list M such that Commented [E15]: Both have the same complexity O(n)
but EvensFirst2 is better in execution time
M contains the even numbers of L first then L’s odd numbers. because it iterates on the list once (n)
but tEvensFirst1 iterates on it twice (2*n)
Algorithms: EvensFirst1 & EvensFirst2
Input L: List of integer values, n:size of List L
Output M: List of integers such that even numbers are first then odd numbers

EvensFirst1 (L, n, M) EvensFirst2 (L, n, M)


1. J = 0 1. J = 0
2. For I = 0 to n - 1 2. K = Size - 1
1- IF ( L[I] % 2 == 0 ) // 3. For I = 0 to n - 1
Even 1- IF ( L[I] % 2 == 0 ) //
a. M[J] = L[I] Even
b. J = J+1 a. M[J] = L[I]
3. For I = 0 to n - 1 b. J = J+1
2- IF ( L[I] % 2 != 0 ) // 2- ELSE // Odd
Odd a. M[K] = L[I]
a. M[J] = L[I] b. K = K - 1
b. J = J+1 4. Return M
4. Return M

CMP 102 & CMP N102 3/3 Spring 2018

Das könnte Ihnen auch gefallen