67% found this document useful (3 votes)
54 views63 pages

[FREE PDF sample] Algorithmic Thinking. Learn Algorithms...Your Coding Skills 2ed 2024 Zingaro D. ebooks

Coding

Uploaded by

gullualcek
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
67% found this document useful (3 votes)
54 views63 pages

[FREE PDF sample] Algorithmic Thinking. Learn Algorithms...Your Coding Skills 2ed 2024 Zingaro D. ebooks

Coding

Uploaded by

gullualcek
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/ 63

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

Algorithmic Thinking. Learn Algorithms...Your


Coding Skills 2ed 2024 Zingaro D.

https://textbookfull.com/product/algorithmic-thinking-learn-
algorithms-your-coding-skills-2ed-2024-zingaro-d/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Algorithmic Thinking 2nd Edition A Problem Based


Introduction Daniel Zingaro

https://textbookfull.com/product/algorithmic-thinking-2nd-edition-a-
problem-based-introduction-daniel-zingaro/

textboxfull.com

Competitive Programming in Python: 128 Algorithms to


Develop your Coding Skills 1st Edition Christoph Dürr

https://textbookfull.com/product/competitive-programming-in-
python-128-algorithms-to-develop-your-coding-skills-1st-edition-
christoph-durr/
textboxfull.com

PHP and Algorithmic Thinking for the Complete Beginner 2nd


Edition Learn to Think Like a Programmer Bouras

https://textbookfull.com/product/php-and-algorithmic-thinking-for-the-
complete-beginner-2nd-edition-learn-to-think-like-a-programmer-bouras/

textboxfull.com

Learning functional data structures and algorithms learn


functional data structures and algorithms for your
applications and bring their benefits to your work now
Khot
https://textbookfull.com/product/learning-functional-data-structures-
and-algorithms-learn-functional-data-structures-and-algorithms-for-
your-applications-and-bring-their-benefits-to-your-work-now-khot/
textboxfull.com
Generative Art with JavaScript and SVG: Utilizing Scalable
Vector Graphics and Algorithms for Creative Coding and
Design (Design Thinking) 1st Edition David Matthew
https://textbookfull.com/product/generative-art-with-javascript-and-
svg-utilizing-scalable-vector-graphics-and-algorithms-for-creative-
coding-and-design-design-thinking-1st-edition-david-matthew/
textboxfull.com

Learn JavaScript with p5.js: Coding for Visual Learners


Arslan

https://textbookfull.com/product/learn-javascript-with-p5-js-coding-
for-visual-learners-arslan/

textboxfull.com

Learning Functional Data Structures and Algorithms Learn


functional data structures and algorithms for your
applications and bring their benefits to your work now 1st
Edition Atul S. Khot
https://textbookfull.com/product/learning-functional-data-structures-
and-algorithms-learn-functional-data-structures-and-algorithms-for-
your-applications-and-bring-their-benefits-to-your-work-now-1st-
edition-atul-s-khot/
textboxfull.com

Algorithmic Governance And Governance Of Algorithms: Legal


And Ethical Challenges Martin Ebers

https://textbookfull.com/product/algorithmic-governance-and-
governance-of-algorithms-legal-and-ethical-challenges-martin-ebers/

textboxfull.com

Learn Python Visually Creative Coding with Processing py


1st Edition Tristan Bunn

https://textbookfull.com/product/learn-python-visually-creative-
coding-with-processing-py-1st-edition-tristan-bunn/

textboxfull.com
PRAISE FOR
ALGORITHMIC THINKING, 2ND EDITION

“Algorithmic Thinking will empower you–whether you’re looking to get a leg up


on technical interviews, enter the world of competitive programming, or just
want to sharpen your skills.”
—josh lospinoso, phd, author of
c++ crash course

“This book . . . is by far the quickest way to get hands-on experience with
algorithms, and is also a great supplement to more theoretical expositions.”
—r ichard peng, associate professor
at the university of waterloo’s
cheriton school of computer
science

“Algorithmic Thinking provides the theoretical background and detailed


problem explanations required to stay ahead of our human and robotic
competitors.”
—duncan smith, senior software
engineer at microsoft

“Not only does Algorithmic Thinking guide readers on how to approach


tackling problems, but Zingaro also helps them understand why these
approaches work.”
—sushant sachdeva, phd,
algorithms professor at
the university of toronto

“The step-by-step solution explanations are so detailed, it feels like Zingaro


is directly teaching us, his readers. This second edition is a worthy update to
an already excellent text.”
—dr steven halim, senior
lecturer at national
university of singapore

“Algorithmic Thinking discusses many interesting problems from programming


contests and presents useful techniques that are not often included in
algorithm textbooks.”
—a ntti laaksonen,
university of helsinki
ALGORITHMIC
THINKING
2ND EDITION

Learn Algorithms to Level up


Your Coding Skills

by Daniel Zingaro

San Francisco
ALGORITHMIC THINKING, 2ND EDITION. Copyright © 2024 by Daniel Zingaro.

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the
prior written permission of the copyright owner and the publisher.

First printing

27 26 25 24 23 12345

ISBN-13: 978-1-7185-0322-9 (print)


ISBN-13: 978-1-7185-0323-6 (ebook)

® Published by No Starch Press®, Inc.


245 8th Street, San Francisco, CA 94103
phone: +1.415.863.9900
www.nostarch.com; [email protected]

Publisher: William Pollock


Managing Editor: Jill Franklin
Production Manager: Sabrina Plomitallo-González
Production Editor: Sydney Cromwell
Developmental Editor: Alex Freed
Cover Illustrator: Rob Gale
Interior Design: Octopod Studios
Technical Reviewers: Naaz Sibia and Larry Yueli Zhang
Copyeditor: George Hale
Proofreader: Elizabeth Littrell

The Library of Congress has catalogued the first edition as follows:

