Laboratory Exercise 02: Problems on Recurrence Relations

Total Page:16

File Type:pdf, Size:1020Kb

Laboratory Exercise 02: Problems on Recurrence Relations

CmSc 250 Intro to Algorithms Laboratory Exercise 02: Problems on Recurrence Relations Due 10/08

1. von Neumann’s neighborhood. How many one-by-one squares are generated by the algorithm that starts with a single square and on each of its n iterations adds new squares all round the outside. How many one-by-one squares are generated on the nth iteration? The results for n = 0, 1, and 2 are illustrated below:

Find the number of cells in the von Neumann neighborhood of range n by setting up and solving a recurrence relation

2. Write a pseudocode for a recursive algorithm for finding the position of the largest element in an array of n numbers. Set up and solve a recurrence relation for the number of key comparisons made by your algorithm. How does this algorithm compare with the iterative algorithm for this problem?

3. Write a pseudocode for a recursive algorithm that determines whether a given word is a palindrome. A given word is a palindrome if it can be read backwards, for example pop, rotor, noon. Set up and solve a recurrence relation for the number of comparisons made by your algorithm. How does this algorithm compare with the iterative algorithm for this problem?

4. Write a pseudocode for a recursive algorithm that reverses the contents of an array. Set up and solve a recurrence relation for the number of swaps made by your algorithm. How does this algorithm compare with the iterative algorithm for this problem?

Optional: 5. There are n bacteria and 1 virus in a Petri dish. Within the firs minute, the virus kills one bacterium and produces another copy of itself, and all of the remaining bacteria reproduce, making 2 viruses and 2*(n-1) bacteria. In the second minute, each of the viruses kills a bacterium and produces a new copy of itself (resulting in 4 viruses and 2*(2*(n-1) -2) = 4*n – 8 bacteria; again, the remaining bacteria reproduces. This process continues every minute. Will the viruses eventually kill all the bacteria? If so, design and implement an algorithm that will compute how many steps it will take. How does the running time of your algorithm depend on n?

1 2

Recommended publications