Professional Documents
Culture Documents
خورزميات اسئله3
خورزميات اسئله3
خورزميات اسئله3
Ex. 1: Write a data list that results from running the shuffle-left algorithm to clean up the
following data. Find the exact number of copies done.
3002670051
Solution: There are 10 numbers in this list, four of which are zero, meaning invalid. Position 2 is a
zero, meaning that every cell from 3 to 10 needs to be copied left. After this first copy, we have
the following list:
Thus we see that eight copies have been performed on this first step. Unfortunately, another zero
has been shifted into position 2, meaning that we need to make another eight copies to produce
the list:
Now we see a zero in position 5, meaning that we need to copy cells 6 through 10 left:
This has performed five copies, but now we must perform five more because of the last zero in
position 5:
Note that the algorithm is particularly inefficient here in making extra copies which were not
necessary – since legit gets reduced to 9, there is no need to copy position 10 into position 9.
When legit gets reduced to 8, there is no need to copy 10 into 9 and 9 into 8, and so forth Thus
there are six useless copies in this algorithm execution.
Once item n has been copied one cell left, it need not be copied again. Similarly, once item n
– 1 has been copied one cell left, it need not be copied again. The value of legit shows how
many cells from the right end have been copied; the algorithm can be changed to “while right is
less than or equal to legit." 8 + 7 + 3 + 2 = 20 copies
Ex. 2: Write the resulting data list and find the exact number of copies done by the converging-
pointers algorithm when it is executed on the data in Example 1.
3002670051
Ex. 3: Write the resulting data list and find the exact number of copies done by the copy over
algorithm when it is executed on the data in Example 1.
Solution:
6 copies to create 2nd list.
Solution
1) False, since n^2 (the dominate term on the left) is asymptotically faster growing than n log n and
hence not upperbounded by it.
(2,3) True, since n^2 (the dominate term on the left) asymptotically grows like n^2 and hence it is
Omega(n^2) and also Theta(n^2). faster growing than n log n and hence not upper bounded by it.
4)False since n log n (the dominate term on the left) is not asymptotically upper bounded by n.
5)True, since the dominate term on the left, 10 SQRT(n), is asymptotically upper bounded by n.
(6,7) False, since the dominate term on the left, SQRT(n), is not asymptotically upper bounded by n.
See the class notes where we showed that that lim as n -> infinity of log n / SQRT(n) = 0 giving that
SQRT(n) is asymptotically faster growing.
(8) False, since the dominate term on the left, SQRT(n), is asymptotically faster slower growing than n.
(9) True, since the dominate term on the left, 2 SQRT(n), grows asymptotically at the same rate as
SQRT(n).
(10) True, since the dominate term on the left, SQRT(n), is asymptotically faster growing than 1.
(11) True, since the dominate term on the left, SQRT(n), is asymptotically faster growing than log n.
(12) False, since the dominate term on the left, SQRT(n), is asymptotically slower growing than n.
Ex. 5: Give the best asymptotic (“big-Oh”) characterization of the worst case and the best case time
complexities of the algorithm DoAgain(A, n).
Algorithm DoAgain(A,n)
Input: Array A storing integers and of size n > 1.
sum ← 0
for c ← 0 to n2 do
if A[0] < 0 then
for k ← 0 to n − 1 do
sum ← sum + c · A[k]
a) Best case O(n) and worst case O(n2) b) Best case O(n) and worst case O(n3)
c) Best case O(n2) and worst case O(n3) d) Best case O(n) and worst case O(n4)
e) Best case O(n3 ) and worst case O(n4) f) Best case O(n4) and worst case O(n4)
Ex. 6: Use big-O notation to summarize the asymptotic behavior of the following function:
void test ( )
for(int s=1; s<n;s++){
for(int t=1; t<n;t=t*6){
System.out.println(t);
}
}
void silly(int n) {
for (inti = 0; i< n * n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k <i; ++k)
System.out.println(”k = ” + k);
for (int m = 0; m < 100; ++m)
System.out.println(”m = ” + m);
}
}
}
a) O(n5) b) O(n4) c) O(n6) d) O(n2) e) O(logn3) f) O(n3)