Name: Zingaro, Daniel, author.


Title: Algorithmic thinking : a problem-based introduction / by Daniel Zingaro.
Includes bibliographical references and index.
Identifiers: LCCN 2020031510 (print) | LCCN 2020031511 (ebook) | ISBN 9781718500808 (paperback) |
ISBN 1718500807 (paperback) | ISBN 9781718500815 (ebook)
Subjects: LCSH: Computer algorithms--Problems, exercises, etc. | Computer programming--Problems, exercises, etc.
Classification: LCC QA76.9.A43 Z56 2020 (print) | LCC QA76.9.A43 (ebook) | DDC 005.13--dc23
LC record available at https://lccn.loc.gov/2020031510
LC ebook record available at https://lccn.loc.gov/2020031511

For customer service inquiries, please contact [email protected]. For information on distribution, bulk sales,
corporate sales, or translations: [email protected]. For permission to translate this work: [email protected].
To report counterfeit copies or piracy: [email protected].

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and
company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark
symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been
taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the
information contained in it.
To Doyali
About the Author
Dr. Daniel Zingaro is an associate teaching professor of computer science
and award-winning teacher at the University of Toronto. His main area of
research is computer science education, where he studies how students learn
computer science material.

About the Technical Reviewer


Naaz Sibia is an MSc student of computer science at the University of Toronto
with an interest in computer science education research and human com-
puter interaction (HCI). Her research focuses on identifying challenges
faced by students in computing and utilizing HCI principles to design in-
terventions that improve their learning experience.

About the First Edition Technical Reviewer


Larry Yueli Zhang is an assistant professor of computer science in the De-
partment of Electrical Engineering and Computer Science at York Univer-
sity’s Lassonde School of Engineering. His teaching and research span a
wide range of topics, including introductory programming, algorithms, data
structures, operating systems, computer networks, and social network anal-
ysis, all underscored by a passion for computing education. Larry holds a
PhD in computer science from the University of Toronto.
BRIEF CONTENTS

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

Chapter 1: Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Chapter 2: Trees and Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Chapter 3: Memoization and Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Chapter 4: Advanced Memoization and Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . 125
Chapter 5: Graphs and Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter 6: Shortest Paths in Weighted Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 7: Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Chapter 8: Heaps and Segment Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Chapter 9: Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Chapter 10: Randomization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Afterword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

Appendix A: Algorithm Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405


Appendix B: Because I Can’t Resist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Appendix C: Problem Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
CONTENTS IN DETAIL

FOREWORD xix

ACKNOWLEDGMENTS xxi

INTRODUCTION xxiii
What We’ll Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
New to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Our Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Why Use C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Static Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Freeing Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Topic Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Programming Judges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii
Anatomy of a Problem Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
Starter Problem: Food Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
Solving the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv

1
HASH TABLES 1
Problem 1: Unique Snowflakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Simplifying the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Solving the Core Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Solution 1: Pairwise Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Solution 2: Doing Less Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Hash Table Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Why Use Hash Tables? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Problem 2: Login Mayhem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Solution 1: Looking at All Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Solution 2: Using a Hash Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Problem 3: Spelling Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Thinking About Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
An Ad Hoc Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

2
TREES AND RECURSION 37
Problem 1: Halloween Haul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Solving the Sample Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Representing Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Collecting All the Candy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
A Completely Different Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Walking the Minimum Number of Streets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Reading the Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Why Use Recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Problem 2: Descendant Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Reading the Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Number of Descendants from One Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Number of Descendants from All Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Sorting Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Outputting the Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

3
MEMOIZATION AND DYNAMIC PROGRAMMING 77
Problem 1: Burger Fervor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Forming a Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Characterizing Optimal Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Solution 1: Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Solution 2: Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Solution 3: Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Memoization and Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Step 1: Structure of Optimal Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Step 2: Recursive Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Step 3: Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Step 4: Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

xii Contents in Detail


Problem 2: Moneygrubbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Characterizing Optimal Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Solution 1: Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Solution 2: Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Problem 3: Hockey Rivalry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
About Rivalries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Characterizing Optimal Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Solution 1: Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Solution 2: Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Solution 3: Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
A Space Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

4
ADVANCED MEMOIZATION AND DYNAMIC PROGRAMMING 125
Problem 1: The Jumper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Working Through an Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Solution 1: Backward Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Solution 2: Forward Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Problem 2: Ways to Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Working Through an Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Solution 1: Using “Exactly” Subproblems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Solution 2: Adding More Subproblems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

5
GRAPHS AND BREADTH-FIRST SEARCH 151
Problem 1: Knight Chase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Moving Optimally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Best Knight Outcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Knight Flip-Flop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
A Time Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Graphs and BFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
What Are Graphs? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Graphs vs. Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
BFS on Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Graphs vs. Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Contents in Detail xiii


Problem 2: Rope Climb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Solution 1: Finding the Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Solution 2: A Remodel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Problem 3: Book Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Reading the Language Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Building the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
The BFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
The Total Cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

6
SHORTEST PATHS IN WEIGHTED GRAPHS 197
Problem 1: Mice Maze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Moving On from BFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Finding Shortest Paths in Weighted Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Building the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Implementing Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Two Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Runtime of Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Negative-Weight Edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Problem 2: Grandma Planner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Adjacency Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Building the Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Working Through a Weird Test Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Task 1: Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Task 2: Number of Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

7
BINARY SEARCH 231
Problem 1: Feeding Ants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
A New Flavor of Tree Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Reading the Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Testing Feasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Searching for a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

xiv Contents in Detail


Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Runtime of Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Determining Feasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Searching a Sorted Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Problem 2: River Jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
A Greedy Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Testing Feasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Searching for a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Reading the Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Problem 3: Living Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Sorting Every Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Using Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Testing Feasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
A Quicker Way to Test Feasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Problem 4: Cave Doors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Solving a Subtask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Using Linear Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Using Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

