• Tidak ada hasil yang ditemukan

RECURRENCE RELATIONS

remainder = M mod N

return GCD( N, remainder ) end if

Give the recurrence relation for the number of multiplications (in this case, the division and mod) that are done in this function.

3. We have a problem that can be solved by a direct (nonrecursive) algorithm that operates in N2 time. We also have a recursive algorithm for this prob- lem that takes N lg N operations to divide the input into two equal pieces and lg N operations to combine the two solutions together. Show whether the direct or recursive version is more efficient.

4. Draw the tournament tree for the following values: 13, 1, 7, 3, 9, 5, 2, 11, 10, 8, 6, 4, 12. What values would be looked at in stage 2 of finding the second largest value in the list?

5. What is the lower bound on the number of comparisons needed to do a search through a list with N elements? Think about what the decision tree might look like for this problem in developing your answer. (Hint: The nodes would be labeled with the location where the key is found.) If you packed nodes into this tree as tightly as possible, what does that tell you about the number of comparisons needed to search?

The second is used if the direct solution is applied for a larger number of cases:

These forms are equivalent. We can convert from the second form to the first by just listing those values for which we have the direct answer. This means that the second recurrence relation above could also be given as

Consider the following recurrence relation:

We will want to substitute an equivalent value for T(n 2) back into the first equation. To do so, we replace every n in the first equation with n 2, giving:

But now we can see when this substitution is done, we will still have T(n 4) to eliminate. If you think ahead, you will realize that there will be a series of these values that we will need. As a first step, we create a set of these equations for successively smaller values:

T n( ) 4 if n≤4 4T n( ⁄2)–1 otherwise



= 

T n( ) = 4T n( ⁄2)–1 T( )4 = 4

T( )3 = 4 T( )2 = 4 T( )1 = 4

T n( ) = 2T n( –2)–15 T( )2 = 40

T( )1 = 40

T n( –2) = 2T n( –2–2)–15 2T n( –4)–15

=

T n( –2) = 2T n( –4)–15 T n( –4) = 2T n( –6)–15 T n( –6) = 2T n( –8)–15 T n( –8) = 2T n( –10)–15 T n( –10) = 2T n( –12)–15

Now we begin to substitute back into the original equation. We will be care- ful not to simplify the resulting equation too much because that will make the pattern more difficult to see. Doing the substitution gives us

You are probably beginning to see a pattern develop here. First, we notice that each new term at the end of the equation is 15 multiplied by the next higher power of 2. Second, we notice that the coefficient of the recursive call to T is going through a series of powers of 2. Third, we notice that the value that we are calling T with keeps going down by 2 each time. Now, you might wonder When does this process end? If we look back at the original equation, you will see that we have a fixed value for T(1) and T(2). How many times would we have to substitute back into this equation to get to either of these values? We can see that 2 = n (n 2) if n is even. This seems to indicate that we would substitute back into this equation [(n 2) / 2] 1 times giving n / 2 1 terms based on 15 in the equation, and the power of the coefficient of T will be n / 2 1. To see this, consider what we would have if the value of n was 14. In this case, the previous sentence

T n( ) = 2T n( –2)–15 = 2 2T n( ( –4)–15)–15 T n( ) = 4T n( –4)–2 * 15–15

T n( ) = 4 2T n( ( –6)–15)–2 * 15–15 T n( ) = 8T n( –6)–4 * 15–2 * 15–15

T n( ) = 8 2T n( ( –8)–15)–4 * 15–2 * 15–15 T n( ) = 16T n( –8)–8 * 15–4 * 15–2 * 15–15

T n( ) = 16 2T n( ( –10)–15)–8 * 15–4 * 15–2 * 15–15 T n( ) = 32T n( –10)–16 * 15–8 * 15–4 * 15–2 * 15–15

T n( ) = 32 2T n( ( –12)–15)–16 * 15–8 * 15–4 * 15–2 * 15–15 T n( ) = 64T n( –12)–32 * 15–16 * 15–8 * 15–4 * 15–2 * 15–15

indicates that we would have substituted five times, would have six terms based on 15, and would have 26 for the coefficient of T(2). If you look closely at the last equation and substitute 14 in for n, you will see that this is exactly what we have.

