100% found this document useful (2 votes)
65 views

Algorithms Sequential Parallel A Unified Approach Electrical and Computer Engineering Series 2nd Edition Russ Miller download pdf

The document provides information about the book 'Algorithms Sequential Parallel: A Unified Approach' by Russ Miller and Laurence Boxer, including details on how to download it and other related ebooks. It outlines the contents of the book, which covers various topics in algorithms, including asymptotic analysis, recursion, and parallel computing. Additionally, it includes a limited warranty and disclaimer of liability regarding the book's content and performance.

Uploaded by

ntulagudden
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
65 views

Algorithms Sequential Parallel A Unified Approach Electrical and Computer Engineering Series 2nd Edition Russ Miller download pdf

The document provides information about the book 'Algorithms Sequential Parallel: A Unified Approach' by Russ Miller and Laurence Boxer, including details on how to download it and other related ebooks. It outlines the contents of the book, which covers various topics in algorithms, including asymptotic analysis, recursion, and parallel computing. Additionally, it includes a limited warranty and disclaimer of liability regarding the book's content and performance.

Uploaded by

ntulagudden
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Visit https://ebookultra.

com to download the full version and


explore more ebooks

Algorithms Sequential Parallel A Unified


Approach Electrical and Computer Engineering
Series 2nd Edition Russ Miller

_____ Click the link below to download _____


https://ebookultra.com/download/algorithms-sequential-
parallel-a-unified-approach-electrical-and-computer-
engineering-series-2nd-edition-russ-miller/

Explore and download more ebooks at ebookultra.com


Here are some suggested products you might be interested in.
Click the link to download

Design and Analysis of Distributed Algorithms Wiley Series


on Parallel and Distributed Computing 1st Edition Nicola
Santoro
https://ebookultra.com/download/design-and-analysis-of-distributed-
algorithms-wiley-series-on-parallel-and-distributed-computing-1st-
edition-nicola-santoro/

Parallel Metaheuristics A New Class of Algorithms 1st


Edition Enrique Alba

https://ebookultra.com/download/parallel-metaheuristics-a-new-class-
of-algorithms-1st-edition-enrique-alba/

Basic electrical and electronics engineering 2nd Edition


R.K. Rajput

https://ebookultra.com/download/basic-electrical-and-electronics-
engineering-2nd-edition-r-k-rajput/

Engineering Thermodynamics A Computer Approach SI Units


Version Third Edition R. K. Rajput

https://ebookultra.com/download/engineering-thermodynamics-a-computer-
approach-si-units-version-third-edition-r-k-rajput/
Evolutionary computation a unified approach 1st Edition
Kenneth A. De Jong

https://ebookultra.com/download/evolutionary-computation-a-unified-
approach-1st-edition-kenneth-a-de-jong/

Fundamentals of Computer Organization and Architecture


Wiley Series on Parallel and Distributed Computing 1st
Edition Mostafa Abd-El-Barr
https://ebookultra.com/download/fundamentals-of-computer-organization-
and-architecture-wiley-series-on-parallel-and-distributed-
computing-1st-edition-mostafa-abd-el-barr/

Mechatronics Engineering and Electrical Engineering 1st


Edition Ai Sheng

https://ebookultra.com/download/mechatronics-engineering-and-
electrical-engineering-1st-edition-ai-sheng/

Partial Differential Equations A Unified Hilbert Space


Approach 1st Edition Rainer Picard

https://ebookultra.com/download/partial-differential-equations-a-
unified-hilbert-space-approach-1st-edition-rainer-picard/

Latent Variable Models and Factor Analysis A Unified


Approach 3rd Edition David J. Bartholomew

https://ebookultra.com/download/latent-variable-models-and-factor-
analysis-a-unified-approach-3rd-edition-david-j-bartholomew/
Algorithms Sequential Parallel A Unified Approach
Electrical and Computer Engineering Series 2nd Edition
Russ Miller Digital Instant Download
Author(s): Russ Miller, Laurence Boxer
ISBN(s): 1584504129
Edition: 2
File Details: PDF, 1.95 MB
Year: 2005
Language: english
Algorithms
Sequential and Parallel
LIMITED WARRANTY AND DISCLAIMER OF LIABILITY

CHARLES RIVER MEDIA, INC. (“CRM”) AND/OR ANYONE WHO HAS BEEN IN-
VOLVED IN THE WRITING, CREATION OR PRODUCTION OF THE ACCOMPANY-
ING CODE IN THE TEXTUAL MATERIAL IN THE BOOK, CANNOT AND DO NOT
WARRANT THE PERFORMANCE OR RESULTS THAT MAY BE OBTAINED BY USING
THE CONTENTS OF THE BOOK. THE AUTHOR AND PUBLISHER HAVE USED
THEIR BEST EFFORTS TO ENSURE THE ACCURACY AND FUNCTIONALITY OF
THE TEXTUAL MATERIAL AND PROGRAMS DESCRIBED HEREIN. WE HOWEVER,
MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, REGARDING THE
PERFORMANCE OF THESE PROGRAMS OR CONTENTS. THE BOOK IS SOLD “AS
IS” WITHOUT WARRANTY (EXCEPT FOR DEFECTIVE MATERIALS USED IN MAN-
UFACTURING THE BOOK OR DUE TO FAULTY WORKMANSHIP).

THE AUTHOR, THE PUBLISHER, AND ANYONE INVOLVED IN THE PRODUCTION


AND MANUFACTURING OF THIS WORK SHALL NOT BE LIABLE FOR DAMAGES
OF ANY KIND ARISING OUT OF THE USE OF (OR THE INABILITY TO USE) THE
PROGRAMS, SOURCE CODE, OR TEXTUAL MATERIAL CONTAINED IN THIS PUB-
LICATION. THIS INCLUDES, BUT IS NOT LIMITED TO, LOSS OF REVENUE OR
PROFIT, OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THE PRODUCT.

THE SOLE REMEDY IN THE EVENT OF A CLAIM OF ANY KIND IS EXPRESSLY LIM-
ITED TO REPLACEMENT OF THE BOOK, AND ONLY AT THE DISCRETION OF CRM.

THE USE OF “IMPLIED WARRANTY” AND CERTAIN “EXCLUSIONS” VARIES FROM


STATE TO STATE, AND MAY NOT APPLY TO THE PURCHASER OF THIS PRODUCT.
Algorithms
Sequential and Parallel
A Unified Approach
Second Edition

Russ Miller
Laurence Boxer

CHARLES RIVER MEDIA, INC.


Hingham, Massachusetts
Copyright 2005 by CHARLES RIVER MEDIA, INC.
All rights reserved.

The first edition of this book was previously published by: Pearson Education, Inc.

No part of this publication may be reproduced in any way, stored in a retrieval system of any type, or
transmitted by any means or media, electronic or mechanical, including, but not limited to, photocopy,
recording, or scanning, without prior permission in writing from the publisher.

Editor: David Pallai


Cover Design: Tyler Creative

CHARLES RIVER MEDIA, INC.


10 Downer Avenue
Hingham, Massachusetts 02043
781-740-0400
781-740-8816 (FAX)
info@charlesriver.com
www.charlesriver.com

This book is printed on acid-free paper.

Russ Miller and Laurence Boxer. Algorithms Sequential and Parallel: A Unified Approach, Second Edition.
ISBN: 1-58450-412-9
eISBN: 1-58450-652-0
All brand names and product names mentioned in this book are trademarks or service marks of their re-
spective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be
regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks
used by companies, manufacturers, and developers as a means to distinguish their products.

Library of Congress Cataloging-in-Publication Data


Miller, Russ.
Algorithms sequential and parallel : a unified approach / Russ Miller and Laurence Boxer.-- 2nd ed.
p. cm.
Includes bibliographical references and index.
ISBN 1-58450-412-9 (hardcover : alk. paper)
1. Computer algorithms. 2. Computer programming. I. Boxer, Laurence. II. Title.
QA76.9.A43M55 2005
005.1--dc22
2005010052

05 7 6 5 4 3 2 1

CHARLES RIVER MEDIA titles are available for site license or bulk purchase by institutions, user
groups, corporations, etc. For additional information, please contact the Special Sales Department at
781-740-0400.
To my wife, Celeste, and my children, Brian, Amanda,
and Melissa.

—Russ Miller

To my wife, Linda; my daughter and son-in-law, Robin


and Mark Waldman; and my son, Matthew.

—Laurence Boxer
This page intentionally left blank
Contents

Preface xv

1 Asymptotic Analysis 2
Notation and Terminology 4
Asymptotic Notation 6
More Notation 9
Asymptotic Relationships 11
Asymptotic Analysis and Limits 12
Summations and Integrals 14
Rules for Analysis of Algorithms 21
Limitations of Asymptotic Analysis 27
Common Terminology 29
Summary 29
Chapter Notes 30
Exercises 30

2 Induction and Recursion 34


Mathematical Induction 36
Induction Examples 37
Recursion 40
Binary Search 43
Merging and Mergesort 47
Summary 54
Chapter Notes 54
Exercises 54

vii
viii Contents

3 The Master Method 58


Master Theorem 61
Proof of the Master Theorem (optional) 61
The General Case 66
Summary 73
Chapter Notes 73
Exercises 73

4 Combinational Circuits 74
Combinational Circuits and Sorting Networks 76
Sorting Networks 76
Bitonic Merge 80
BitonicSort 84
Summary 87
Chapter Notes 88
Exercises 88

5 Models of Computation 90
RAM (Random Access Machine) 92
PRAM (Parallel Random Access Machine) 94
Examples: Simple Algorithms 98
Fundamental Terminology 106
Distributed Memory versus Shared Memory 107
Distributed Address Space versus Shared Address Space 108
Interconnection Networks 108
Processor Organizations 109
Linear Array 110
Ring 118
Mesh 119
Tree 123
Pyramid 125
Mesh-of-trees 127
Hypercube 131
Contents ix

Coarse-Grained Parallel Computers 136


Additional Terminology 139
Summary 142
Chapter Notes 142
Exercises 143

6 Matrix Operations 146


Matrix Multiplication 148
Gaussian Elimination 153
Roundoff Error 160
Summary 161
Chapter Notes 161
Exercises 161

7 Parallel Prefix 164