8
HEAPS AND SEGMENT TREES 277
Problem 1: Supermarket Promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Solution 1: Maximum and Minimum in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Max-Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Min-Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Solution 2: Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Two More Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Choosing a Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Problem 2: Building Treaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Recursively Outputting Treaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Sorting by Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Solution 1: Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Range Maximum Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Segment Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Solution 2: Segment Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Segment Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

Contents in Detail xv
Problem 3: Two Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Filling the Segment Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Querying the Segment Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Updating the Segment Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

9
UNION-FIND 331
Problem 1: Social Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Modeling as a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Solution 1: BFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Solution 2: Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Optimization 1: Union by Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Optimization 2: Path Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Relationships: Three Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Choosing Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Problem 2: Friends and Enemies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Augmenting Union-Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Find and Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
SetFriends and SetEnemies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
AreFriends and AreEnemies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Problem 3: Drawer Chore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Equivalent Drawers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Find and Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

10
RANDOMIZATION 375
Problem 1: Yokan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Randomly Choosing a Piece . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

xvi Contents in Detail


Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Determining Number of Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Guessing Flavors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
How Many Attempts Do We Need? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Filling the Flavor Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Randomization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Monte Carlo Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Las Vegas Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Deterministic vs. Randomized Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Problem 2: Caps and Bottles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Solving a Subtask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Solution 1: Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Solution 2: Adding Randomization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Implementing Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Worst-Case and Expected Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

AFTERWORD 403

A
ALGORITHM RUNTIME 405
The Case for Timing . . . and Something Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Big O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Linear Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Constant Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Another Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Quadratic Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Big O in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

B
BECAUSE I CAN’T RESIST 411
Unique Snowflakes: Implicit Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Burger Fervor: Reconstructing a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Knight Chase: Encoding Moves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Dijkstra’s Algorithm: Using a Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Mice Maze: Tracing with Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Mice Maze: Implementation with Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

Contents in Detail xvii


Compressing Path Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Step 1: No More Ternary If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Step 2: Cleaner Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Step 3: Understand the Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Caps and Bottles: In-Place Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

C
PROBLEM CREDITS 427

INDEX 431

xviii Contents in Detail


FOREWORD

For the novice tennis player, keeping the ball in the court is hard enough
(especially from the backhand side). Only after months of practice, once
the basic rallying skills have been mastered, does the sport and its addic-
tive nature begin to reveal itself. You add to your repertoire more advanced
tools—a slice backhand, a kick serve, a drop volley. You strategize at a higher
level of abstraction—serve and volley, chip and charge, hug the baseline. You
develop intuition for which tools and strategies will be most effective against
different types of opponents—there’s no silver-bullet approach that works
well against everyone.
Programming is like tennis. For the beginning coder, coaxing the com-
puter to understand what you want it to do—to execute your solution to a
problem—is hard enough. Graduate from this white-belt level and the true
problem-solving fun begins: How do you come up with the solution in the
first place? While there’s no silver-bullet approach that solves every com-
putational problem efficiently, there are enduringly useful, advanced tools
and strategies: hash tables, search trees, recursion, memoization, dynamic
programming, graph search, and more. And to the trained eye, many prob-
lems and algorithms offer dead giveaways as to which tools are the right
ones. Does your algorithm perform repeated lookups or minimum com-
putations? Speed it up with a hash table or min-heap, respectively! Can you
build a solution to your problem from solutions to smaller subproblems?
Use recursion! Do the subproblems overlap? Speed up your algorithm with
memoization!
Be it tennis or programming, you can’t up your game to the next level
without two things: practice and a good coach. To this end, I give you
Algorithmic Thinking: Learn Algorithms to Level up Your Coding Skills and
Daniel Zingaro. This book teaches all the concepts that I’ve mentioned, but
it is no mere laundry list. With Zingaro as a tutor by your side, you’ll learn,
through practice on challenging competition problems, a repeatable process
for figuring out and deftly applying the right algorithmic tools for the job.
And you’ll learn it all from a book that exudes clarity, humor, and just the
right dose of Canadian pride. Happy problem-solving!

Tim Roughgarden
New York, NY
May 2020

xx Foreword
ACKNOWLEDGMENTS

What an idyllic experience it was to work with the folks at No Starch Press.
They’re all laser-focused on writing books to help readers learn. I’ve found
my people! Liz Chadwick supported my book from the start (and unsup-
ported another one—I’m grateful for that!). It is a gift to have worked with
Alex Freed, my developmental editor. She’s patient, kind, and always eager
to help me improve how I write instead of just fixing my mistakes. I thank all
those involved in the production of the second edition of this book, includ-
ing my copyeditor, George Hale; production editor, Sydney Cromwell; and
cover designer, Rob Gale.
I thank the University of Toronto for offering me the time and space to
write. I thank Naaz Sibia, my technical reviewer, for her careful review of the
manuscript and for finding the time to help me.
I thank Tim Roughgarden for authoring the book’s foreword. Tim’s
books and videos are examples of the kind of clarity that we need to strive
for when teaching algorithms.
I thank my colleagues Larry Zhang, Jan Vahrenhold, and Mahika
Phutane for their review of draft chapters.
I thank everyone who contributed to the problems that I used in this
book and to competitive programming in general. I thank the DMOJ ad-
ministrators for their support of my work. Special thanks to Tudor Brindus,
Radu Pogonariu, and Maxwell Cruickshanks for their help in improving and
adding problems.
I thank my parents for handling everything—everything. All they asked
me to do was learn. I thank Doyali, my partner, for giving some of our time
to this book and for modeling the care it takes to write.
Finally, I thank all of you for reading this book and wanting to learn.
INTRODUCTION

I’m assuming that you’ve learned to use


a programming language such as C, C++,
Java, or Python . . . and I’m hoping that
you’re hooked. It’s hard to explain to nonpro-
grammers why solving problems through program-
ming is so rewarding and fun.
I’m also hoping that you’re ready to take your programming skill to the
next level. I have the privilege of helping you do that.