What if n is an odd number? Will these formulas still work? Let’s consider an n value of 13. In the above equation, the only thing that would change is thatT would have a value of 1 instead of 2, but by our equations, n / 2 1 is 5 (not 6) when n is 13. For odd n, we will use n / 2 instead of n / 2 1. We will have two cases in our answer.

Now, applying Equation 1.17, for an even value of n we get

and, if n is odd, we get

Consider another recurrence relation:

We will proceed in the same way as we did in the previous example. We first substitute in a set of values for n, only in this case, because n is being divided

T n( ) 2(n2)1T( )2 15 2i ifn is even

i=0 n2

( )1

= T n( ) 2n2T( )1 15 2i ifn is odd

i=0 n2

=

T n( ) 2(n2)1 * 40 15 2i ifn is even

i=0 n2

( )1

= T n( ) 2n2 * 40 15 2i ifn is odd

i=0 n2

=

T n( ) = 2(n2)1 * 40–15 * (2n2–1) 2n2 * 20–2n2 * 15+15

=

2n2(20–15)+15

=

2n2 * 5+15

=

T n( ) = 2n2 * 40–15 * (2(n2)+1–1) 2n2 * 40–2n2 * 30 + 15

=

2n2(40–30)+15

=

2n2 * 10+15

=

T n( ) 5 if n≤4 4T n( ⁄2)–1 otherwise



= 

by 2, each of the subsequent equations will have half the value. This gives us the equations

Now, we again substitute back into the original giving the following series of equations:

We notice that the coefficient of 1 increases by a power of 4 each time we substitute, and it is the case that the power of 2 that we divide n by is 1 greater than the largest power of 4 for this coefficient. Also, we notice that the coeffi- cient of T is the same power of 4 as the power of 2 that we divide n by. When we have , its coefficient will be 4i and we will have terms from –(4i–1) to –1. Now, for what value of i can we stop the substitution? Well, because we

T n( ⁄2) = 4T n( ⁄4)–1 T n( ⁄4) = 4T n( ⁄8)–1 T n( ⁄8) = 4T n( ⁄16)–1 T n( ⁄16) = 4T n( ⁄32)–1 T n( ⁄32) = 4T n( ⁄64)–1

T n( ) = 4T n( ⁄2)–1 = 4 4T n( ( ⁄4)–1)–1 T n( ) = 16T n( ⁄4)–4 * 1–1

T n( ) = 16 4T n( ( ⁄8)–1)–4 * 1–1 T n( ) = 64T n( ⁄8)–16 * 1–4 * 1–1

T n( ) = 64 4T n( ( ⁄16)–1)–16 * 1–4 * 1–1 T n( ) = 256T n( ⁄16)–64 * 1–16 * 1–4 * 1–1 T n( ) = 256 4T n( ( ⁄32)–1)–64 * 1–16 * 1–4 * 1–1 T n( ) = 1024T n( ⁄32)–256 * 1–64 * 1–16 * 1–4 * 1–1 T n( ) = 1024 4T n( ( ⁄64)–1)–256 * 1–64 * 1–16 * 1–4 * 1–1 T n( ) = 4096T n( ⁄64)–1024 * 1–256 * 1–64 * 1–16 * 1–4 * 1–1

T n( ⁄2i)

have the direct case specified for n≤ 4, we can stop when we get to . Putting this together we get

Using the value for the direct case, and Equation 1.18, we get

As you can see, the closed form of a recurrence relation may not be simple or neat, however, it does eliminate the recursive “call” so that we can quickly compare equations and determine their order.

1.6.1

Put the following recurrence relations into closed form.

1.

2.

3.

4.

T( )4 = T n( ⁄2lgn2)

T n( ) 4lgn2T( )4 4i

i=0 lgn3

=

T n( ) 4lgn2 * 5 4lgn2–1 4–1 --- –

=

T n( ) 4lgn2 * 5 4lgn2–1 ---3 –

=

T n( ) 15 * 4lgn2–4lgn2+1 ---3

=

T n( ) 4lgn2(15–1)+1 ---3

=

T n( ) 4lgn2 * 14+1 ---3

=

1.6.1 EXERCISES

T n( ) = 3T n( 1)15 T( )1 = 8

T n( ) = T n( 1)+n1 T( )1 = 3

T n( ) = 6T n( 6)+2n+3 T( )1 = 1

forn a power of 6

T n( ) = 4T n( 3)+2n1 T( )1 = 2

forn a power of 3