authentication and integrity, 533-537discarding invalid winnowing, 558hiding steganography, 555-558passwords, authorizing, 528-533 smoothing, 648 data encryption see encryption Data Encr
Trang 1access methods, 526
Trang 2authentication and integrity, 533-537discarding invalid (winnowing), 558hiding (steganography), 555-558
passwords, authorizing, 528-533
smoothing, 648
data encryption (see encryption)
Data Encryption Standard (see DES)
data link escape (DLE) character, 414
tracking both ends, 52-55
lookup searching and, 159, 173
matrices (see matrices)
sets (see sets)
traversal performance, 72
union-tree forests, 326
Trang 3within arrays, 37-45
dataflow analysis, 396
Date: :Calc module, 489
Date: :Convert module, 490
Date: :GetDate module, 490
Date: :Manip module, 489
dates and times, 489
Trang 4binary tree elements, 77, 80-81, 84
graph edges and vertices, 297-299
linked list elements, 55, 60
set members (subtracting), 205, 218
DES (Data Encryption Standard), 528, 543
Perl implementation (SSLeay), 543
DES-EDE and DES-EDE3 encryption algorithms, 543descendant vertices, 315
destroy( ) (double package), 66, 68
Trang 5difference levels (Wu-Manber), 382
differences and fuzzy matching, 379
directed acyclic graphs (DAGs), 304, 312single-source shortest paths, 338
directed graphs and edges, 277-279, 291-293directed( ), 292
direction, specifying, 433-435
directional alternative hypotheses, 612
directories, searching with B-trees, 169
discrete probability distributions, 575
algorithms for (list), 592-598
Trang 6greatest common (see GCD)
DLE character, 414break
Page 664
Dominus, Mark-Jason, 71, 113, 171, 386, 416, 505
double package (example), 65
double-ended queues (deques), 40-42
circular linked lists vs., 60
double_head package, 68
doubly-linked lists, 65-71
downloading files, risks, 534
dynamic programming, 22, 202
Trang 7matrix chain products, 269dynamic variables, 36
caching, 16
dynamic allocation, 37
floating-point numbers, 14heaps, 92
linked lists, 47
tail pointers, 54
list traversal performance, 72
Trang 9of Perl source code, 562-564
product development (government restrictions), 527public key, 533, 543, 548-554
winnowing and chaffing, 558-562
(see also cryptography)
encryption object, creating, 546
English stemming (see stemming)
epsilon, 472
eq statement, 359break
Trang 10Page 665
equations, solving, 634-642
approximating roots, 638-640
multiple nonlinear equations, 640-642
quadratics and cubics, 635-638
Eratosthenes, sieve of, 504
shift-OR exact matching, 377-379
exclusive binary or (XOR) operator, 220-221
exclusive logical or (xor) operator, 210
exclusive-or (XOR), 538, 540
exhaustive search, 180-185
Trang 12factoring large numbers, 549
false hits (string matching), 357, 365
feedback methods (encryption), 543
B-trees to search for, 169
content changes, detecting, 526
downloading, security risks, 534
encrypting/decrypting entire, 547
mailing (security risks), 527
Filter module, 562
filtering Perl source code, 562
finding (see searching)
finite arithmetic, 510
finite automata, 395-396
deterministic vs nondeterministic, 396
Trang 13fuzzy (approximate) matching, 353, 379, 388
Baeza-Yates-Gonnet shift-add algorithm, 379-382
Baeza-Yates-Gonnet shift-OR algorithm, 377
longest common subsequence (LCS), 386
regular expressions (String::Approx), 387
Wu-Manber k-differences algorithm, 382-387
(see also phonetic algorithms)
fuzzy sets, 241
Trang 14exhaustive search, 180-185
game interface for, 176-180
killer move strategy, 189, 202
Trang 15inclusion within polygons, 443-449intersections, 435-443
line intersection, 435-443
Manhattan intersections, 440-443trigonometry, 491
Trang 16grading test results, 607
Graham's scan algorithm, 452-456
Graph: :Directed class, 278, 293, 299
Graph: :Kruskal class, 231, 352
Graph: :Undirected class, 278, 293, 299
(see also graphs)
graphical application development, 467
graphics, general references on, 650
graphics modules, 464-468
graphs, 273-352
attributes, 286, 299
Trang 17classes and connectivity, 279-281, 296, 316-326biconnected graphs, 320-323
Traveling Salesman problem, 350
CPAN modules for, 351
edges (see vertices, graph)
general references on, 650
Trang 18graphviz package, 301
great circle distance, 429
great_circle_distance( ), 429
greatest common divisor (see GCD)
greedy search algorithms, 195
Trang 19avoiding searching with, 158
hashes of hashes (hohs), 29
hashes of lists (hols), 29, 118
Huffman encoding, 417
inserting and deleting elements, 55
binary search and, 164
lists of hashes (lohs), 29break
Page 668
(cont'd)
hashes
lists of lists (lols), 29
lookup searching and, 174
Heap: :Binary module, 99
Heap: :Binomial module, 99
Heap: :Fibonacci module, 99
heapdown( ), 95-99
Trang 20heapdump( ) (Heap modules), 101
branch and bound method, 196
caching prime numbers, 505
efficiency of, 92
finding maximum/minimum, 122-125Heap modules, 99
hohs (hashes of hashes), 29
hols (hashes of lists), 29
reversing element order, 118
Horner's rule, 363
Howland, Gary, 568
HP program 183
Trang 21images, secret messages in, 555
imaginary (complex) numbers, 485
implementations of algorithms, 6
in_degree( ), 297
in-degree, vertex, 279
in site (in place) sorting, 119
inclusion within polygons, 443-449
incremental sum (Rabin-Karp), 365
Trang 22individuals using cryptography, 527
infinite discrete distributions, 590-591
Trang 24iterative vs recursive algorithms, 21
J
jacobian( ), 630, 640
Jacobian matrix, 629-631, 640
Janus heaps, 99
joining binary trees, 85-86
junk messages, adding (chaffing), 558
K
k-connected graphs, 286, 320
keys, encryption, 540, 542
converting from hex to bytes, 548
interchanging public and private, 552lost or stolen, 564
Trang 25Laplace distribution, 596
laplace_expected( ), 596
laplace_variance( ), 596
last occurrence heuristic, 373
LCM (least common multiple), 504lcm( ), 504
LCS (longest common subsequence), 386ldump( ) (double_head), 70
leaf vertices, 312
least common multiple (LCM), 504legal issues (cryptography), 526-527Lehman, Marc, 465
linear combination theorem, 500
linear congruential generators, 567linear least squares method, 622
linear search, 161
Trang 26linear_solve( ), 643
linear_string( ), 161break
Page 670
lines, fitting to data, 622-624
lines, searching for multiple, 362
Lingua: :EN: :Inflect module, 393
Lingua: :PT: :Conjugate module, 393
links (see edges, graph; graphs)
list iterators (Set: :IntSpan), 230
lists (see arrays; hashes)
literal( ), 399
loaded dice, 582-589
local statement, 36
locales and sorting, 113-114
location of variables (see references)
Trang 27logical and (&&) operator, 240
logical exclusive or (xor) operator, 210logical not (!) operator, 240
logical or ( | | ) operator, 210, 240
lognormal( ), 597
lognormal_expected( ), 597
lognormal_variance( ), 597
lohs (list of hashes), 29
lols (lists of lists), 29, 34
binary search in list, 162-165
binary search in tree, 167-171
Trang 28Marquess, Paul, 117, 170, 423, 562match heuristic, 373
matching strings (see regular expressions)Math modules
Math: :BigFloat module, 477-480Math: :BigInt module, 363, 477-480random BigInts, 579-581
Math: :Complex module, 485, 635Math: :Fraction module, 480
Math: :Matrix module, 245, 641Math: :MatrixBool module, 231Math: :MatrixReal module, 245adding constants to matrices, 248
Trang 29adding matrices together, 251
Math: :Trig module, 429, 491
Math: :TrulyRandom module, 568
Trang 30chain products, 269-272rotating, 258
Trang 31minimum cost spanning tree, 185
minimum spanning trees (MSTs), 326-332Kruskal's algorithm, 326-329, 352
Trang 32Monty Hall problem, 590
move object (game interface), 177
multibyte character matching, 360
multibyte character sorting, 107break
Trang 33LCM (least common multiple), 504multiplication (*) operator, 257
multiplying matrices together, 256-258chain products, 269-272
National Security Agency (NSA)
strong encryption, controlling use, 527natural numbers, 469
Trang 34none, probability of, 569-570
nongame dynamic searches, 193-202
normal (Gaussian) distribution, 566, 591, 595normalization, 582
normalize( ), 601
not (!) operator, 240
Trang 35NP-complete, NP-hard problems, 184
dates and times, 489
factorials of fractional numbers, 15
Trang 36derivatives and integrals, 627-634
interpolation, extrapolation, curve fitting, 642-648solving equations, 634-642
probability (see probability)
random (see random numbers)
Trang 37odd perfect numbers, 523
OFB (Output Feedback), 543
offset (pattern shift), 357
opening book strategy, 192
operating systems and data access, 527operator precedence (priority), 397optimization (see efficiency of algorithms)optimizing parse trees, 396
order of growth values, 17-20
ordering (see sorting)
Trang 38Parse: :Lex module, 406
Parse: :RecDescent module, 407
partial ordering (graphs), 304
partition( ), 137-140
Trang 40Baeza-Yates-Gonnet shift-add algorithm, 379-382Baeza-Yates-Gonnet shift-OR algorithm, 377longest common subsequence (LCS), 386
regular expressions (String: :Approx), 387
Wu-Manber k-differences algorithm, 382-387phonetic algorithms, 388-389
metaphone algorithm (Text: :Metaphone), 389soundex algorithm (Text: :Soundex), 388-389Rabin-Karp algorithm
binary mask, 368
regular expressions (see regular expressions)
stemming and inflection, 389-393
Lingua: :EN: :Inflect module, 393
Lingua: :PT: :Conjugate module, 393
Text: :German module, 393
Text: :Stem module, 392
manipulating matrix elements, 246
matrix chain products, 269
Trang 42metaphone algorithm (Text: :Metaphone), 389
soundex algorithm (Text: :Soundex), 388-389
pluralizing English words, 393
Point class (example), 26
point_in_quadrangle( ), 448
point_in_triangle( ), 446
points
closest pair of, 457-464
directives at, computing, 628-629
fitting polynomials to, 643
Trang 43approximating roots, 638-640fitting to points, 643
solving for roots, 634-642pop operator, 38, 40
population variance estimation, 614Portuguese verb conjugation, 393position object (game interface), 176positive correlation, 620, 624
positive lookahead assertion, 356POSIX module, 473-474
postorder vertex processing, 314pow( ), 479
Trang 44Pretty Good Privacy (PGP), 565
prev( ) (double package), 66
Trang 46permutations and combinations, 571-574random numbers, 567-568
(see also statistics)
productions (of tokens), 396
profiling, 38
programs
passwords for (see passwords)
in this book, obtaining, xv
progressions of numbers, 492-498
proper string prefixes/suffixes, 357
proportional_binary_string_search( ), 166proportional search, 165-167
Trang 48El Gamal key, generating, 552
generating prime numbers, 549
generator seed, 567-568
improving randomness, 568
(see also probability distributions)
random values (prepending to passwords), 532
random variables, 574
randomized running time, 140
randomizing the pivot choice, 140
rand( ) function, 540break
Trang 49tracking both ends, 52-55
reformulating the problem, 22
regression line, 622
regular expressions, 354-357
Trang 50positive/negative lookahead assertions, 356repetition quantifiers, 356
tips and tricks, 355
removing (see deleting)
rendering graphs (see graphs, displaying)
Trang 52RSA public key encryption, 549-552run-length encoding (RLE), 228, 412-415rvals( ), 253
S
s/// substitution operator, 357, 399saddle( ), 642
graph traversal, 309, 314
efficiency, 157, 172-175
external searches, 174
generative searching, 158, 175-202
Trang 53exhaustive search, 180-185
game interface for, 176-180
killer move strategy, 189, 202
linked list contents, 56
for list maxima/minima, 122-125
lookup searches 158-175
binary search in list, 2-5, 162-165, 389
binary search in tree, 167-171
security (see cryptography)
seed for random number generation, 567-568
seek( ), 8
Trang 54CPAN modules for, 227-232
Bit: :Vector module, 227, 230
Set: :Bag module, 241
Set: :IntegerFast module, 231
Set: :IntRange module, 227, 232
Trang 55Set: :IntSpan module, 227-228, 242
Set: :Object module, 227-228
Trang 59queue order (FIFO), 38
radix sorts, 145-147
in reverse (see reverse order)
Schwartzian Transform, 109-113
Sort: :ArbBiLex module, 114
Sort: :Fields module, 106
Sort: :Versions module, 106
stability and sensitivity, 119
stack order (LIFO), 39
topological sort, 304-307, 309
sos_as_string( ), 234
soundex algorithm, 388-389
source code, encrypted, 562-564
source vertex (flow network), 279, 345source_vertices( ), 297
space (see disk space)
space characters, hiding messages with, 557space in regular expressions, 355
spaceship (<=>) operator, 105
Spafford, Eugene, 536
sparse graphs, 286
sparse sets, 229
speaking secret messages, 555
speed (see efficiency of algorithms)
Trang 61fitting lines to data, 622-624
general references on, 651
Lingua: :EN: :Inflect module, 393
Lingua: :PT: :Conjugate module, 393
Text: :German module, 393
Text: :Stem module, 392
straight radix sort, 145break
Page 681
street addresses, data structures for, 27, 31
Trang 62string( ), 399-400
String: :Approx module, 387
String: :ShellQuote module, 410
run-length encoding (RLE), 412-415
converting data into, 299
general references on, 650
matching algorithms, 353-424
approximate (fuzzy) matching, 353, 377, 379, 387Baeza-Yates-Gonnet shift-OR algorithm, 377-379Boyer-Moore algorithm, 373-375
Boyer-Moore-Horspool algorithm, 375
exact matching, 354
Knuth-Morris-Pratt algorithm, 370-372, 395nạve matching, 357-362
Trang 63sorting (see sorting)
subroutines (see algorithms)
subscripts, array and hash elements, 26, 29subsets, 223
Trang 64tabs, hiding messages in, 557
tail pointer (linked lists), 52
text (see strings)
Text: :Abbrev module, 408
Text: :Balanced, 410
Text: :DelimMatch module, 409
Text: :German module, 393
Text: :Metaphone module, 389
Text: :ParseWords module, 409
Text: :Soundex module, 388-389Text: :Stem module, 392
Trang 65three-dimensional modeling, 466break
Time: :CTime module, 490
Time: :Date module, 490
Time: :DaysInMonth module, 490
Time: :JulianDay module, 490
time measurement (uniform distributions), 578
Time: :ParseDate module, 490
time resources, 9-11
Time: :Timezone module, 490
times and dates, 489
timestamp, hiding messages in, 557
Trang 66top-down programming strategy, 22
topological sort, 304-307, 309
total degree, 280
total sum (Rabin-Karp) 365
trailing spaces, hiding messages with, 557transformation matrices, 257
transitive closure, 342-344
TransitiveClosure_Floyd_Warshall( ), 344translation, 405
Trang 67true hits (string matching), 357
TSP (Traveling Salesman problem), 184, 350t-test, 614-616
underscores and numeric comparison, 107
undirected graphs and edges, 277-279, 291-293, 317Unicode standard, 107, 651
Trang 68bit vectors for, 216
use constant statement, 48
use integer statement, 14, 470
use locale statement, 106
''Useless use of in void context" warning, 13
usernames and matching passwords, 528
/usr/dict/words file, 6-8
V
validate( ) (Heap modules), 101
validating data (checksums), 534-537
validation keys (winnowing and chaffing), 558
Trang 69verifying passwords (Perl program), 528
versioncmp( ) (Sort: :Versions), 107
versions( ) (Sort: :Versions), 107
versions of Perl, xiv
leaves and branches, 312
parents, children, ancestors, descendants, 315predecessor, successor, neighbor, 315
sink and source vertices, 345
vertex sets, 326
weight/cost, 329
(see also graphs)
very big/small numbers, 477-480, 574
Trang 70weight, graph edges, 286, 334
weight, graph vertices, 329
Trang 71/x regular expression modifier, 355
XOR (binary exclusive or) operator, 220-221, 538
xor (exclusive logical or) operator, 210
Trang 72About the Authors
Jon Orwant is president of Readable Publications, Inc He founded The Perl Journal in 1995
and served as the sole editor, publisher, accountant, designer, and postal antagonizer until
1999 He has been on the technical committee of all of O'Reilly' s Perl conferences (where he
is the emcee of the Internet Quiz Show), and he speaks frequently about Perl, most recently atthe first YAPC on Rebuilding Post-Apocalyptic Civilization with Perl
He is currently an MIT Media Laboratory IBM Fellow, creating programs that create programsthat play games His other research interests are user modeling and electronic publishing Hegives frequent talks about Media Lab research, most recently on the promise and perils ofInternet gambling
In 1993, he created the world's first Internet stock-picking game His Markovmodel basedRock-Paper-Scissors program has been undefeated since 1997 He also performs professionalvoice-overs A court injunction from the Commonwealth of Massachusetts prohibits him fromcooking or otherwise assisting in the preparation of any foodstuff meant for human
consumption
Jarkko Hietaniemi is the creator and Master Librarian of CPAN: Comprehensive Perl
Archive Network He has also been known to frequent Perl developer forums Luckily enough,getting his M.S in Computer Science in the field of parallel computing didn't interfere overlymuch with his Perl and Unix hacking During those savored moments of offline time, he fanciesgobbling up speculative fiction and popular science His real life employer is Nokia ResearchCenter
John Macdonald has been using Perl commercially since 1988 for a suite of Unix system
administration tools His background with Unix dates back to the days when Unix was written
in PDP-11 assembler and later includes representing the University of Waterloo at the firstUnix Users Meeting at City University of New York in the mid-1970s while finishing his M.Math degree (In those days before the creation of Usenix, the people at the meeting would sittogether around a single table.) In addition, his background includes work on compilers, kernelinternals, device drivers, and the like He has also been observed partaking in recreationalcomputing activities
Colophon
The animal on the cover of Mastering Algorithms with Perl is a wolf (Canis lupus) The wolf,
a member of the dog family, is one of the most sociable of mammals Wolves live and hunt inpacks, which consist of one pair of alpha male and female wolves and several offspring Packsize is usually five to nine wolves Each wolfcontinue
Page 686
pack has its own territory, which they will mark and fight to defend Wolves often travel long