What We’ll Do
I could start by teaching you some fancy new techniques, telling you why
they’re useful, and comparing them to other fancy techniques, but I won’t.
That material would lay inert, holding on for a little, waiting for the opportu-
nity to spring into action—if in fact some opportunity ever did present itself.
Instead, what I do throughout this book is pose problems: hard prob-
lems. These are problems that I hope you cannot solve, problems that I
hope stymie your current approaches. You’re a programmer. You want
to solve problems. Now it’s time for learning those fancy techniques. This
book is all about posing hard problems and then solving them by bridging
between what you know and what you need.
You won’t see traditional textbook problems here. You won’t find an op-
timal way to multiply a chain of matrices or compute Fibonacci numbers. I
promise: you won’t solve the Towers of Hanoi puzzle. There are many excel-
lent textbooks out there that do these things, but I suspect that many people
are not motivated by those kinds of puzzles.
My approach is to use new problems that you haven’t seen before. Each
year, thousands of people participate in programming competitions, and
these competitions require new problems to measure what participants can
come up with on their own rather than who can google the fastest. These
problems are fascinating, riffing on the classics while adding twists and con-
text to challenge people to find new solutions. There is a seemingly endless
stream of programming and computing knowledge encompassed by these
problems. We can learn as much as we like by choosing the right problems.
Let’s start with some basics. A data structure is a way to organize data so
that desirable operations are fast. An algorithm is a sequence of steps that
solves a problem. Sometimes we can make fast algorithms without using
sophisticated data structures; other times, the right data structure can
offer a significant speed boost. My goal is not to turn you into a competi-
tive programmer, though I’d take that as a happy side benefit. Rather, my
goal is to teach you data structures and algorithms using problems from the
competitive programming world—and to have fun while doing so. You can
reach me at [email protected]. Email me if you have learned. Email
me if you have laughed.

New to the Second Edition


I’ve thoroughly enjoyed the opportunity to discuss the first edition of this
book with readers. Their feedback has led to many of the changes and im-
provements in this new edition.
I’ve made small improvements and additions throughout, but here are
the major highlights of what’s new:
Chapter 1 I removed the Compound Words problem, as it can be
solved in ways that did not require a hash table. Instead, now we have
a problem about passwords on a social networking website. I’ve also
streamlined the code in this chapter to help those without a C/C++ pro-
gramming background, and I added more information about the effi-
ciency of hash tables.
Chapter 3 I added more guidance for how to discover the needed sub-
problems when solving a problem with dynamic programming.
Chapter 4 This chapter is completely new, and it focuses on more ad-
vanced uses of memoization and dynamic programming. This was a fre-
quent request of readers, and I’m excited to have been able to add this.
You’ll learn how to look at dynamic programming problems in reverse
(and why you’d want to), how to work with more dimensions in your
subproblem arrays, and how to further optimize your dynamic program-
ming code when it’s just not fast enough.

xxiv Introduction
Chapter 5, previously Chapter 4 I added guidance on how to choose
between using dynamic programming and using a graph.
Chapter 8, previously Chapter 7 I added further discussion of why we
implement heaps as arrays rather than as explicit trees.
Chapter 10 This chapter is completely new and teaches you how to use
randomization, a topic not often covered in books. Randomization is a
technique that can help you design algorithms that are simple and fast.
You’ll use two types of randomization algorithms to solve problems that
would otherwise be very difficult. You’ll also learn what to look for in
a problem when trying to decide whether to use randomization in the
first place.

Who This Book Is For


This book is for any programmer who wants to learn how to solve tough
problems. You’ll learn many data structures and algorithms, their benefits,
the types of problems they can help you solve, and how to implement them.
You’ll be a better programmer after this!
Are you taking a course on data structures and algorithms and getting
your butt kicked by a wall of theorems and proofs? It doesn’t have to be that
way. This book can serve as a companion text for you, helping you get to the
core of what’s going on so that you can write code and solve problems.
Are you looking for an edge in your next coding interview? You need
to be able to compare and contrast different approaches for solving a prob-
lem, choose the best data structure or algorithm, and explain and imple-
ment your solution. You’ll practice all of these skills over and over as you
read this book. Never fear hash tables, recursion, dynamic programming,
trees, graphs, or heaps again!
Are you an independent learner working toward expertise in data struc-
tures and algorithms? Piecing together stuff from all over the internet can
be tiring and lead to knowledge gaps if the right resources are not found.
This book will provide you the solid foundation and consistent presentation
that you need to become an expert.
As explored further in the next section, all code in this book is written
in the C programming language. However, this isn’t a book on learning C.
If your prior programming experience is in C or C++, then jump right in. If
instead you’ve programmed in a language such as Java or Python, I suspect
that you’ll pick up most of what you need by reading, but you may wish to
review some C concepts now or on first encounter. In particular, I’ll use
pointers and dynamic memory allocation, so, no matter what your prior
experience, you might like to brush up on those topics. The best C book I
can recommend is C Programming: A Modern Approach, 2nd edition, by K. N.
King. Even if you’re okay with C, read it anyway. It’s that good and a won-
derful companion any time you get tripped up by C stuff.

Introduction xxv
Our Programming Language
I’ve chosen to use C as the programming language for this book, rather than
some higher-level language such as C++, Java, or Python. I’ll discuss why and
also justify a couple of other C-related decisions I’ve made.

Why Use C?
The primary reason for using C is that I want to teach you data structures
and algorithms from the ground up. When we want a hash table, we’ll build
it ourselves. There will be no reliance on dictionaries or hashmaps or simi-
lar data structures of other languages. When we don’t know the maximum
length of a string, we’ll build an extensible array: we won’t let the language
handle memory allocation for us. I want you to know exactly what’s going
on, with no tricks up my sleeve. Using C helps me toward this goal.
Solving programming problems in C, as we do in this book, is a use-
ful primer should you decide to continue with C++. If you become serious
about competitive programming, then you’ll be happy to know that C++ is
the most popular language used by competitive programmers, thanks to its
rich standard library and ability to generate code that favors speed.