Parallel Prefix 166
Parallel Algorithms 167
Parallel Prefix on the PRAM 167
Mesh 171
Hypercube 174
Analysis 176
Coarse-Grained Multicomputer 176
Application: Maximum Sum Subsequence 176
RAM 176
PRAM 177
Mesh 179
Array Packing 179
RAM 180
PRAM 181
Network Models 181
Interval (Segment) Broadcasting 182
Solution Strategy 182
Analysis 183
x Contents

(Simple) Point Domination Query 183


RAM 185
PRAM and Network Models 185
Computing Overlapping Line Segments 185
RAM 186
PRAM 187
Mesh 188
Maximal Overlapping Point 188
Analysis 188
Summary 189
Chapter Notes 189
Exercises 189

8 Pointer Jumping 192


List Ranking 194
Linked List Parallel Prefix 196
Summary 197
Chapter Notes 198
Exercises 198

9 Divide-and-Conquer 200
MergeSort (Revisited) 202
RAM 202
Linear Array 203
Selection 205
RAM 206
Analysis of Running Time 209
Parallel Machines 210
QuickSort (Partition Sort) 211
Array Implementation 216
Analysis of QuickSort 221
Expected-Case Analysis of QuickSort 223
Improving QuickSort 226
Contents xi

Modifications of QuickSort for Parallel Models 228


HyperQuickSort 228
BitonicSort (Revisited) 229
BitonicSort on a Mesh 230
Sorting Data with Respect to Other Orderings 234
Concurrent Read/Write 235
Implementation of a Concurrent Read 236
Implementation of Concurrent Write (overview) 237
Concurrent Read/Write on a Mesh 238
Summary 238
Chapter Notes 238
Exercises 239

10 Computational Geometry 242


Convex Hull 244
Graham’s Scan 246
Jarvis’ March 250
Divide-and-Conquer Solution 251
Smallest Enclosing Box 260
RAM 261
PRAM 261
Mesh 261
All-Nearest Neighbor Problem 262
Running Time 264
Architecture-Independent Algorithm Development 264
Line Intersection Problems 265
Overlapping Line Segments 266
Summary 270
Chapter Notes 270
Exercises 272

11 Image Processing 276


Preliminaries 278
xii Contents

Component Labeling 280


RAM 280
Mesh 281
Convex Hull 285
Running Time 287
Distance Problems 288
All-Nearest Neighbor between Labeled Sets 288
Running Time 289
Minimum Internal Distance within Connected Components 290
Hausdorff Metric for Digital Images 293
Summary 296
Chapter Notes 296
Exercises 297

12 Graph Algorithms 300


Terminology 303
Representations 306
Adjacency Lists 307
Adjacency Matrix 308
Unordered Edges 309
Fundamental Algorithms 309
Breadth-First Search 309
Depth-First Search 313
Discussion of Depth-First and Breadth-First Search 315
Fundamental PRAM Graph Techniques 316
List Ranking via Pointer Jumping 316
Euler Tour Technique 318
Tree Contraction 318
Computing the Transitive Closure of an Adjacency Matrix 323
Connected Component Labeling 325
RAM 325
PRAM 325
Mesh 330
Contents xiii

Minimum-Cost Spanning Trees 330


RAM 330
PRAM 334
Mesh 336
Shortest-Path Problems 339
RAM 339
PRAM and Mesh 342
Summary 343
Chapter Notes 344
Exercises 345

13 Numerical Problems 350


Primality 352
Greatest Common Divisor 354
Lamé’s Theorem 355
Integral Powers 355
Evaluating a Polynomial 357
Approximation by Taylor Series 359
Trapezoidal Integration 362
Summary 365
Chapter Notes 365
Exercises 366

Bibliography 368

Index 373
This page intentionally left blank
Preface

major thrust of computer science is the design, analysis, implementation, and


A scientific evaluation of algorithms to solve critical problems. In addition, new
challenges are being offered in the field of computational science and engineering,
an emerging discipline that unites computer science and mathematics with disci-
plinary expertise in biology, chemistry, physics, and other applied scientific and
engineering fields. Computational science and engineering is often referred to as
the “third science,” complementing both theoretical and laboratory science. These
multidisciplinary efforts typically require efficient algorithms that run on high-
performance (typically parallel) computers in order to generate the necessary com-
puter models and simulations.
With advances in computational science and engineering, parallel computing
continues to merge into the mainstream of computing. It is therefore critical that
students and scientists understand the application and analysis of algorithmic par-
adigms to both the (traditional) sequential model of computing and to a variety of
parallel models.
Many computer science departments offer courses in “Analysis of Algo-
rithms,” “Algorithms,” “An Introduction to Algorithms,” or “Data Structures and
Their Algorithms” at the junior or senior level. In addition, a course in “Analysis
of Algorithms” is required of most graduate students pursuing a degree in com-
puter science. Throughout the 1980s, the vast majority of these course offerings
focused on algorithms for sequential (von Neumann) computers. In fact, not until
the late-1980s did courses covering an introduction to parallel algorithms begin to
appear in research-oriented departments. Furthermore, these courses in parallel
algorithms were typically presented to advanced graduate students. However, by
the early 1990s, courses in parallel computing began to emerge at the undergradu-
ate level, especially at progressive four-year colleges.
It is interesting to note that throughout much of the 1990s, traditional algo-
rithms-based courses changed very little. Gradually, such courses began to incor-
porate a component of parallel algorithms, typically one to three weeks near the
end of the semester. During the later part of the 1990s, however, it was not uncom-
mon to find algorithms courses that contained as much as 1/3 of the material
devoted to parallel algorithms.
In this book, we take a very different approach to an algorithms-based course.
Parallel computing has moved into the mainstream, with clusters of commodity-
off-the-shelf (COTS) machines dominating the list of top supercomputers in the

xv
xvi Preface

world (www.top500.org), and smaller versions of such machines being exploited in


many research laboratories. Therefore, the time is right to teach a fundamental
course in algorithms that covers paradigms for both sequential and parallel models.
The approach we take in this book is to integrate the presentation of sequential
and parallel algorithms. Specifically, we employ a philosophy of presenting a par-
adigm, such as divide-and-conquer, and then discussing implementation issues for
both sequential and parallel models. Due to the fact that we present design and
analysis of paradigms for sequential and parallel models, the reader might notice
that the number of paradigms we can treat within a semester is limited when com-
pared to a traditional sequential algorithms text.
This book has been used successfully at a wide variety of colleges and
universities.
Prerequisites: We assume a basic knowledge of data structures and mathemati-
cal maturity. The reader should be comfortable with notions of a stack, queue, list,
and binary tree, at a level that is typically taught in a CS2 course. The reader should
also be familiar with fundamentals of discrete mathematics and Calculus. Specifi-
cally, the reader should be comfortable with limits, summations, and integrals.

Overview of Chapters
Background material for the course is presented in Chapters 1, 2, and 3. Chapter 1
introduces the concept of asymptotic analysis. While the reader might have seen
some of this material in a course on data structures, we present this material in a
fair amount of detail. The reader who is uncomfortable with some of the funda-
mental material from a Freshman-level Calculus sequence might want to brush up
on notions such as limits, summations and integrals, and derivatives, as they natu-
rally arise in the presentation and application of asymptotic analysis. Chapter 2
focuses on fundamentals of induction and recursion. While many students have
seen this material in previous courses in computer science and/or mathematics, we
have found it important to review this material briefly and to provide the students
with a reference for performing the necessary review. In Chapter 3, we present the
Master Method, a very useful cookbook-type of system for evaluating recurrence
equations that are common in an algorithms-based setting.
Chapter 4 presents an overview of combinational circuits and sorting net-
works. This work is used to motivate the natural use of parallel models and to
demonstrate the blending of architectural and algorithmic approaches. In Chapter
5, we introduce fundamental models of computation, including the RAM (a formal
sequential architecture) and a variety of parallel models of computation. The par-
allel models introduced include the PRAM, mesh, hypercube, and the Coarse-
Grained Multicomputer, to name a few. In addition, Chapter 5 introduces
terminology such as shared-memory and distributed-memory.
Preface xvii

The focus of Chapter 6 is the important problem of matrix multiplication,


which is considered for a variety of models of computation. In Chapter 7, we
introduce the parallel prefix operation. This is a very powerful operation with a
wide variety of applications. We discuss implementations and analysis for a num-
ber of the models presented in Chapter 5 and give sample applications. In Chapter
8, we introduce pointer jumping techniques and show how some list-based algo-
rithms can be efficiently implemented in parallel.
In Chapter 9, we introduce the powerful divide-and-conquer paradigm. We
discuss applications of divide-and-conquer to problems involving data movement,
including sorting, concurrent reads/writes, and so forth. Algorithms and their
analysis are presented for a variety of models.
Chapters 10 and 11 focus on two important application areas, namely, Compu-
tational Geometry and Image Processing. In these chapters, we focus on interest-
ing problems chosen from these important domains as a way of solidifying the
approach of this book in terms of developing machine independent solution strate-
gies, which can then be tailored for specific models, as required.
Chapter 12 focuses on fundamental graph theoretic problems. Initially, we
present standard traversal techniques, including breadth-first search, depth-first
search, and pointer jumping. We then discuss fundamental problems, including
tree contraction and transitive closure. Finally, we couple these techniques with
greedy algorithms to solve problems, such as labeling the connected components
of a graph, determining a minimal spanning forest of a graph, and problems
involving shortest or minimal-weight paths in a graph.
Chapter 13 is an optional chapter concerned with some fundamental numeri-
cal problems. The focus of the chapter is on sequential algorithms for polynomial
evaluation and approximations of definite integrals.

Recommended Use
This book has been successfully deployed in both elective and required courses,
with students typically ranging from juniors (3rd-year undergraduates) to 2nd-year
graduates. A student in a course using this book need not be advanced in a mathe-
matical sense, but should have a basic, fundamental, background.

Correspondence
Please feel free to contact the authors directly with any comments or criticisms
(constructive or otherwise) of this book. Russ Miller may be reached at
miller@buffalo.edu and Laurence Boxer may be reached at boxer@niagara.edu.
In addition, a Web site for the book can be found from http://www.cse.buffalo.
edu/pub/WWW/faculty/miller/research.htm. This Web site contains information
related to the book, including pointers to education-based pages, relevant parallel
computing links, and errata.
xviii Preface

