Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithm Design: Solutions to Exercises from Goodrich and Tamassia's Textbook, Exams of Java Programming

adadadadadadadadadadadadadadada

Typology: Exams

2017/2018

Uploaded on 01/11/2018

jim-allen
jim-allen 🇹🇷

7 documents

1 / 111

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm Design
M. T. Goodrich and R. Tamassia
John Wiley & Sons
Solution of Exercise R-1.7
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
logn2106
10300000 23.6×109
1010922.6×1012
100.8×1012 23.1×1015
101015
n1012 1.3×1019 6.8×1024 9.7×1030
n1063.6×1092.6×1012 3.12×1015
nlogn10510910111014
n21000 6×1041.6×1065.6×107
n3100 1500 14000 1500000
2n19 31 41 51
n! 9 12 15 17
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Algorithm Design: Solutions to Exercises from Goodrich and Tamassia's Textbook and more Exams Java Programming in PDF only on Docsity!

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise R-1.

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

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise R-1.

The Loop1 method runs in O ( n ) time.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise R-1.

The Loop3 method runs in O ( n^2 ) time.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise R-1.

The Loop4 method runs in O ( n^2 ) time.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise R-1.

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 ncn^3 for nn 0. Choosing c = 1 and n 0 = 2, shows n^3 log ncn^3 for nn 0 , since log n >≥ 1 in this range.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

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.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

One possible solution is f ( n ) = n^2 + ( 1 + sin ( n )).

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

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.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

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 ).

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

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 ≤ ik , 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,

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

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.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-1.

Using the two properties of the array, the method is described as follows.

  • Starting from element A [ n − 1 , 0 ], we scan A moving only to the right and upwards.
  • If the number at A [ i , j ] is 1, then we add the number of 1s in that column ( i + 1) to the current total of 1s
  • Otherwise we move up one position until we reach another 1.

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.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-2.

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.

M. T. Goodrich and R. Tamassia

John Wiley & Sons

Solution of Exercise C-2.

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 ′); } }