Static Keyword
Regular local variables are stored on what’s called the call stack. On each call
of a function, some of the call stack memory is used to store local variables.
Then, when the function returns, that memory is freed up for other local
variables to use later. The call stack is small, though, and isn’t appropriate
for some of the massive arrays that we’ll meet in this book. Enter the static
keyword. When used on a local variable, it changes the storage duration
from automatic to static, which means that the variable maintains its value
between function calls. As a side effect, these variables are not stored in the
same memory area along with regular local variables, since then their values
would be lost when a function is terminated. Instead, they’re stored in their
own separate segment of memory, where they don’t have to compete with
whatever else might be on the call stack.
One thing to watch out for with this static keyword is that such local
variables are only initialized once! For a quick example, see Listing 1.

int f(void) {
¶ static int x = 5;
printf("%d\n", x);
x++;
}

int main(void) {
f();
f();
f();

xxvi Introduction
return 0;
}

Listing 1: A local variable with a static keyword


I’ve used static on local variable x ¶. Without that, you’d expect 5 to
be printed three times. However, since static is there, you should see this
output instead:

5
6
7

Include Files
To save space, I don’t include the #include lines that should be added to the
start of C programs. You’ll be safe if you include the following:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

Freeing Memory
Unlike Java or Python, C requires the programmer to free all memory that is
manually allocated. The pattern is to allocate memory using malloc, use that
memory, and then free the memory using free.
For two reasons, though, I do not free memory here. First, freeing mem-
ory adds clutter, distracting from the primary teaching purpose of the code.
Second, these programs are not long-lived: your program will run on a few
test cases, and that’s it. The operating system reclaims all of the unfreed
memory on program termination, so there’s nothing to worry about even
if you run a program many times. Of course, not freeing memory is quite ir-
responsible in practice: no one is happy with a program that consumes more
and more memory as it runs. If you’d like to practice freeing memory, you
can add calls of free to the programs presented in this book.

Topic Selection
The fields of data structures and algorithms are too large to be corralled by
one book (or by this one author!). I used three criteria to help me decide
what topics made the cut.
First, I chose topics of broad applicability: each can be used to solve not
only the corresponding problems in the book but many other problems as
well. In each chapter, I focus on at least two problems. I generally use the
first problem to introduce the data structure or algorithm and one of its pro-
totypical uses. The other problems are meant to give a sense of what else

Introduction xxvii
Other documents randomly have
different content
ten. At the present rate of increase it has been estimated that by
1950 no fewer than one-fourth of all marriages will be terminated by
divorce, and if the same condition of affairs should continue until the
end of the twentieth century, one-half of all the marriages would
eventuate in that way. This would indeed be an ominous outlook
were it not that conditions are likely, sooner or later, to undergo a
change. When a social problem becomes very serious, as this one is
now becoming, it is the habit of society to seek out and apply
appropriate remedies.

269. Since its foundation in 1788 the national government has


spent, in round figures, about sixty-seven billion dollars. Of this
entire sum fifty-eight billions have been spent for war, that is, for
maintaining the army and navy, for carrying on the nation’s various
wars, for pensions, and for interest on war debts.

270. Theodore Roosevelt, Fear God and Take Your Own Part (N. Y.,
1915), Ch. I.

271. History is full of examples to support this statement. When


Carthage proved unable to defend herself against Roman
aggression, the victors left not one stone upon another. Look at
Poland, ripped apart during the seventeenth and eighteenth
centuries by her avaricious neighbors and now restored to
nationhood by the armed forces of France, England, Italy, and
America. And what of China today? Are her four hundred million
people happier and more prosperous because they happen to be
citizens of a defenceless country?

272. The War of Independence was won by a volunteer army. On


the conclusion of peace this army was disbanded, but the absence of
a defence force was deemed a serious danger. Accordingly, when the
constitution was framed in 1787, it provided that the new Congress
should have power “to raise and support armies.” During
Washington’s first term a Department of War was established in the
national government and a small regular army was created under
the supervision of this department. The size of this army was not
above five thousand men of all ranks, barely sufficient to keep the
Indian tribes from giving trouble. But the Napoleonic wars in Europe
led Congress to increase its size as a measure of precaution, and
during the War of 1812 an endeavor was made to raise the regular
army, by enlistment, to about 35,000 men. Recruits, however, did
not come readily because the war was unpopular in some parts of
the country, and it therefore became necessary to call out the militia
organizations of the several states. After 1816, when peace was
made, the regular army was greatly reduced, and until 1860 it
remained small with the exception of the years in which the United
States was at war with Mexico. The Civil War necessitated a
considerable expansion of the regular army, but the larger portion of
the fighting force was obtained by calling out the state militia and by
raising regiments of volunteers. When the war was over, Congress
fixed the maximum strength of the regular army at 25,000, and
there it remained until the outbreak of the war with Spain, when it
was more than doubled. In 1916, during the World War, but before
the United States entered the conflict, a further increase to a
maximum of 175,000 was authorized. This figure subsequently rose
to 225,000 but in 1921 it was cut down by Congress to 150,000, at
which point it remains today, although a further reduction is now
being considered.

273. Prior to 1916 the national guard could not be called upon for
service outside the United States, but only for defence against
invasion and for the suppression of internal disturbances. But in
1916 it was provided by the National Defence Act that whenever
Congress authorizes the use of armed forces in addition to the
regular army, the President may draft any or all members of the
national guard into the service of the United States and may employ
them outside American territory.

274. Theodore Roosevelt was serving as Assistant Secretary of the


Navy when the war with Spain began in 1898. He offered to raise a
volunteer cavalry regiment of cowboys from the cattle country and
his offer was accepted by the government. Resigning his position in
the Navy Department he organized this regiment of Rough Riders
and became its lieutenant-colonel. The Rough Riders went to Cuba,
where they gave a good account of themselves.