Acknowledgments
The authors would like to thank several anonymous reviewers for providing
insightful comments, which have been used to improve the presentation of this
book. We would like to thank the students at SUNY-Buffalo who used early drafts
of this book in their classes and provided valuable feedback. We would like to
thank Ken Smith, a member of the technical support staff at SUNY-Buffalo, for
providing assistance with Wintel support. We would also like to thank our families
for providing us the support necessary to complete this time-consuming project.

Russ Miller & Laurence Boxer, 2005


This page intentionally left blank
1
Asymptotic Analysis
Notation and Terminology
Asymptotic Relationships
Rules for Analysis of Algorithms
Limitations of Asymptotic Analysis
Common Terminology
Summary
Chapter Notes
Exercises

2
e live in a digital-data-driven society that relies increasingly on simulation and
W modeling for discovery. Data is increasing at an astonishing rate, typically two
to three times the rate of increase of processing power and network bandwidth. Thus,
to compete in a knowledge-based economy, students must learn to collect, organize,
maintain, analyze, and visualize data efficiently and effectively.
A comprehensive study of algorithms includes the design, analysis, implementa-
tion, and experimental evaluation of algorithms that solve important problems. These
include enabling problems, such as sorting, searching, and transferring data; as well as
applications-oriented problems, such as retrieving a reservation record, forecasting the
weather, or determining the positions of atoms in a molecule to improve rational drug
design.
In this chapter, we introduce some basic tools and techniques that are required to
evaluate effectively both a theoretical and an experimental analysis of algorithms. It is
important to realize that without analysis, it is often difficult to justify the choice of one
algorithm over another or to justify the need for developing a new algorithm. Therefore,
a critical aspect of most advanced data structures or algorithms courses is the develop-
ment of techniques for estimating the resources (running time, disk space, memory, and
so forth) required for a given algorithm. As an aside, we should point out that a course
covering proofs of correctness for algorithms is also critical, because having fast algo-
rithms that produce incorrect results is not desirable. However, for pragmatic reasons,
nontrivial proofs of correctness are not covered in this text.
Throughout this book, we will focus on resources associated with a given algo-
rithm. Specifically, we will be concerned with quantities that include the number of
processors, the size of the memory, and the running time required of an algorithm. A
comparison of such quantities will allow for a reasonable comparison between algo-
rithms, typically resulting in an informed choice of an algorithm for a target application.
For example, such analyses will allow us to make a more informed decision on which
sorting algorithm to use on a sequential machine, given data with certain properties that
are maintained in certain data structures. We should point out that when computing
solutions to numerical problems, one must often consider the quality of the solution.
Although this topic is critical, we believe it is covered in a more comprehensive fashion
in “Numerical Methods” or “Computational Science” courses than is possible in a
course on algorithms. In fact, most of the algorithms we consider in this book can be
viewed as “nonnumerical” in nature.

3
4 Chapter 1 Asymptotic Analysis

In practice, it often turns out that we are more concerned with time than with
memory. This statement may surprise students thinking of relatively small home-
work projects that, once freed of infinite loops, begin printing results almost
immediately. However, many important applications require massive processing
of large data sets, requiring hours or even days of CPU time. Examples of these
applications are found in areas such as molecular modeling, weather forecasting,
image analysis, neural network training, and simulation. Aside from the dollar cost
of computer time, human impatience or serious deadlines can limit the use of such
applications. For example, it helps to have a weather forecast only if it is made
available in advance of the forecast period. By contrast, it is not uncommon to be
able to devise algorithms and their associated data structures such that the memory
requirements are quite reasonable, often no more than a small multiple of the size
of the data set being processed.
In this chapter, we develop mathematical tools for the analysis of resources
required by a computer algorithm. Because time is more often the subject of our
analysis than memory, we will use time-related terminology; however, the same
tools can naturally be applied to the analysis of memory requirements or error
tolerance.

Notation and Terminology


In this section, we introduce some notation and terminology that will be used
throughout the text. We make every effort to adhere to traditional notation and
standard terminology. In general, we use the positive integer n to denote the size of
the data set processed by an algorithm. We can process an array of n entries, for
example, or a linked list, tree, or graph of n nodes. We will use T ( n) to represent
the running time of an algorithm operating on a data set of size n.
An algorithm can be implemented on various hardware/software platforms.
We expect that the same algorithm operating on the same data values will execute
faster if implemented in the assembly language of a supercomputer rather than in
an interpreted language on a personal computer (PC) from, say, the 1980s. Thus, it
rarely makes sense to analyze an algorithm in terms of actual CPU time. Rather,
we want our analysis to reflect the intrinsic efficiency of the algorithm without
regard to such factors as the speed of the hardware/software environment in which
the algorithm is to be implemented; we seek to measure the efficiency of our pro-
gramming methods, not their actual implementations.
Thus, the analysis of algorithms generally adheres to the following principles:
Ignore machine-dependent constants: We will not be concerned with
how fast an individual processor executes a machine instruction.
Look at growth of T ( n) as n q h : Even an inefficient algorithm will
often finish its work in an acceptable time when operating on a small
data set. Thus, we are usually interested in T (n), the running time of an
Notation and Terminology 5

algorithm, for large n (recall that n is typically the size of the data input
to the algorithm).
Growth Rate: Because asymptotic analysis implies that we are
interested in the general behavior of the function as the input parameter
gets large (we are interested in the behavior of T (n) as n q h ), this
implies that low-order terms can (and should) be dropped from the
expression. In fact, because we are interested in the growth rate of
the function as n gets large, we should also ignore constant factors
when expressing asymptotic analysis. This is not to say that these terms
are irrelevant in practice, just that they are irrelevant in terms of
considering the growth rate of a function. So, for example, we say that
the function 3n3 + 10n2 + n + 17 grows as n3. Consider another example:
as n gets large, would you prefer to use an algorithm with running time
95n2 + 405n + 1997 or one with a running time of 2n3 + 12? We hope
you chose the former, which has a growth rate of n2, as opposed to the
latter, which has a growth rate of n3. Naturally, though, if n were small,
one would prefer 2n3 + 12 to 95n2 + 405n + 1997. In fact, you should be
able to determine the value of n that is the breakeven point. Figure 1.1
presents an illustration of this situation.

y=g(n)

T(n)

y=f(n)

better

n0 n

FIGURE 1.1 An illustration of the growth rate


of two functions, f(n) and g(n). Notice that for
large values of n, an algorithm with an asymp-
totic running time of f(n) is typically more
desirable than an algorithm with an asymptotic
running time of g(n). In this illustration,
“large” is defined as n v n0.
6 Chapter 1 Asymptotic Analysis

Asymptotic Notation
At this point, we introduce some standard notation that is useful in expressing the
asymptotic behavior of a function. Because we often have a function that we wish
to express (more simply) in terms of another function, it is easiest to introduce this
terminology in terms of two functions. Suppose f and g are positive functions of n.
Then

f(n) = 6(g(n)) (read “f of n is theta of g of n”) if and only if there exist posi-
tive constants c1, c2, and n0 such that c1 g ( n) f f ( n) f c2 g ( n) whenever n v n0.
See Figure 1.2.

y = g(n)

y = f(n)

n0

FIGURE 1.2 An illustration of 6 notation.


f(n) = 6(g(n)) because functions f(n) and
g(n) grow at the same rate for all n v n0.

f (n) = O(g(n)) (read “f of n is oh of g of n”) if and only if there exist posi-


tive constants c and n0 such that f (n) f cg(n) whenever n v n0. See Figure
1.3.

f (n) = <(g(n)) (read “f of n is omega of g of n”) if and only if there exist


positive constants c and n0 such that cg ( n) f f ( n) whenever n v n0. See
Figure 1.4.

f (n) = o(g(n)) (read “f of n is little oh of g of n”) if and only if for every


positive constant C there is a positive integer n0 such that f(n) < Cg(n)
whenever n v n0. See Figure 1.5.
Notation and Terminology 7

y = g(n)

y = f(n)

y = f(n) y = g(n)

n0 n0

FIGURE 1.3 An illustration of O notation. FIGURE 1.4 An illustration of < notation.


f(n) = O(g(n)) because function f(n) is f(n) = <(g(n)) because function f(n) is
bounded from above by g(n) for all n v n0. bounded from below by g(n) for all n v n0.

y=g(n)

y=Cg(n)

y=C'g(n)

