




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
adadadadadadadadadadadadadadada
Typology: Exams
1 / 111
This page cannot be seen from the preview
Don't miss anything!
The numbers in the first row are quite large. The table below calculates it approxi- mately in powers of 10. People might also choose to use powers of 2. Being close to the answer is enough for the big numbers (within a few factors of 10 from the answers shown).
1 Second 1 Hour 1 Month 1 Century
log n 210
6 ≈ 10300000 23.^6 ×^10
9 ≈ 1010
9 22.^6 ×^10
12 ≈ 100.^8 ×^10
12 23.^1 ×^10
15 ≈ 1010
15 √ n ≈ 1012 ≈ 1. 3 × 1019 ≈ 6. 8 × 1024 ≈ 9. 7 × 1030 n 106 3. 6 × 109 ≈ 2. 6 × 1012 ≈ 3. 12 × 1015 n log n ≈ 105 ≈ 109 ≈ 1011 ≈ 1014 n^2 1000 6 × 104 ≈ 1. 6 × 106 ≈ 5. 6 × 107 n^3 100 ≈ 1500 ≈ 14000 ≈ 1500000 2 n^ 19 31 41 51 n! 9 12 15 17
The Loop1 method runs in O ( n ) time.
The Loop3 method runs in O ( n^2 ) time.
The Loop4 method runs in O ( n^2 ) time.
By the definition of big-Omega, we need to find a real constant c > 0 and an integer constant n 0 ≥ 1 such that n^3 log n ≥ cn^3 for n ≥ n 0. Choosing c = 1 and n 0 = 2, shows n^3 log n ≥ cn^3 for n ≥ n 0 , since log n >≥ 1 in this range.
To say that Al’s algorithm is “big-oh” of Bill’s algorithm implies that Al’s algo- rithm will run faster than Bill’s for all input greater than somenon-zero positive integer n 0. In this case, n 0 = 100.
One possible solution is f ( n ) = n^2 + ( 1 + sin ( n )).
The induction assumes that the set of n − 1 sheep without a and the set of n − 1 sheep without b have sheep in common. Clearly this is not true with the case of 2 sheep. If a base case of 2 sheep could be shown, then the induction would be valid.
For convenience assume that n is even. Then
n
i = 1
log 2 i / geq
n
i = n 2 + 1
log 2
n 2
n 2
log 2
n 2
which is Ω( n log n ).
Let Bn − 1 , Bn − 2 ,..., B 1 , B 0 be the n bottles of wine. Consider any binary string b = bn − 1 bn − 2 ... b 1 b 0 of length n. A string b corresponds to a test as follows. If b has k 1’s, we give a taste tester to drink a sample that consists of exactly k drops: a drop of bottle Bi is included to the sample only if bi = 1. The idea is to have that sufficient collection of tests b 1 , b 2 ,..., bT running in parallel, so that, when (after a month) their outcome is known, we can deterministically identify the poisoned bottle. There are several ways for someone to construct the tests. A naive solution uses n tests, each of them having a drop from only one distinct bottle. We can do much better. An efficient construction of the tests will be in such a way so that a binary search is performed in the sequence of the bottles. For simplicity assume that n is a power of 2, i.e., n = 2 k^ for some integer k. We define B ( t ) to be the binary string of length t that consists of t 2 consecutive 0’s and followed by 2 t 1’s. Let the first test be b 1 = B ( n ). Given the output of this test, our search space is reduced by half: if b 1 is positive then we know that the poisoned bottle is one of Bn 2 − 1 ,..., B 1 , B 0 , otherwise one of Bn − 1 , Bn − 2 ,..., Bn 2.
Let now the second test be b 2 = B ( n 2 )|| B ( n 2 ), where || denotes string concate- nation. Clearly tests b 1 and b 2 reduce the search space to one forth of the ini- tial. We proceed in the same way: b 3 = B ( n 4 )|| B ( n 4 )|| B ( n 4 )|| B ( n 4 ) and, generally, bi = B ( (^) i + n 1 )|| B ( (^) i + n 1 )|| B ( (^) i + n 1 )|| B ( (^) i + n 1 ), for 1 ≤ i ≤ k , where k = log n. Combining all k tests b 1 , b 2 , ..., bk the search space is finally reduced to only one bottle. A similar reasoning can be followed in the case that n is not an exact power of 2; in that case, the number of tests are ⌈ n ⌉. We give an example for n = 13. The tests could be:
test 1: b 1 =11111100 0 0000
test 2: b 2 =11100011 1 0000
test 3: b 3 =10010010 0 1100
test 4: b 4 =11011011 0 1010,
Start at the upper left of the matrix. Walk across the matrix until a 0 is found. Then walk down the matrix until a 1 is found. This is repeated until the last row or column is encountered. The row with the most 1’s is the last row which was walked across. Clearly this is an O ( n )-time algorithm since at most 2 · n comparisons are made.
Using the two properties of the array, the method is described as follows.
An example of the traversal of the array is shown in the figure 0.1.
array A
Figure 0.1: The traversal of the array.
The pseudo-code of the algorithm is shown below. The running time is O ( n ). In the worst case, you will visit at most 2 n − 1 places in the array. In the case that the diagram has all 0s in rows 2 through n and 1s in the first row, then there will be n − 1 iterations of the for loop at constant time (since it will never enter the while loop) and 1 iteration of the while loop which has n iterations of constant time.
Name the two stacks as E and D , for we will enqueue into E and dequeue from D. To implement enqueue( e ), simply call E .push( e ). To implement dequeue(), simply call D .pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D , until E is empty, and then call D .pop(). For the amortized analysis, charge $2 to each enqueue, using $1 to do the push into E. Imagine that we store the extra cyber-dollar with the element just pushed. We use the cyber-dollar associated with an element when we move it from E to D. This is sufficient, since we never move elements back from D to E. Finally, we charge $1 for each dequeue to pay for the push from D (to remove the element returned). The total charges for n operations is O ( n ); hence, each operation runs in O ( 1 ) amortized time.
The number of permutations of n numbers is n! = n ( n − 1 )( n − 2 )... 3 · 2 · 1. The idea is to compute this product only with increments of a counter. We use a stack for that purpose. Starting with the call Enumerate( 0 , S ), where S is a stack of n elements. the pseudo code should be like that:
Algorithm Enumerate( t , S ){ k = S .size(); while (! S .isEmpty() ) { S.pop(); t ++; S ′: a new stack of size k − 1 Enumerate( t , S ′); } }