275. The actual organization and disciplining of the army during


peace, as well as its movements and operations in war, are under
the immediate direction of the General Staff. This body consists of a
Chief of Staff, who is appointed from among the high officers of the
army, and numerous other army officers who are detailed for this
service. The General Staff is so organized that in the event of war
one section of it can take charge of operations in the field while the
other keeps building up the army at home. General Pershing, who
commanded the American Expeditionary Forces in the World War is
now Chief of Staff, his principal assistant being Major-General
Harbord, who commanded the First Army overseas.

276. For minor offences an enlisted man is tried by summary


court-martial before a single officer. For more serious offences a
special court-martial of from three to five officers is convened. If the
offence is very serious, or if the accused person is a commissioned
officer, the trial takes place before a general court-martial of from
five to fifteen officers, who must be, wherever possible, at least of
equal rank with the accused. The verdict, or finding, of the court-
martial, together with its recommendations for punishment in case
of conviction, is transmitted to the officer by whose order the court
was convened. This officer has power to diminish but not to increase
the punishment recommended by the court-martial.

277. There is still another phase of military jurisdiction which must


be distinguished from both military law and martial law. This is called
military government. It may be explained as follows: When any
territory is conquered and held by an invading army it must
obviously be given some temporary form of government. The former
government usually flees and something must be put in its place.
Under such conditions the commander-in-chief of the occupying
force sets up a temporary administration. In 1919, when a portion of
the American Expeditionary Force advanced into German territory
under the terms of the armistice, a military government with its
headquarters at Coblenz was established for the area occupied by
the American troops. A military government may even be set up in
home territory during a civil war or insurrection. After the fall of the
Confederacy military governments were maintained in the South
until the state governments were reconstructed, hence we
commonly speak of the “reconstruction” period. Military government
is always a temporary arrangement, never intended to be
permanent, although it may last for several years. It does not, like
martial law, supplant the ordinary laws of the occupied territory, but
merely means that the occupying army, through its commander-in-
chief, takes over the administration.

278. The beginnings of the American Navy go back to the time of


the Revolutionary War, when a few frigates were placed in service;
but when the war was over these ships were sold and the navy
abolished. In 1794, however, Congress authorized the building of six
new frigates, and four years later a Department of the Navy was
created, with a member of the Cabinet at its head. The number of
vessels increased very slowly and when the War of 1812 began the
United States had only sixteen war vessels, some of them too small
to be of great usefulness. This small navy, nevertheless, gave a good
account of itself during the course of the war at sea. From 1815 to
the outbreak of the Civil War little attention was paid to the
upbuilding of American naval strength, but during the course of this
struggle a great expansion took place. The invention of the iron-clad
Monitor revolutionized naval construction. But when the South had
been subdued the Navy was once more allowed to dwindle and it
was not until after 1885 that the United States again made a serious
attempt to build up a strong naval establishment. Since that date
naval progress has been steady and today the United States navy
ranks second in point of size among the sea forces of the world. By
the terms of the agreement concluded among the chief naval powers
of the world at Washington in 1922 it has been arranged that the
United States, Great Britain, and Japan shall each destroy certain
war vessels now built or in process of building, and that each shall
refrain from building new capital ships (except for purposes of
replacement), during the next ten years. At the end of this period
the navies of the United States and Great Britain will be
approximately equal in strength, while that of Japan will be about
three-fifths as strong. See also p. 577.

279. For the action of the conference with reference to matters in


the Far East, see p. 619.

280. Brigadier-General Mitchell of the United States Army Air


Service, in his testimony before a committee of Congress in 1920,
declared that a few planes could visit New York City and rain down
enough phosgene gas to kill every inhabitant “unless we provide
some means of repelling them.”

281. See the quotations from various military authorities given in


The Next War, by Will Irwin, pp. 46-66.

282. There is a tradition in England that if a person goes into Hyde


Park, London (a large open space in the center of the city), he may
gather a crowd around him and say anything he pleases, subject
only to the chance that he may be roughly handled if his hearers do
not like what he says. For this reason, Hyde Park is sometimes
referred to as the “safety valve” of the English government. Anyone
who has a grievance, real or imaginary, can go there and blow off
steam. Having had his say, without let or hindrance, the speaker
feels better about it. Somewhere in this country we ought to have a
Hyde Park.
We must be careful not to judge the liberties of the citizen and the
severity of a government by what may happen in war-time or in time
of civil insurrection. War inflames popular passion and impels both
the officers of government and the people to do unwise things,
sometimes to violate the laws of the land in the name of patriotism.
An excited nation, like an excited man, is entitled to some allowance.
Nevertheless, it is the duty of all who understand the meaning of
free government to stand firmly against the wrongful curtailment of
personal rights at any time; for the true interests of free government
are never promoted by resort to injustice or oppression.

283. This is a great and fundamental weakness of


international law, that there is no executive authority to
apply it and there are no courts to enforce its rules when
nations disobey. During the World War the rules of
international law were violated on many occasions, for
example, in the use of poison gas, the bombing of hospitals,
the sinking of hospital ships, the forcing of prisoners to labor
on military works, and the illegal detention of neutral ships.
Yet in spite of these violations international law emerged
from the war stronger than it was before. The nations which
violated international law most shamelessly were the ones
that lost the war, and their defeat was due in no small
measure to the resentment which was aroused throughout
the world by reason of these violations.

284. Illustrations are too fresh in everyone’s mind to


require any extended comment. In 1918 President Wilson
took with him to the peace negotiations at Paris no member
of the Senate. He did not keep in touch with the leaders of
the majority party in this body. But in 1921 when President
Harding appointed the four American delegates to the
Washington Conference he named two of them from the
Senate.

285. In addition to regular envoys, it is sometimes


