17.01.2013 Views

Data Structures and Algorithm Analysis in C - SVS

Data Structures and Algorithm Analysis in C - SVS

Data Structures and Algorithm Analysis in C - SVS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Structures</strong>, <strong>Algorithm</strong> <strong>Analysis</strong>: CHAPTER 10: ALGORITHM DESIGN TECH...<br />

from this.<br />

Skip lists are similar to hash tables, <strong>in</strong> that they require an estimate of the number of elements<br />

that will be <strong>in</strong> the list (so that the number of levels can be determ<strong>in</strong>ed). If an estimate is not<br />

available, we can assume a large number or use a technique similar to rehash<strong>in</strong>g. Experiments have<br />

shown that skip lists are as efficient as many balanced search tree implementations <strong>and</strong> are<br />

certa<strong>in</strong>ly much simpler to implement <strong>in</strong> many languages.<br />

10.4.3. Primality Test<strong>in</strong>g<br />

In this section we exam<strong>in</strong>e the problem of determ<strong>in</strong><strong>in</strong>g whether or not a large number is prime. As<br />

was mentioned at the end of Chapter 2, some cryptography schemes depend on the difficulty of<br />

factor<strong>in</strong>g a large, 200-digit number <strong>in</strong>to two 100-digit primes. In order to implement this scheme,<br />

we need a method of generat<strong>in</strong>g these two primes. The problem is of major theoretical <strong>in</strong>terest,<br />

because nobody now knows how to test whether a d-digit number n is prime <strong>in</strong> time polynomial <strong>in</strong> d.<br />

For <strong>in</strong>stance, the obvious method of test<strong>in</strong>g for the divisibility by odd numbers from 3 to<br />

requires roughly divisions, which is about 2 d/2 . On the other h<strong>and</strong>, this problem is not<br />

thought to be NP-complete; thus, it is one of the few problems on the fr<strong>in</strong>ge--its complexity is<br />

unknown at the time of this writ<strong>in</strong>g.<br />

Figure 10.61 Before <strong>and</strong> after an <strong>in</strong>sertion<br />

In this chapter, we will give a polynomial-time algorithm that can test for primality. If the<br />

algorithm declares that the number is not prime, we can be certa<strong>in</strong> that the number is not prime.<br />

If the algorithm declares that the number is prime, then, with high probability but not 100<br />

percent certa<strong>in</strong>ty, the number is prime. The error probability does not depend on the particular<br />

number that is be<strong>in</strong>g tested but <strong>in</strong>stead depends on r<strong>and</strong>om choices made by the algorithm. Thus,<br />

this algorithm occasionally makes a mistake, but we will see that the error ratio can be made<br />

arbitrarily negligible.<br />

The key to the algorithm is a well-known theorem due to Fermat.<br />

THEOREM 10.10.<br />

Fermat's Lesser Theorem: If p is prime, <strong>and</strong> 0 < a < p, then a p-1 1(mod p).<br />

PROOF:<br />

A proof of this theorem can be found <strong>in</strong> any textbook on number theory.<br />

mk:@MSITStore:K:\<strong>Data</strong>.<strong>Structures</strong>.<strong>and</strong>.<strong>Algorithm</strong>.<strong>Analysis</strong>.<strong>in</strong>.C.chm::/...<br />

页码,53/83<br />

2006-1-27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!