y=f(n)
n(C) n(C')

FIGURE 1.5 An illustration of o notation: f(n) = o(g(n)).

f (n) = \(g(n)) (read “f of n is little omega of g of n”) if and only if for every
positive constant C there is a positive integer n0 such that f (n) > Cg(n)
whenever n v n0. See Figure 1.6.
8 Chapter 1 Asymptotic Analysis

y=f(n)

y=C'g(n)

y=Cg(n)

y=g(n)

n(C) n(C')

FIGURE 1.6 An illustration of \ notation: f(n) = \(g(n)).

Strictly speaking, 6, O, <, o, and \ are set-valued functions. Therefore, it


would be appropriate to write (3n2 + 2) ‘ 6(n2). In fact, some authors have tried to
use this membership notation “correctly,” but it has not caught on. In the literature,
it is more common to see this idea expressed as 3n2 + 2 = 6(n2). This notation is
certainly not correct in the mathematical sense; however, it is the standard. The
expression 3n2 + 2 = 6(n2) is read as “3 n squared plus 2 is theta of n squared.”
Note that one does not write 6(n2) = 3n2 + 2.
The set-valued functions 6, O, <, o, and \ are referred to as asymptotic nota-
tion. Recall that we use asymptotic notation to simplify analysis and capture
growth rate. Therefore, we want the simplest and best function as a representative
of each 6, O, <, o, and \ expression. Some examples follow.

EXAMPLE

Given f (t ) = 5 + sin t and g (t ) = 1 , then 5 + sin t = 6(1) because 4 f 5 + sint f


6. (See Figure 1.7.) Note also that f (t ) = O(1) and f (t ) = <(1) , but the best
choice for notation is to write f (t ) = 6(1) because 6 conveys more informa-
tion than either O or <.
Notation and Terminology 9

6
5
4
3
2
1
t
−2π −π π 2π 3π

FIGURE 1.7 Graph of f(t) = 5 + sint.

More Notation
We will often find the floor and ceiling functions useful. Given a real number x,
there is a unique integer n such that

n f x < n + 1.

We say that n is the “floor of x,” denoted

­® x ½¾ = n.

In other words, ­® x ½¾ is the largest integer that is less than or equal to x.


Similarly, given a real number x, there is a unique integer n such that

n < x f n + 1.

Then n + 1 is the “ceiling of x,” denoted

¬­ x ¼½ = n + 1.

In other words, ¬­ x ¼½ is the smallest integer that is greater than or equal to x.


For example, ­®3.2 ½¾ = 3 ; ¬­3.2 ¼½ = 4 ; ­®18 ½¾ = ¬­18 ¼½ = 18
Notice for all real numbers x we have

x  1 < ­® x ½¾ f x f ¬­ x ¼½ < x + 1.

It follows that ­® x ½¾ = 6(x) and ¬­ x ¼½ = 6(x).


Also, in describing the assignment of a value to a variable, we will use either
the equal sign or the left arrow (both are widely used in computer science). That is,
either of the notations
10 Chapter 1 Asymptotic Analysis

left = right

or

left o right

will mean “assign the value of right as the new value of left.”

EXAMPLE

Show that

( )
n

¨k p
= 6 n p+1
k =1

for p > 1, a fixed constant. First, we consider an upper bound on the summation.
We know that
n

¨k p
f n × np
k =1

because the summation contains n terms, the largest of which is n p. Therefore,


we know that

( )
n

¨k p
= O n p+1 .
k =1

Next, we consider a lower bound on the sum. Notice that it is easy to derive a
trivial lower bound of <(n), because there are n terms in the summation, the
least of which is equal to 1. However, we can derive a more useful, larger, lower
bound. Notice that
n ­® n / 2 ½¾ n n

¨k p = ¨ kp + ¨ kp v ¨ k p.
k =1 k =1 k =­® n / 2 ½¾+1 k = ­® n / 2 ½¾+1

Notice that in
n

¨ kp
k =­® n /2 ½¾+1
Asymptotic Relationships 11

( )
p
there are n  ­® n / 2 ½¾ terms, where ­® n / 2 ½¾ + 1 is the smallest term. Therefore,
we know that
n
n p+1
¨k p
v ( n / 2)( n / 2) p = .
k =1 2 p+1

Because 2 p+1 is a constant, we have

( )
n

¨k p
= < n p+1 .
k =1

Therefore, we know that

( )
n

¨k p
= 6 n p+1 .
k =1

Asymptotic Relationships
Useful relationships exist among 6, O, <, o, and \, some of which are given in
the following proposition. The reader might want to try to prove some of these.
(An instructor might want to assign some of these as homework problems.)

Proposition: Let f and g be positive functions of n. Then


( )
1. f ( n) = O g ( n) ž g ( n) = < f ( n) ( )
2. f ( n) = 6 ( g ( n) ) ž g ( n) = 6 ( f ( n) )

3. f ( n) = 6 ( g ( n) ) ž [ f ( n) = O ( g ( n) ) and f ( n) = < ( g ( n) )]

4. f ( n) = o ( g ( n) ) ž g ( n) = \ ( f ( n) )

( )
5. f ( n) = o g ( n) ž lim
nqh
f ( n)
g ( n)
=0

( )
6. f ( n) = \ g ( n) ž lim
nqh
f ( n)
g ( n)
=h
12 Chapter 1 Asymptotic Analysis

7. f (n) = o(g(n)) ¡ f (n) = O(g(n)), but the converse is false.


8. f (n) = \(g(n)) ¡ f (n) = <(g(n)), but the converse is false.
9. f (n) is bounded above and below by positive constants if and only if
f (n) = 6(1).

Asymptotic Analysis and Limits


To determine the relationship between functions f and g, it is often useful to examine

f ( n)
lim = L.
nqh g ( n)
The possible outcomes of this relationship, and their implications, follow:
L = 0: This means that g(n) grows at a faster rate than f (n), and hence
that f = O(g) (indeed, f = o(g) and f | 6 (g)).
L = h: This means that f (n) grows at a faster rate than g(n), and hence
that f = <(g) (indeed, f = \(g) and f | 6 (g)).
L | 0 is finite: This means that f (n) and g(n) grow at the same rate, to
within a constant factor, and hence that f = 6(g), or equivalently, g = 6( f ).
Notice that this also means that f = O(g), g = O( f ), f = <(g), and g = <( f ).
f ( n)
There is no limit: In the case where lim does not exist, this technique
nqh g ( n )
cannot be used to determine the asymptotic relationship between f(n) and
g(n).
We now give some examples of how to determine asymptotic relationships
based on taking limits of a quotient.

EXAMPLE

Let

n( n + 1)
f ( n) = and g(n) = n2.
2
Then we can show that f (n) = 6(g(n)) because
f ( n) n2 + n
lim = lim =
nqh g ( n) nqh 2 n2

(dividing both numerator and denominator by n2)


1
1+
lim n = 1.
nqh 2 2
Asymptotic Relationships 13

EXAMPLE

If P(n) is a polynomial of degree d, then P(n) = 6(nd ). The proof is left to the
reader as an exercise.

EXAMPLE

Compare n100 and 2 n . We remind the reader of a useful result.

d f (x)
e = e f ( x ) f '( x ).
dx
We have
n
2n eln 2 e n ln 2
lim 100 = lim 100 = lim 1000 .
nqh n nqh n nqh n

We can apply L’Hopital’s Rule to the numerator and denominator of this limit
100 times. After this, we have

( )
100
2n e n ln 2 ln 2 2 n
lim 100 = lim 100 = lim = h.
nqh n nqh n nqh 100!

Therefore, we know that n100 = O(2n) and 2n = <(n100). In addition, using some
of the properties previously presented, we know that n100 = o(2n) and
2n = \(n100). Further, we know that n100 | 6(2n).

At this point, it is reasonable to discuss logarithmic notation and to note that


logarithms play an important role in asymptotic analysis and will be used fre-
quently throughout this text. As appropriate, we will use fairly standard terminol-
ogy in referring to logarithms. We write log e x as ln x , log 2 x as lg x , and log10 x
as log x .
We now continue with an example that uses logarithms.
14 Chapter 1 Asymptotic Analysis

EXAMPLE

Let f (n) = ln n and g(n) = n. Then, by applying L’Hopital’s Rule, we have


n 1
lim = lim ,
nqh ln n nqh 1/ n

which evaluates as
1
lim = lim n = h.
nqh 1 / n nqh

Therefore, ln n = O (n).

We remind the reader that log b x = (log b a)(log a x), for positive a, b, and x
with a | 1 | b . Therefore, because log b a is a constant, log b x = 6(log a x). That is,
the base of a logarithm is irrelevant inside asymptotic notation, except that we
assume a, b > 1 (so that the logarithms are positive, because we generally have
x > 1 in such contexts).

Summations and Integrals


Because many algorithms involve looping and/or recursion, it is common for the
analysis of an algorithm to include a dependence on some function f (n) that is best
expressed as the sum of simpler functions. For example, it may be that the domi-
nant term in an analysis of an algorithm can be expressed as f(n) = h(1) + h(2) +
... + h(n). When we consider the worst-case number of comparisons in the
InsertionSort routine later in this chapter, we will find that the total number of
comparisons can be computed as f (n) = 1 + 2 + 3 + ... + n = n(n + 1)/2 = 6(n2).
We first consider the case where the function h(i) is nondecreasing. (Notice
that the worst-case number of comparisons used in InsertionSort, as mentioned
previously, uses the nondecreasing function h(i) = i.) Specifically, let
n
f ( n) = ¨ h( i ),
i =1

where h is nondecreasing. (An illustration of this situation is presented in Figure


1.8.)
Asymptotic Relationships 15

Rectangles aligned Rectangles aligned


to the right. to the left.

h(n) h(n)
h(1) h(1)
1 2 ... ... n 01 ... ... n−1
n
FIGURE 1.8 An illustration of bounding the summation ¨ h(i) by the integral of
i=1
the nondecreasing function h(t). On the left, we demonstrate how to use the inte-
n +1
gral µ1
h( t )dt to derive an upper bound on the summation by aligning the rec-
n n +1
tangles to the right. Notice that ¨ h(i) f µ 1
h(t)dt . On the right, we show how
i=1
n
to use the integral µ0
h(t)dt to derive a lower bound on the summation by align-
n
n
ing the rectangles to the left. Notice that µ h(t)dt f ¨ h(i) . Therefore, we have
0
n n n +1 i=1

µ h(t)dt f ¨ h(i) f µ h(t)dt .


0 1
i=1

To evaluate f (n), we can consider summing n unit-width rectangles, where the


th
i rectangle has height h(i). In Figure 1.8, we present these rectangles in two ways
to obtain tight bounds on the asymptotic behavior of the total area of the rectangles
(in other words, the value of f (n)). On the left, we draw the rectangles so that the
ith rectangle is anchored on the left. That is, the left edge of the rectangle with
height h(i) is at value i on the x-axis. In this way, you will notice that each rec-
tangle is below the curve of h(t), where t takes on values between 1 and n + 1
(assuming 1 is the value of the lower bound and n is the value of the upper bound
in the sum).
Conversely, on the right of Figure 1.8, we draw the rectangles so that the ith
rectangle is anchored on the right. That is, the right edge of the rectangle with
height h(i) is at value i on the x-axis. This allows us to use the rectangles to bound
the area of the curve, between 0 and n (assuming that 1 is the value of the lower
bound and n is the value of the upper bound) from above. Notice that in Figure 1.8,
we give the relationships of the area under the curve bounding the rectangles (left)
16 Chapter 1 Asymptotic Analysis

and the rectangles bounding the area under the curve (right side). In addition, we
show how to combine these relationships to obtain a bound on the summation by
related integrals.
The method of determining asymptotic analysis of a summation by integration
is quite powerful. Next, we give several examples and, in doing so, illustrate a
variety of techniques and review some basic principles of integration.

EXAMPLE

Find the asymptotic complexity of


n
f ( n) = ¨ i.
i =1

First, we consider the integral bounding principles that were given previously.
Because the function h(i) = i is nondecreasing, we can apply the conclusion di-
rectly and arrive at the bound
n
n n+1
µ tdt f ¨ i f µ tdt.
0 1
i =1

Evaluating both the left and right sides simultaneously yields


n n+1
t2 n
t2
f ¨i f
2 0 i =1 2 1

which can be evaluated in a fairly routine fashion, resulting in

( )
2
n2 n n +1 1
f ¨i f  .
2 i=1 2 2
Working with the right side of this inequality, we can obtain

( n + 1)2 1 1 2
 = n + n.
2 2 2
Further simplification of the right side can be used to give

1 2 1
n + n f n2 + n2
2 2
Asymptotic Relationships 17

for n v 1 . Therefore,

1 2 n 3
n f ¨ i f n2 .
2 i =1 2

Because the function


n
f ( n) = ¨ i
i =1

is bounded by a multiple of n2 on both the left and right sides, we can conclude
that

( )
n
f ( n) = ¨ i = 6 n 2 .
i =1

EXAMPLE

Find the asymptotic complexity of


n
1
g ( n) = ¨ .
k =1 k

1
First, it is important to realize that the function k is a nonincreasing function.
This requires an update in the analysis presented for nondecreasing functions.
In Figure 1.9, we present a figure that illustrates the behavior of a nonincreas-
ing function over the interval [ a, b] . Notice that with the proper analysis, you
should be able to show that
b b
b
¨ f ( k ) f µ f ( x ) dx f ¨ f ( k ).
a
k = a +1 k =a

Based on this analysis, we can now attempt to produce an asymptotically tight


bound on the function g(n). First, we consider a lower bound on g(n). Our
analysis shows that
n
n 1 1
µ dx f ¨ .
1 x k =1 k
18 Chapter 1 Asymptotic Analysis

Because
n 1 n
µ dx = ln x ¼¾ 1 = ln n  ln 1 = ln n,
1 x
we know that g(n) is bounded below by ln n.
Next, we consider an upper bound on g(n). Notice that if we apply the re-
sult of our analysis for a nonincreasing function blindly, we obtain
n
1 n 1 n
¨k f µ dx = ln x ¼¾ 0 = h.
k =1
0 x

Unfortunately, this result does not yield a useful upper bound. However, notice
that the cause of this infinite upper bound is evaluation of the integral at the
specific point of 0. This problem can be alleviated by carefully rewriting the
equation to avoid the problematic point. Let’s consider the more restricted
inequality
n
1 n 1
¨k f µ 1 x
dx.
k =2

Notice that the integral evaluates to ln n. Therefore, if we now add back in the
problematic term, we arrive at
n n
1 1 n1
¨k = 1 + ¨ k f 1 + µ1 x dx = 1 + ln n.
k =1 k =2

Combining the results of both the upper and lower bounds on g(n), we arrive at
n
1
ln n f ¨ f 1 + ln n f 2 ln n,
k =1 k

for n large enough (verify). Therefore,


n

¨ k = 6 ( ln n).
1
k =1
Asymptotic Relationships 19

Note: f (x) is nonincreasing

a a1 b1 b

FIGURE 1.9 An illustration of bounding


n
the summation ¨ f(i) for a nonincreas-
i=1

ing function f. For f nonincreasing,

we can derive the relationship


b
b+1 b
µ f(t)dt f ¨ f(i) f µ f(t)dt .
a a-1
i=a

EXAMPLE

As our final example of evaluating the asymptotic behavior of a summation by


integrals, we consider the function
n
f ( n) = ¨ k p ,
k =1

for p > 0 . (We showed earlier that

( )
n
f ( n) = ¨ k p = 6 n p+1 .
k =1

However, we now show how to obtain this result by another method.) Consider
the derivative of k p. For k > 0 , we have

d p
k = pk p1 > 0.
dk
Therefore, the function k p is an increasing function. A quick sketch of an in-
creasing function (f is increasing if u < v ¡ f (u ) < f ( v ) ), in a setting more
general than illustrated earlier, appears in Figure 1.10.
20 Chapter 1 Asymptotic Analysis

Using the analysis associated with Figure 1.10, we have both


n n
n n+1
µ x p dx f ¨ k p and ¨k p
fµ x p dx. Thus,
0 1
k =1 k =1

n n+1
x p+1 n
x p+1
f ¨k p f , or
p + 1 0 k =1 p +11

( ) ( )
p +1 p +1
n p+1 n n +1 1 n +1
f ¨k p f < .
p + 1 k =1 p +1 p +1

Because n + 1 f 2 n for n v 1 ,

( ) ( 2n)
p +1 p +1
n p+1 n n +1 2 p+1 n p+1
f ¨k p f f = , or
p + 1 k =1 p +1 p +1 p +1

1 p+1 n p 2 p+1 p+1


n f ¨k f n ,
p +1 k =1 p +1

which, based on asymptotic properties given earlier in this chapter, yields the
expected solution of

( )
n

¨k p
= 6 n p+1 .
k =1

a b

FIGURE 1.10 An increasing function in the


range [ a, b] . We have
b1 b
b
¨ f ( k ) f µ f ( x ) dx f
a
¨ f ( k ).
k =a k = a +1
Rules for Analysis of Algorithms 21

Rules for Analysis of Algorithms


The application of asymptotic analysis is critical to provide an effective means of
evaluating both the running time and space of an algorithm as a function of the
size of the input. In this section, we present fundamental information about the
analysis of algorithms and give several algorithms to illustrate the major points of
emphasis.
Fundamental operations execute in 6(1) time: Traditionally, it is
assumed that “fundamental” operations require a constant amount of
time (that is, a fixed number of computer “clock cycles”) to execute. We
assume that the running time of a fundamental operation is bounded by a
constant, irrespective of the data being processed. Such operations
include the following:
• Arithmetic operations ( + ,  , × , / ) as applied to a constant number
(typically two) of fixed-size operands.
• Comparison operators ( <, f, > , v , = , | ) as applied to two fixed-
size operands.
• Logical operators (AND, OR, NOT, XOR) as applied to a constant
number of fixed-size operands.
• Bitwise operations, as applied to a constant number of fixed-size
operands.
• I/O operations that are used to read or write a constant number of
fixed-size data items. Note this does not include input from a
keyboard, mouse, or other human-operated device, because the user’s
response time is unpredictable.
• Conditional/branch operations.
• The evaluation of certain elementary functions. Notice that such
functions need to be considered carefully. For example, when the
function sin V is to be evaluated for “moderate-sized” values of V, it is
reasonable to assume that 6 (1) time is required for each application of
the function. However, for very large values of V, a loop dominating
the calculation of sin V might require a significant number of
operations before stabilizing at an accurate approximation. In this case,
it might not be reasonable to assume 6 (1) time for this operation.
We mention additional fundamental properties.

• Suppose the running times of operations A and B are, respectively,


O(f (n)) and O (g(n)). In this case, the performance of A followed by B
takes O(f (n) + g(n)) time. Note that this analysis holds for 6, <, o,
and \, as well.
22 Chapter 1 Asymptotic Analysis

• Next, suppose that each application of the body of a loop requires


O(f (n)) time, and the loop executes its body O (g(n)) times. The time
required to execute the loop (that is, all performances of its body) is
O(f (n)g(n)). A similar property holds for 6, <, o, and \.

EXAMPLE (INSERTIONSORT)

As an example, we consider the analysis of InsertionSort, a simple sorting


technique that is introduced in many first-semester computer science courses.
Suppose we are given a set of data arbitrarily distributed in an array and we
want to rearrange the data so that it appears in increasing order. We give
pseudocode for the algorithm and then present an analysis of both its time and
space requirements. Note that later in this book, we compare more advanced al-
gorithms to InsertionSort, and also show how InsertionSort can be effectively
exploited in restricted situations, for example, where the set of data presented to
InsertionSort is such that no item is very far from where it belongs.

Subprogram InsertionSort(X)
Input: an array X of n entries
Output: the array X with its entries in ascending order
Local Variables: indices current, insertPlace

Action:

For current = 2 to n do
{The first (current–1) entries of X are ordered.
This is why current is initially set to 2.}
Search X[1…current–1] to determine the index, denoted as insertPlace
‘ {1,…,current –1}, where X[current] should be inserted.
Make a copy of X[current].
Shift the elements X[insertPlace,…, current–1] down by one position
into elements X[insertPlace+1,…,current].
Place the copy of X[current] into its proper position at
X[insertPlace].
End For

The previous description presents a top-level view of InsertionSort. An ex-


ample is given in Figure 1.11. We observe that the search called for in the first
step of the loop can be performed by a straightforward sequential search that re-
quires O(k) time, where k is the value of current. The reader should verify that
Rules for Analysis of Algorithms 23

this requires 6(k) time on average. Alternately, an O(logk) time binary search
can be performed, as will be discussed in the chapter on Induction and Recur-
sion. Thus, the total search time is

( )
© n ¹
O ª ¨ k º = O n2
« k =2 »
time if sequential searches are used, and
© n ¹
(
O ª ¨ log k º = O n log n
« k =2 »
)
time if binary searches are used. Notice that O-notation is used, because both
results represent upper bounds on the search time.
Regardless of which search is used to locate the position that X[current]
should be moved to, notice that on average, it will require current/2 movements
of data items to make room for X [current]. In fact, in the worst case, the insert
step always requires X[current] to be moved to position number 1, requiring
current data items to be moved. Therefore, the running time of the algorithm is
dominated by the data movement, which is given by
n
T ( n) = ¨ movementk
k =2

where movementk is 0 in the best case, k in the worst case, and k/2 in the aver-
age case. Hence, the running time of InsertionSort is 6(n) in the best case
(when data is already sorted and a sequential search from (current – 1) down to
1 is used), 6(n2) in the average (or expected) case, and 6(n2) in the worst case.
The reader should verify these results by substituting the appropriate values
into the summation and simplifying the equation. Notice that the average- and
worst-case running times are dominated by the data movement operations.
Finally, notice that 6(n) space is required for the algorithm to store the n
data items. More important, the amount of extra space required for this algo-
rithm is constant, that is, 6(1). An insertion routine follows.

Subprogram Insert(X, current, insertPlace)


Insert X[current] into the ordered
subarrary X[1… current–1] at position
insertPlace.
24 Chapter 1 Asymptotic Analysis

We assume 1 f insertPlace f current f n


Local variables: index j, entry-type hold

Action:

If current | insertPlace , then {there’s work to do}


hold = X [current ]
For j = current  1 downto insertPlace, do
X [ j + 1] = X [ j ]
End For
X [insertPlace] = hold
End If

For completeness, we present an efficient implementation of InsertionSort


based on the analysis we have presented.

Subprogram InsertionSort(x, n)
{This is a simple version of InsertionSort with sequential search.}

For i = 2 to n, do
hold = x[i]
position = 1
While hold > x[position], do
position = position + 1
End While
If position < i, then
For j = i downto position, do
x[j] = x[j – 1]
End For
x[position] = hold
End If
End For
End InsertionSort

It is often possible to modify an algorithm designed for one data structure to


accommodate a different data structure. The reader should consider how Insertion-
Sort could be adapted to linked lists (see Exercises).
Rules for Analysis of Algorithms 25

4 3 3 1 1

3 4 4 3 2

5 5 5 4 3

1 1 1 5 4

2 2 2 2 5

FIGURE 1.11 An example of InsertionSort. It is initially assumed that


the first item (4) is in the correct position. Then the second item (3) is
placed into position with respect to all of the items in front of it, result-
ing in (3,4) being properly ordered. The algorithm continues until the
last item (2) is placed in its proper position with respect to the items
(1,3,4,5) that are in front of it.

EXAMPLE: BINSORT

Sorting is a fundamental problem in computer science because a major use of


computers is to maintain order in large collections of data. Perhaps for this rea-
son, researchers have developed many algorithms for sorting. Some of these are
considerably faster than others. Yet, sometimes the asymptotically slower algo-
rithms are useful because, for example, they may be very fast on relatively
small data sets or they may be very fast on sets of data that exhibit certain char-
acteristics. We will present several sorting algorithms in this book and examine
such issues.
In the previous section, we presented an analysis of InsertionSort. In one of
the exercises at the end of this chapter, we present SelectionSort, a fairly
straightforward, useful sorting routine that exhibits the same worst case
6(n2) running time as InsertionSort. Later in the book, we present alternative
comparison-based sorting algorithms that exhibit optimal 6(nlog n) worst case
running times. In fact, many of you may already be familiar with the result that
states that comparison-based sorting requires <(nlog n) time.
Although <(nlog n) is a lower bound on general comparison-based sorting,
one might ask whether or not it is possible to sort a set of data in o(nlog n) time.
In fact, although this is not possible in general, it is possible given a set of data
that is not “arbitrary.” An important theme that runs through this book is that
one should attempt to design an o(nlog n) time-sorting algorithm if one knows
something about the data a priori.
26 Chapter 1 Asymptotic Analysis

For example, suppose you know that you are required to sort data that is
chosen from a restricted set. Maybe you know that the keys can take on only
O(n) distinct values. In this case, one can employ a BinSort algorithm. BinSort
is modeled on the process of placing each member of a collection of numbered
items (such as machine parts) into a correspondingly numbered bin. Alterna-
tively, one might think about sorting a deck of cards by going through the deck
once, tossing all the aces in one pile, all the 2s in another, and so on. Once you
have gone through all the cards and created your 13 bins, you simply need to
concatenate the bins to create the final sorted set. Notice that if you sort more
than one deck of cards, you still need only 13 bins. Given one complete deck of
cards, each bin will wind up with exactly four cards in it. An example of Bin-
Sort is presented in Figure 1.12.
Next, we give a description of BinSort under the assumption that the range
of data is the integer values from 1 to n. It is important to note (in terms of the
proof that <(nlog n) comparisons are required to sort an arbitrary set of data by
a comparison-based sort) that BinSort is not a “comparison-based” sorting al-
gorithm. That is, BinSort does not rely on comparing data items to each other.
In fact, the algorithm never compares two data items.

Subprogram BinSort(X)
Sort the array X via the BinSort algorithm.
We assume entries of X have integer key values 1… n.
Local variables: indices j, s;

temp, an array of pointers, each representing a stack


Action:

For j = 1 to n, do
{make temp[j] an empty stack}
temp[j] = null
For j = 1 to n, do
push(X[j], temp[X[j].key])
s=1
For j = 1 to n, do
while emptyStack(temp[s])
s o s +1
end while
pop(temp[s], X[j])
End For

An analysis of the algorithm follows. It is easy to see that the first two For
loops each require 6(n) time, after which each element is in one of the n bins.
Another Random Scribd Document
with Unrelated Content
towards him; but another voice told her she had no right, outcast as
she was, to marry this man; that such an act would make her guilty
forever of having destroyed him as a part of society. A sacrifice was
demanded of her! She must be more generous even than he,
subdue herself, suffer, submit to her fate, refrain from dragging him
down with her! She did not know where the voice came from. It may
have been crying out to her from that afternoon when she first
listened to Monsalvat telling her to suffer in order to find
redemption; but it was a voice that awed her tormented soul even
while it bade her speak and leave this man. Then the strange
serenity of sacrifice came to her rescue. She was pale as death, and
smiled so as not to weep. She summoned all the love within her not
to let her yield.
"Yes, you must marry me," Monsalvat was insisting desperately.
"No."
"What is it, Nacha? Why are you so strange? I love you, you love
me...."
Her will triumphed. She called to mind other moments of her life and
made one supreme effort. Then she began to laugh.
"No, I couldn't love you. All this is ridiculous anyway! Such make-
believe is unworthy of you. I put you out of my house once before,
and I'll do it again. You simply want to make fun of me, because I'm
a poor girl, and defenceless. You wanted to make a fool of me,
getting me to swallow all this stuff! But now it's my turn to laugh at
you, just as I did in the cabaret. I—married! And to you, a crazy
man!"
She broke into a laugh that was loud and false and harsh.
Monsalvat remained seated, his hands clasped over his head; he was
dizzy with pain, and he could not understand....
"You are mad ... you have gone mad!" he exclaimed.
Was she really fainting? She saw Monsalvat cover his face with his
hands; she turned to the wall and leaned against it, letting herself
weep for a brief moment. There was relief in that. With renewed
strength, she sat down on a chair and waited. Soon Monsalvat stood
up. He too was pale as he came near her and, barely looking at her,
held out his hand.
"Some time ... you will ... let me see you?" he faltered.
"No. Why should I? I don't love you. Leave me. And if it's true that
you love me, forget me as soon as you can. Go, please! I am ill, and
want to be alone...."
Monsalvat did not insist. He could not have done so. He took his hat
and went away, stumbling like a man who has come to the end of
his strength. One might have thought him sick, or crazy, or perhaps
drunk, as he staggered out. Crossing that threshold was like
wrenching his soul from his body; and in the little parlor that knew
only shabbiness and shame, grief remained, lending it a dignity it
had never known before.
Nacha could no longer hold her anguish at bay. She snatched off her
hat with a frantic gesture, and tore it into bits. Moaning and weeping
she fled into one of the other rooms and threw herself down on the
bed.
The cripple rolled her wheel chair to the door and looked in.
Believing that she understood Nacha's trouble, she did not disturb
her, but went away again. She talked to the girls awhile; but the
tragedy she saw close at hand saddened her; for it reminded her of
old intimate griefs of her own. She too, in her youth, had known
love, in far away Italy; and that love had been maimed and
destroyed. After that, dishonor and vice seemed a small matter; yet,
at times, even now, she went back in thought to the home of her
childhood, so different in its simple beauty from the wretchedness of
her present surroundings. But here she was, old, crippled, with no
choice but to go on in the familiar rut. Why let herself be saddened
then? She had known life, and found that melancholy had a bad
effect on the liver! So she chatted with the girls, merrily, as was her
custom whenever she felt a touch of sadness.
But someone came in, and asked for Nacha. The cripple rolled her
chair into the bedroom where the girl was still weeping, her head
almost hidden by the pillow.
"Nacha child! Don't cry that way! Why let yourself suffer so? No man
is worth it. You know that. You are worth more than the best of
them, you have a good heart ... and they...."
She muttered an obscene word to herself and began to laugh.
"Come, Nacha, someone wants to see you. They are all alike! No
one of them is worth more than another. They're all rotten—just
good to ruin women and then desert them. Come, child, come—
here's a friend!"
She patted Nacha on the shoulder, and told her she would send her
caller in. Nacha suddenly sat up. She wiped away her tears and said
quietly, "No, señora. Don't send him. I am going away for good."
"But, child, why? Are you angry with me?" the old cripple exclaimed,
astonished by Nacha's tone. "Aren't you ever coming back to my
house?"
"Neither to your house nor to any other. I am not angry. You have
been very kind to me, and I shall never forget it."
"Well then...." The woman did not know what to make of the girl's
words.
Nacha was silent while she smoothed her hair, and straightened her
dress. Then she kissed the cripple, took both her hands and said,
her lips quivering with pain:
"It's because ... I want to be worthy ... of that man's love...."
"Oh, I see. You want to be respectable for awhile, and then get
married...."
The cripple spoke with the certainty of a woman who understands
what she is talking about. Nacha's expression, however, indicated
that her purpose was not quite as the cripple supposed.
"What is it then? Tell me. You know I like you, child, and respect
you. And I'd do for you anything you ask. If you want to live decent,
and need money, I'll give it to you—I'll save so I can!"
Nacha was touched.
"You are good, señora. I thank you from my very heart; and because
I know how good you are, I'll tell you. No, I'm not going to get
married. I couldn't let him marry me. But he loves me—so much!
And if he gives me such great love, I want to be decent. Not to get
married, no, just to be worthy of living in his thoughts, and in his
heart...."
The paralytic drew the girl's head down to her twisted old lips and
kissed her. Freeing herself from the woman's embrace, Nacha hastily
left the room.
As she fled down the stairs she realized that it was many years since
she had felt as happy as at that moment!
CHAPTER XVI
One afternoon as Torres was lunching with Ruiz de Castro in a
restaurant on the Esmeralda he thought he caught a glimpse of
Nacha.
As a matter of fact it was Nacha. She was returning to the store
where she had been employed some six years earlier, and with her
were a number of other girl employees, for it was nearly two o'clock,
the end of the lunch hour. Torres would have gone up to speak to
her if he had been alone; but Ruiz was relating his adventures with
that plump lady who had carried on so persistent a discussion with
Monsalvat at de Castro's dinner party, and had so eloquently
defended established institutions.
"You don't say!" murmured Torres, absently; for all his attention was
fixed on the slender figure hovering in front of the huge shop door
which was about to open and swallow her up.
"She's a wonder, my friend," proclaimed Ruiz, who was given to
committing indiscretions in words as well as actions. "What passion!
and how she can sob!"
When Torres reached his house he went at once to talk to Monsalvat
who was now living with him. After the serious illness that had
followed close upon his interview with Nacha, Torres had taken him
in hand, and when he discovered that his patient was paying no
attention to doctor's orders, had carried him off to his own home
where he could insist on obedience. He persuaded Monsalvat to ask
for a two months' leave, for there was no doubt that he was
suffering from brain-fag and serious nervous derangement.
Torres had a theory that Monsalvat's condition was not entirely due
to his passion for Nacha. He knew the history of his friend's moral
struggles, and he believed that the causes of Monsalvat's illness
were numerous and complex. The latter's abrupt change of attitude
towards life could not but profoundly affect his whole nature.
Following this, had come several months of constant self-reproach,
and self-disgust for the uselessness and selfishness of his life up to
that time. He went as far as to blame himself for his inability to
transform the world. Torres had tried, vainly, to prove to him that he
was far from useless, and that no one could have called him selfish.
His conduct compared surprisingly well with that of other men of his
generation; and his reputation indicated general recognition of that
fact. Monsalvat protested that all this might be true from a
superficial and worldly view of his life, but it only proved how false
were society's standards.
"Useless and selfish," Monsalvat repeated. "Not less so than
prominent politicians or ranch owners, lawyers, and men in society.
We are all selfish. I do not condemn myself only. I condemn all the
rest as well. The world is full of evil, selfishness, meanness—and I
have shared in it all. That is why I despise myself, and abhor my
past life."
Torres wisely kept silent, for fear of exciting his patient.
It was clear also that the knowledge of his sister's mode of life, and
of the degradation his mother had fallen into before her death, had
seriously injured Monsalvat's nervous system. The scene with Irene,
his worrying about the tenement, the anxieties of that search
through the world of fallen women, the sight of so many horrors,
had all left their mark on him; and finally the shock of Eugenia's
death, intensified by the manner in which he had learned of it, had
played its part in undermining his health. Obviously his love for
Nacha, his unsuccessful attempt to save her, the knowledge that she
was leading a vicious life, perhaps because of him, were the
principal causes of his breakdown, but all these other matters played
an important part in bringing about his present condition.
Now, however, after two months of rest and quiet, Monsalvat was
beginning to be himself. The companionship of Torres had done him
a great deal of good. The doctor made him eat, gave him stimulants
when he needed them, encouraged him to spend most of his time
out of doors and even stayed up with him on the nights when he
was unable to sleep.
Torres might have accomplished a complete cure, had not the evil
that flourishes in certain human hearts prevented. Monsalvat had
recently received some anonymous letters, four in all. One of them
insulted him by insulting his mother, another called him to account
for living on women, and being an anarchist! The other two were
content with intimating that he belonged in a lunatic asylum, and
would soon be put there. The effect of these letters was to excite
him so that he could neither sleep nor eat. The first especially
reawakened in him his life-long obsession, cruelly reminding him of
what was, in his estimation, the reason for his moral bankruptcy.
The doctor wondered who could have sent these letters, for
Monsalvat's position was not such as to excite envy. At the Ministry
his new ideas had become known, and Monsalvat was looked upon
with hostility or contempt. Even the Minister mistrusted him now. In
the social circles where he was once respected, he had lost all
consideration. Ercasty was methodically discrediting him, with
admirable persistence and thoroughness. Informed by mutual
acquaintances of Monsalvat's views with respect to Nacha and other
girls of her sort, and of that frantic search through houses of ill-
fame, he confirmed the rumor that Monsalvat had fallen very low
indeed. At first he was content with making insinuations; but finally
he came out with the bald statement that Monsalvat was a vulgar
exploiter of women. Of course there were not lacking those who
accused him of participating in frightful anarchist plots, and
preparing bombs for wholesale assassinations.
Financially too he was ruined. The forty thousand of the mortgage
raised on his property had melted away. His mother's debts, the
mulatto's blackmail, Moreno's incessant appeals, had taken several
thousand. His excursion through the city's public houses had cost
him four thousand pesos. Ten thousand pesos had gone for
improvements on the tenement. Monsalvat decided he would have
to sell the building, for his salary was barely enough for his own
expenses, and his tenants either paid no rent or paid very little.
That afternoon Monsalvat was reading as he lay in bed. The book
beside him was the New Testament. On his face was reflected
something of the serenity of late afternoon. When Torres opened the
window to let in air and sunshine, everything in the room seemed to
draw a breath, and grow animate. A bar of light like a luminous
golden coverlet spread over the bed.
"Look at that!" exclaimed the doctor. "And you spend your time shut
up here almost in the dark. You'll never get well that way. You ought
to go to Palermo, stay out in the sun—and not read or write a line."
"I know what I need," replied his friend quietly.
"What do you need? You are always mysterious."
Monsalvat went on reading. Torres remained with him for a few
moments and then withdrew without a word.
The doctor had been observing his friend for over a month, with
constantly growing curiosity. Monsalvat's intelligence seemed to have
grown sharper and deeper. He was still weak in body but his mind
was keener than ever. He reasoned with irrefutable logic, and
divined his opponent's arguments at a word. Torres attributed this
mental fitness to mental exercise. His patient talked with no one but
his host, did not go out, read very little; but all day long he was
occupied in thinking and remembering, trying to interpret his past
life, trying to understand the significance of the life he was then
experiencing. He spent hours analyzing the persons he knew, and
with extraordinary penetration. Torres was more than once
overcome with amazement when Monsalvat guessed his thoughts.
"Why should you be startled?" Monsalvat asked him on a certain
occasion. "What has happened is simply this. I am living from within
now. Up to six months ago I lived from without, superficially; and
the life I lived seemed to be the life of other people rather than my
own. It was an objective, a false, a lying kind of life. Just like your
own and that of nearly everyone. A materialistic kind of life, never
transcending the commonplace, devoid of mystery, and of genuinely
spiritual anxiety. But now my eyes are open and I begin to
understand. I have analyzed myself, I have looked within; and I
have discovered a great many things there that I knew nothing of. I
know now what there is in me, and what parts of it are worth
something, and what I must give to others. And I even begin to
suspect why I am alive!"
"I knew before that...."
Torres stopped abruptly, not caring to end his sentence. He
pretended to have forgotten what he wanted to say.
"Why don't you go on? Have you really forgotten what was on the
tip of your tongue? Well, I know what it was. You were going to say
that all that happened this past year, and the love I found, would
lead me straight to ... mysticism!"
"What? No, no, not that, exactly."
But that was exactly what he had been thinking. Monsalvat knew
how abhorrent to a man as orderly and normal, as submissive to
society's dicta, as Torres, the word "mysticism" must be. The doctor
had come to admit society's responsibility for much of the
unhappiness in the world; but he had no sympathy for those heroic
acts necessary to drive out injustice. He admired Monsalvat but at
the same time considered his passion for redeeming others a form of
insanity. According to Torres a normal man should accept things as
they are. The rebel, he who at sight of the suffering of life's victims,
breaks out into indignant accusations or takes up some useless but
heroic work, was, in his estimation, a madman.
Since his recent glimpse of Nacha, Torres had been anxious to talk
to her. Once or twice he watched the girls coming out of the shop.
He saw Nacha again, but it was very evident that she avoided him.
Convinced that Nacha did not care to hear any news of Monsalvat,
whose friendship with him she must have known, he gave up his
attempt to communicate with her.
The days went by. Monsalvat never spoke of Nacha and little by little
Torres came to the conclusion that he had forgotten her.
One morning, in March, Torres went to his guest's room at a very
early hour, to dissuade him from going away.
"Why leave me, Monsalvat? Stay here a couple of months longer,
until you are quite all right again. The kind of breakdown you're just
getting over is no joke, my dear boy. And where are you going
without a cent to your name, eh? Back to your quixotic notions
about righting all humanity's wrongs, and redeeming people who
have nothing to redeem about them? That's all nonsense, and leads
nowhere. One man alone can't accomplish anything. All you can do
is harm, filling the heads of those poor people with wild ideas. No,
my son. The world is full of evil. Well, what's to be done? You have
to take it as it is, and get what good you can out of it, and
—'forward, march!' Eh?"
Monsalvat did not reply. He lay on his side, his elbow resting on the
pillow, his hand on his breast, and his eyes turned towards the
window. But he was not looking at what was out there beyond him:
he was looking within, searching his own heart and the hearts of a
multitude of other human beings whom he saw there standing
between him and his friend. The doctor's words reached him from
far, far away—so far that he scarcely understood them. Meanwhile
the window seemed to be catching fire, making its offering of light
to Monsalvat as from a golden, quivering sheet of flame!
The doorbell rang. Without moving, Monsalvat said:
"That's the postman. He is bringing a letter from Nacha—for you."
Torres smiled at this prophecy; a forced smile, however, for he
feared that it might be true. He got up and was about to leave the
room when the maid came in with a letter. The doctor signed the
receipt for which the messenger was waiting, placing it for that
purpose on the table near Monsalvat's bed. He did not notice that
Monsalvat's eyes were fixed intently on the small bit of paper. Then
he opened the letter and looked at its signature, disconcerted.
Monsalvat laughed, enjoying his friend's confusion.
"It's from Ruiz de Castro. He wants to see me ... some affair of his
... he doesn't say what ..." stammered Torres, thrusting the letter
into his pocket. Then he went out, embarrassed and perplexed,
while Monsalvat smiled to himself.
For the letter actually did come from Nacha! She wrote that she
wanted to see Torres, but not at the entrance to the shop. From her
letter it appeared that she did not know where Monsalvat was. She
wanted to find out—that was why she wrote about him. She had
learned that he was ill; "Was it true?" she asked; and "was she to
blame?"
That evening Torres went to the lodgings at the address Nacha had
sent him. He found a respectable house, the tenants of which
appeared to be shop employees and their families.
"You don't know what I've been through," murmured Nacha. "We
met one afternoon, and I—"
Torres knew something of this meeting.
"But you don't know why I acted as I did," Nacha continued. "It was
because I loved him; because I didn't want to do him harm. So that
he, distinguished and fine as he is, shouldn't be ruined by
associating his life with that of a ... someone like myself.... You see?
Since that day I have lived straight; and somehow, I'm still alive,
although really I am dying ... with grief.... But this I accept, for his
sake, and to make up for the kind of life I led before. I accept it so
that he may not have to suffer, so that he will forget me, and be
happy, and go on with the kind of life he ought to have—even
though I die of it. What good am I?"
They were alone, facing each other over a small table, lit by a small
lamp which had been pushed to one side. Torres felt the shadows of
the room pressing around his throat, choking him. Nacha's face
alone stood out, catching the light. The doctor was thinking of the
frightful pranks Destiny can play.
But this emotion passed, and the man of the world, laden with
prejudices, falsehood, cruelties—and good, withal, replaced the plain
and honest man of feeling.
"You couldn't know what I've been through," Nacha repeated. "Since
that afternoon I have earned my living by work. First there were
days of discouragement, when I went hungry. Then I found
employment in a shop. Eleven hours a day and thirty dollars a
month! I get a bonus too. But there are fines for the slightest thing.
Altogether I earn about sixty dollars more or less—there's no rest
during those eleven hours. Sometimes they send me with a load of
goods up to the fifth floor. We aren't allowed to use the elevators. It
isn't a gay life, you see. But it's for him, so I don't mind! Not so that
he'll love me—I'm not worthy of living with him—just to deserve,
even at a distance, a little of the love he has for me!"
Torres looked away from her; it occurred to him that this change in
Nacha was a danger for Monsalvat. He believed he must save his
friend once for all, and to accomplish that required a lie. He reflected
that it was really too bad that deceit should at times be necessary,
even to accomplish good results. Something inquired of him if he
really believed that the purpose he had in view was "good." He
hesitated a moment; but he remembered the world's opinion, the
world's morality, the world's sentiments. He turned towards Nacha,
and with a gesture as if he was casting from him an unpleasant
thought, and in a hard voice, he said:
"You must not see him again, Nacha, ever. Anyway, he has forgotten
you. Yes! He is in love with another woman, and is thinking of
getting married. You don't want to wreck his plans, eh?"
She could not see. Everything was dark. She felt a "yes" come
mechanically from her throat, and she put out a hand, so inert, that
it barely felt the rapid pressure of another hand. Then came the
noise of a closing door, and the sound of retreating footsteps. But
darkness remained, empty ... and endless.

As she sat at the small table, her senses dull to everything, she did
not hear a knock at her door; nor was she aware that a man had
come in and was there, before her, waiting. A sudden leap of her
heart, and a flash of consciousness made her raise her eyes. She
thought she must be feverish, in a delirium! She would have cried
out, but something within her, that overpowered her, muffled her
voice.
"Nacha!" he said.
"Is this true? It is not a dream? Not a dream?"
They were face to face, but they could not speak. No words could
express what shone in Monsalvat's eyes, and echoed in Nacha's
breathless weeping. The room seemed to fill with memories of the
distant past, scenes fraught with sorrow, and ancient longings,
taking on a strange, mysterious life, like an old temple that has
heard the prayers of centuries.
Nacha's tears were for what had been, and what ought to have
been; for what she had not wanted to be, and what the world had
forced her to become. Monsalvat sat at her side, caressing her
hands; but he saw facing him the two men he had been in his
lifetime, and he demanded an account of them for what his life had
been, looking into their very souls, cursing them; and before Nacha
passed the different women who had dwelt in her body, the bad
woman, and the good, the victim and the weakling.
And in that dim light, they understood one another, these two
suffering human beings. The light in the heart of each shone out to
the other. Their heads drew close together. Without knowing it,
without seeking it, they kissed gently, like children of one mother.
CHAPTER XVII
Monsalvat that very afternoon had taken lodgings in the house in
order to be near Nacha. As Torres signed it, he read the receipt of
the special delivery letter; and he hurried to the Messenger Service
Bureau and there learned Nacha's address. When he reached the
building where she lived he noticed a sign announcing a furnished
room to let in this tenement which was an old family dwelling, now
rented out to numerous lodgers. Monsalvat took the room on the top
floor facing the street. Thus, it happened that when he appeared in
Nacha's quarters he was already a tenant in the same house.
Torres' efforts to find out what had become of Monsalvat were all
unsuccessful. He even wrote to Nacha, who replied that she had not
seen him nor had any news of him. These falsehoods did not much
trouble her conscience. She wanted to keep Monsalvat near her,
have him for herself alone; and she was fearful of his friends, of his
associates at the ministry, of everything which threatened to
interrupt her possession of him. When, in the afternoon, she came
home from work, she could scarcely breathe with the anxiety and
the fear of no longer finding him there.
But the emotion she felt was to all appearances purely fraternal.
Suffering had spiritualized it. The first kiss had been the last. Nacha
knew how little the physical aspect of love meant. She could not
offer her lover something of as little price as her body. To Monsalvat
she would give her heart and soul and whatever good there was in
her; her tenderness, as immeasurable as space, and her suffering,
as deep as the sea. Nor did he desire her. Nacha was no longer a
mere woman to him; she had become a symbol, tremendously
significant, of all women who pay the penalty she was paying, of
those victims rejected by society—daughters of the mire and of
human misery; and she was his sister as well. If at times he desired
Nacha, the desire was fleeting, a passing sentiment. He knew that it
was this sentiment which had drawn him towards her; and in this
fact he saw a proof of the wisdom of instinct, of nature's
fundamental soundness; for desire had, in moments of vacillation
and uneasiness of conscience, led him to the right road. Now he was
no longer a man of the world, nor a distinguished lawyer, nor
anything else that he had been. As far as the world was concerned,
he was a ruined man. But in his own eyes he had saved himself,
found a purpose for his life; the purpose to give everything he had
to others, and to suffer for them. What did all the rest matter if, in
this course of conduct, he found what he recognized as the "Good"
he craved?
And so time passed. Nacha went to the shop in the morning, and
returned at night. Monsalvat went out only to go to the Ministry, and
to offer relief to those in great need. When he came back from the
office he gathered the children in the house together and taught
them to read; and his evenings were for Nacha, for long waking
dreams—a book in his hands, and silence keeping watch over them
like a faithful dog. His evenings were for that idealized love which
Nacha too now understood.
But one evening Nacha told him of the doubts that troubled her.
Why sacrifice one's life, and tranquillity, and happiness, for others?
With so much wretchedness in the world, what could one man's slow
and small accomplishment matter? And why give one's whole soul to
something that offered no visible reward?
"Nacha," he replied, "to sacrifice ourselves for others is a duty. It is
the only reason for our living. If we all accepted this principle, life
would be inconceivably beautiful. And what other principle makes
our lives consistent with our opinions and our ideals—granted we
have opinions and ideals? It is an obligation we owe to those from
whom we have taken their share of happiness. There are not many
who pay this debt, not many who comply with this law. People not
only resist the law of love implicit in sacrifice, but they will to be
selfish, and bad. But doesn't that make it all the more our duty,
Nacha, to do what we can? We must win forgiveness for the wrongs
we do our brothers, for the guilt of society in which we all share."
He stopped, and looked dreamily before him, as though he saw
some luminous object in the distance. Then, after a moment of
silence, he added:
"The work of one individual has tremendous value as an example.
Good work is not lost. It arouses other souls; and each one of these
will waken others, who, but for them, would continue to sleep. So,
little by little, daylight will come; injustice will cease; and poverty will
be a word."
Monsalvat was at work on two plays which Nacha helped him to
copy. They proved to be somewhat incoherent compositions, full of
anguish, and love, and pity. They excited keen interest among the
theatrical managers to whom he submitted them but no one cared
to produce them. Some one of the readers who examined them
called the plays "anti-social"; and they were generally considered
dangerous to established order. In truth, they contained too much
human sympathy: but it may well be that justice, or even simple
honesty, is a serious menace to society!
One Sunday afternoon Julieta came to call on Nacha. She was no
longer the smiling Julieta of old. Bad luck had been haunting her
footsteps of late; and for the last few weeks she had known what it
was to go hungry. While she was telling Nacha her troubles
Monsalvat came in. Julieta did not know him, and stopped short.
"It is my friend," said Nacha. "He can help you. Go on!"
Julieta, reassured as much by a glance at Monsalvat as by Nacha's
words, told how her small savings had all been spent to help Sara
who had suddenly developed a horrible disease.
"I thought I could earn more if I had to," said Julieta, "but I haven't
been able to. I've had to give up my room at Lavalle Street; and
they are going to put me out of the lodging house where I have
been staying, because my rent isn't paid. I can't go out on the
street! But I'm discouraged. What can I do? I had hoped to get away
from this life somehow; and now it seems as though I would have to
go deeper into it than ever before—and after seeing what has
happened to poor Sara! Oh, I can't bear to think of it!"
"Everything is going to come out right for you," Monsalvat said to
her gently. "What little I have is yours. Don't thank me. No, I shall
be angry if you do. It isn't mine after all. No one is the owner of
money. It's stupid to think so! Don't lose heart. I'm going to take
care of all your troubles. No, it isn't only for you I am doing this. It's
for myself, you see."
Monsalvat had received his salary that afternoon. He had just paid
his rent; and he gave all that remained to Julieta, who let herself be
persuaded finally to accept it. Then he left the girls alone.
As he went out of the door, he found, leaning against the wall
opposite, his legs crossed, an ill-favored individual who looked at
him with an impudent and sinister smile. Nacha could not endure
this cross-eyed and thoroughly unprepossessing loafer, who, it was
rumored, was a police spy. His small, close-set eyes, low forehead,
crushed-in nose and vicious expression all suggested the jail bird;
and Nacha could never see him without having a ghastly vision of all
the crimes a man is capable of; nor was she alone in fearing him.
But she learned on a certain occasion that he knew her past life, and
the discovery kept her awake many nights. As the days passed
without her hearing anything more from him, she put aside the
worst of her fears. The man watched Monsalvat closely and even
followed him on the street.
When Monsalvat returned to Nacha's room, Julieta had gone. Nacha
was depressed and this he naturally attributed to Julieta's trouble.
But Nacha was tormented by various concerns of her own, of which
she never spoke. One was her wretched poverty. To eke out her
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like