customary for a country to send an unofficial representative
to conduct negotiations informally. During the years before
the United States entered the war, Colonel Edward M. House,
of Texas, was sent to Europe by President Wilson on at least
two occasions in order that certain confidential discussions
might be carried on without using the regular diplomatic
channels. When unofficial representatives are sent in this
way no public announcement is made.

286. Communications between diplomats and their own


governments are not usually sent by mail if the matters dealt
with are of great importance. They are sent by special
couriers or messengers. When diplomatic communications
are sent by telegraph or cable they are transmitted in cipher,
that is, in a secret code of words which no outsider can read.
Nations occasionally get hold of one another’s diplomatic
codes and decipher communications which they are not
supposed to read. For example, the German government in
the spring of 1917, before the United States declared war,
sent a wireless message to its official representative in
Mexico, telling him in substance that if America entered the
war, he was to stir up Mexico against the United States by
promising that when the war was over Mexico would be
rewarded with some American territory. This message was in
secret code; but the American officials caught it from the air,
deciphered it, and at the appropriate time put the German
government in an embarrassing situation by publishing the
message in plain English to the whole world.

287. When two countries go to war they at once withdraw


their diplomatic representatives from one another’s capitals.
The embassy or legation and its archives are put under the
care of some neutral ambassador until the war is over.
During the years 1914-1917 the American ambassador in
Berlin and the American minister in Brussels looked after the
interests of Great Britain at these two capitals. The work of
Mr. Brand Whitlock at Brussels was notable, and the services
which he rendered to the Belgian people during the years of
their country’s captivity will long be remembered in that
heroic little land.
288. In 1915, for example, the Austro-Hungarian
ambassador to the United States, Dr. Dumba, endeavored to
stir up trouble among certain Hungarian immigrants who
were working in American munition factories, making
weapons and military supplies for sale to Great Britain and
France. When the United States government discovered
these intrigues, Dr. Dumba was dismissed from the country.

289. The making of secret treaties continued, in fact, after


the war began. By secret treaties France and Great Britain
promised that Italy should have certain territories which
were held by Austria and that Russia should have
Constantinople. When the war was over the new government
at Vienna permitted the publication of a whole volume of
secret treaties that had been made during the preceding fifty
years. The Bolsheviks in Russia also published all the secret
treaties of the Czar that they could find.
In the covenant of the League of Nations it is provided
that every treaty between nations which become members of
the League must be registered and published.

290. There are some cases in which the approval of the


House of Representatives is also needed before a treaty can
go into effect. In the treaty which provided for the purchase
of Alaska in 1867 and in the treaty which closed the war with
Spain in 1898, provision was made for the payment of
money by the United States. Now no money can be
appropriated from the treasury without action on the part of
the House, and if the House had declined to appropriate the
money, the conditions of these treaties could not have been
fulfilled. In both cases, however, the House did actually vote
the necessary funds.

291. In 1870, for example, President Grant concluded with


the government of San Domingo a treaty which provided for
the annexation of that island to the United States. The
Senate, after a hard fight, rejected the treaty altogether.
Even more notable, of course, was the Senate’s action in
declining to ratify the treaty which President Wilson signed at
Paris in 1919.

292. The English government proposed that the United


States and Great Britain should issue the declaration jointly,
but President Monroe and his secretary of state, John Quincy
Adams, thought it better that the United States should make
the declaration alone.

293. At the Peace Conference in 1919 the European


countries were willing to concede what was virtually a
recognition of the Monroe Doctrine, and the covenant of the
League of Nations contains a provision that nothing in that
document shall affect the validity of “regional
understandings, like the Monroe Doctrine, for securing the
maintenance of peace” (Article XXI).

294. Hiram Bingham, The Monroe Doctrine: An Obsolete


Shibboleth (New Haven, 1913).

295. No one knows exactly what it means today because


its scope has been rather indefinitely extended at various
times. No doubt it would be further extended if the occasion
should arise. For example, the original doctrine was directed
against European powers only. But if Japan should attempt
to acquire territory in Central or South America, the Monroe
Doctrine would unquestionably be invoked as applicable to
an Asiatic power as well.

296. Washington was well aware that the United States


might have to take a hand in European quarrels if they
should assume an extraordinary importance. Notice the exact
wording of the passage in his Farewell Address. “It would be
unwise in us to implicate ourselves by artificial ties in the
ordinary vicissitudes of her (Europe’s) politics, or the ordinary
combinations and collisions of her friendships or enmities.”
Washington was not in the habit of wasting words, and he
did not twice insert the limitation “ordinary” without good
reason. By the way, he did not use the phrase “entangling
alliances”. That expression was first used by Jefferson in his
inaugural address (March 4, 1801).

297. From 1815 to 1914 all the great wars were localized.
The Crimean War (1854-1855), although five nations took
part in it, was confined to the territories around the Black
Sea; the War of 1859, in which the French and Italians on
the one side fought the Austrians on the other, was settled in
Northern Italy. The other important wars were, for the most
part, individual duels between two nations or between two
sections of a single nation.

298. The total amount loaned to European governments


by the United States during the war was about ten billion
dollars, of which nearly half was loaned to Great Britain.

299. The payments made by Germany to Great Britain,


France, and Italy, as well as the payments made by these
countries to the United States, must inevitably take the form
of payment in goods. There is not enough gold in Europe to
make payment in gold. All this means that so long as the
reparations and loans are being liquidated large imports of
goods from Europe are likely to come into this country.

300. By the terms of a supplementary treaty, this does not


include the main Japanese islands themselves.

301. It is said that the Thirty Years’ War reduced the


