O'Reilly logo

Algorithms in a Nutshell by Gary Pollice, Stanley Selkow, George T. Heineman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Preface

As Trinity states in the movie The Matrix:

It's the question that drives us, Neo. It's the question that brought you here.

You know the question, just as I did.

As authors of this book, we answer the question that has led you here:

Can I use algorithm X to solve my problem? If so, how do I implement it?

You likely do not need to understand the reasons why an algorithm is correct—if you do, turn to other sources, such as the 1,180-page bible on algorithms, Introduction to Algorithms, Second Edition, by Thomas H. Cormen et al. (2001). There you will find lemmas, theorems, and proofs; you will find exercises and step-by-step examples showing the algorithms as they perform. Perhaps surprisingly, however, you will not find any real code, only fragments of "pseudocode," the device used by countless educational textbooks to present a high-level description of algorithms. These educational textbooks are important within the classroom, yet they fail the software practitioner because they assume it will be straightforward to develop real code from pseudocode fragments.

We intend this book to be used frequently by experienced programmers looking for appropriate solutions to their problems. Here you will find solutions to the problems you must overcome as a programmer every day. You will learn what decisions lead to an improved performance of key algorithms that are essential for the success of your software applications. You will find real code that can be adapted to your needs and solution methods that you can learn.

All algorithms are fully implemented with test suites that validate the correct implementation of the algorithms. The code is fully documented and available as a code repository addendum to this book. We rigorously followed a set of principles as we designed, implemented, and wrote this book. If these principles are meaningful to you, then you will find this book useful.

Principle: Use Real Code, Not Pseudocode

What is a practitioner to do with Figure P-1's description of the Ford-Fulkerson algorithm for computing maximum network flow?

Example of pseudocode commonly found in textbooks

Figure 1. Example of pseudocode commonly found in textbooks

The algorithm description in this figure comes from Wikipedia (http://en.wikipedia.org/wiki/Ford_Fulkerson), and it is nearly identical to the pseudocode found in (Cormen et al., 2001). It is simply unreasonable to expect a software practitioner to produce working code from the description of Ford-Fulkerson shown here! Turn to Chapter 8 to see our code listing by comparison. We use only documented, well-designed code to describe the algorithms. Use the code we provide as-is, or include its logic in your own programming language and software system.

Some algorithm textbooks do have full real-code solutions in C or Java. Often the purpose of these textbooks is to either teach the language to a beginner or to explain how to implement abstract data types. Additionally, to include code listings within the narrow confines of a textbook page, authors routinely omit documentation and error handling, or use shortcuts never used in practice. We believe programmers can learn much from documented, well-designed code, which is why we dedicated so much effort to develop actual solutions for our algorithms.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required