Preface
This book provides a comprehensive introduction to the modern study of computer algorithms. It presents many algorithms and covers them in considerable depth, yet makes their design and analysis accessible to all levels of readers. We have tried t
Contents
xi
B
Sets, Etc. 1070 B.1 Sets 1070 B.2 Relations 1075 B.3 Functions 1077 B.4 Graphs 1080 B.5 Trees 1085 Counting and Probability 1094 C.1 Counting 1094 C.2 Probability 1100 C.3 Discrete random variables 1106 C.4 The geometric and binomial distrib
viii
Contents
V Advanced Data Structures
Introduction 18 431 B-Trees 434 18.1 Denition of B-trees 438 18.2 Basic operations on B-trees 441 18.3 Deleting a key from a B-tree 449 Binomial Heaps 455 19.1 Binomial trees and binomial heaps 457 19.2 Operations
Contents
vii
12
Binary Search Trees 253
12.1 What is a binary search tree? 253
12.2 Querying a binary search tree 256
12.3 Insertion and deletion 261
12.4 Randomly built binary search trees 265
13
Red-Black Trees 273
13.1 Properties of red-black trees
13.
vi
Contents
II Sorting and Order Statistics
Introduction 6 123
Heapsort 127 6.1 Heaps 127 6.2 Maintaining the heap property 6.3 Building a heap 132 6.4 The heapsort algorithm 135 6.5 Priority queues 138
130
7
Quicksort 145 7.1 Description of quicksort 145
Contents
Preface
xiii
I Foundations
Introduction 1 3 The Role of Algorithms in Computing 5 1.1 Algorithms 5 1.2 Algorithms as a technology 10 Getting Started 15 2.1 Insertion sort 15 2.2 Analyzing algorithms 21 2.3 Designing algorithms 27 Growth of Functi
Preface
xv
To the professional The wide range of topics in this book makes it an excellent handbook on algorithms. Because each chapter is relatively self-contained, you can focus in on the topics that most interest you. Most of the algorithms we discuss
xiv
Preface
section boundaries marking natural stopping points. In an undergraduate course, you might use only the earlier sections from a chapter; in a graduate course, you might cover the entire chapter. We have included over 920 exercises and over 140
xvi
Preface
•
Cliff Stein was added as a coauthor.
•
Errors have been corrected. How many errors? Let’s just say several.
•
There are three new chapters:
•
•
•
•
Chapter 1 discusses the role of algorithms in computing.
Chapter 5 covers probabilistic analy
CSCE 629 Homework 3
June 16, 2010
Homework 3 is due Wednesday, June 23 in class. Read chapters 15-17. Turn in 1. Problem 15-1, page 404. Suppose that we are given a directed acyclic graph G = (V,E) with real valued edge weights and two distinguished verti
CSCE 629 Homework 2
June 9, 2010
Homework 2 is due Wednesday, June 16 in class.
Read chapters 13-14. Turn in 1. Exercise 13.3-5, page 322 (Consider a red-black tree formed by inserting n nodes with RB-INSERT. Argue that is n is greater than 1, the tree ha
Preface
xix
Bob Tarjan, and Paul Wang. Several of our colleagues also graciously supplied
us with problems; we particularly thank Andrew Goldberg, Danny Sleator, and
Umesh Vazirani.
It has been a pleasure working with The MIT Press and McGraw-Hill in the
xviii
Preface
Acknowledgments for the rst edition Many friends and colleagues have contributed greatly to the quality of this book. We thank all of you for your help and constructive criticisms. MITs Laboratory for Computer Science has provided an ideal w
Preface
xvii
The partitioning method used for quicksort (Section 7.1) and the expected linear-time order-statistic algorithm (Section 9.2) is different. We now use the method developed by Lomuto, which, along with indicator random variables, allows for a
Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein
Introduction to Algorithms
Second Edition
The MIT Press Cambridge, Massachusetts
London, England Dubuque, IA St. Louis Montr al e Madison, WI Toronto
McGraw-Hill Book Company Boston Bur
1.1 Algorithms
7
Electronic commerce enables goods and services to be negotiated and exchanged electronically. The ability to keep information such as credit card numbers, passwords, and bank statements private is essential if electronic commerce is to be
6
Chapter 1 The Role of Algorithms in Computing
have been developed. Which algorithm is best for a given application depends onamong other factorsthe number of items to be sorted, the extent to which the items are already somewhat sorted, possible restric
1
The Role of Algorithms in Computing
What are algorithms? Why is the study of algorithms worthwhile? What is the role of algorithms relative to other technologies used in computers? In this chapter, we will answer these questions.
1.1 Algorithms
Informal
4
Part I Foundations
Chapter 4 delves further into the divide-and-conquer method introduced in Chapter 2. In particular, Chapter 4 contains methods for solving recurrences, which are useful for describing the running times of recursive algorithms. One pow
Introduction
This part will get you started in thinking about designing and analyzing algorithms. It is intended to be a gentle introduction to how we specify algorithms, some of the design strategies we will use throughout this book, and many of the fund