population in some sections of the warring states to one-half
or one-third of what it had been when the struggle began.
The losses of all the countries engaged in the World War
have been estimated to be almost ten millions, more than
the entire population of Canada from ocean to ocean.
Millions more died from famine and under-nourishment at
home. Is it not strange that nations should work for years
with might and main to increase the size and prosperity of
their populations, then turn around and undo a large part of
what they have been able to accomplish? In peace nations
labor to alleviate each others’ distress; in war they labor to
cause it. Patiently through the decades men of science
wrestle with the problem of relieving pain and suffering;
then, in an instant, all their skill is devoted to killing,
maiming, and suffocating men by the million! There is no
wisdom like the wisdom of man, and no folly like it either.

302. The covenant was made an integral part of the peace


treaty, largely at President Wilson’s insistence, for two
reasons: First, because it was believed that this would be a
surer way of obtaining the assent of all the great nations to
the provisions of the covenant; second, because many of the
terms of the treaty (for example, those relating to
boundaries and mandates) were framed on the assumption
that a League of Nations would be in existence to carry them
into effect. Taken together, the treaty and the covenant make
the longest international document ever framed, a printed
book of 87,000 words—about half the size of this text-book.
Nearly a thousand diplomats, experts, and clerks spent more
than three months in drafting it.

303. Invitations were not extended to Germany, Austria,


Hungary, Bulgaria, Turkey, Russia, or Mexico. Austria,
however, has since been admitted to membership.

304. When, for example, a typhus epidemic broke out in


Poland, and the Polish authorities found themselves unable
to control it, the League sent a commission of health experts
to assist them.
305. This is because not only Great Britain herself but
India, Canada, Australia, South Africa, and New Zealand are
members of the League. It was assumed that the six British
votes in the Assembly would always be cast together; but, as
a matter of fact, the various British dominions insisted upon
having separate votes in order that they might vote
according to their own particular interests. In most
international matters the interests of Canada, Australia, and
South Africa are not at all certain to coincide with those of
England.

306. Since the treaty and the covenant were joined


together, the objections to one applied to the other.
Concerning Shantung, see also p. 620.

307. The term “soviet” means council or meeting. The


constitution of the Russian Socialist Federated Soviet
Republic may be found in Frank Comerford, The New World,
pp. 281-305.

308. Nikalai Lenin is now the head of this Council; Leon


Trotzky is Minister of War in it. Each member of the Council
is head of a department.

309. The breakdown was due in part, no doubt, to the


disorganization wrought by the war and the internal revolts
which broke out in Russia after the war. To make matters
worse there were crop failures, with resulting famines, in
some of Russia’s best grain-producing regions.

310. It is quite true that some men and women work


because they like to work and dislike to be idle, or because
they feel that what they do is of value to the community, or
for some other reason not directly connected with their pay.
They form, however, a very small fraction of the total body of
wage-earners.
311. Voters.

312. Annulled by the Thirteenth and Fourteenth


Amendments.

313. Superseded by the Seventeenth Amendment.

314. See Seventeenth Amendment.

315. See Sixteenth Amendment.

316. Superseded by the Twelfth Amendment.

317. Modified by the Eleventh Amendment.

318. Compare Fourteenth Amendment.

319. The first ten Amendments, known as the Bill of


Rights, were adopted in 1791.

320. See Amendment XIV, Sec. 1, which extends part of


this restriction to the States.

321. Adopted in 1798 to protect the sovereignty of the


States.

322. Adopted in 1804, superseding Article II, Sec. 1.

323. Adopted in 1865.

324. Adopted in 1868.

325. Adopted in 1870.

326. Adopted in 1913.

327. Adopted in 1913.


328. Adopted in 1919.

329. Adopted in 1920.


Transcriber’s Note
The index entry regarding the duties of the Vice
President refers to a note on p. 270, but no such
note exists.
The index entry regarding compulsory
arbitration in New Zealand refers to a note on p.
419. The note appears on p. 409.
Other errors deemed most likely to be the
printer’s have been corrected, and are noted here.
The references are to the page and line in the
original. Where the error is in a footnote, the
original note is included and the line within it.
71.32 protect a[rg/gr]iculture Transposed.
80.2 It wa[s] a long and grim Restored.
struggle
82.34 In the case of the Porto Added.
Ricans[,] citizenship
105.10 They are practical[ in] their Added.
nature.
179.33 and[ and] the county- Removed.
manager plans.
193.7 that i[s/t] encourages Replaced.
250.26 [I/i]t could act only Replaced.
316.13 many states[.] Added.
329.8 restocking of lakes with Added.
fish)[.]
353.33 The Non-Partisan League, Replaced.
pp.269-283[;/.]
379.33 and future development[.] Added.
418.10 a certain p[re/er]centage of Transposed.
the total
465.12 to the present taxpayers[.] Added.
468.n2.2 life of a public Replaced.
i[n/m]provement
470.10 Public Finance, [pp. ]261- Added.
280.
471.11 p. 457.[)] Added.
618.6 mastery of the Pacific[.] Added.
673.29 I do solem[n]ly swear Inserted.
683.6 a Senator or Inserted.
Represen[t]ative
*** END OF THE PROJECT GUTENBERG EBOOK SOCIAL CIVICS ***

Updated editions will replace the previous one—the old editions


will be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States
copyright in these works, so the Foundation (and you!) can copy
and distribute it in the United States without permission and
without paying copyright royalties. Special rules, set forth in the
General Terms of Use part of this license, apply to copying and
distributing Project Gutenberg™ electronic works to protect the
PROJECT GUTENBERG™ concept and trademark. Project
Gutenberg is a registered trademark, and may not be used if
you charge for an eBook, except by following the terms of the
trademark license, including paying royalties for use of the
Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such
as creation of derivative works, reports, performances and
research. Project Gutenberg eBooks may be modified and
printed and given away—you may do practically ANYTHING in
the United States with eBooks not protected by U.S. copyright
law. Redistribution is subject to the trademark license, especially
commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU AGREE
THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT
LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT
EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE
THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person
or entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
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!

textbookfull.com

You might also like