(This page has no text content)
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.” — RICHARD 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.” — ANTTI 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 1 2 3 4 5 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; info@nostarch.com 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 info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com.
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.
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 computer interaction (HCI). Her research focuses on identifying challenges faced by students in computing and utilizing HCI principles to design interventions that improve their learning experience.
About the First Edition Technical Reviewer Larry Yueli Zhang is an assistant professor of computer science in the Department of Electrical Engineering and Computer Science at York University’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 analysis, all underscored by a passion for computing education. Larry holds a PhD in computer science from the University of Toronto.
BRIEF CONTENTS Foreword Acknowledgments Introduction Chapter 1: Hash Tables Chapter 2: Trees and Recursion Chapter 3: Memoization and Dynamic Programming Chapter 4: Advanced Memoization and Dynamic Programming Chapter 5: Graphs and Breadth-First Search Chapter 6: Shortest Paths in Weighted Graphs Chapter 7: Binary Search Chapter 8: Heaps and Segment Trees Chapter 9: Union-Find Chapter 10: Randomization
Afterword Appendix A: Algorithm Runtime Appendix B: Because I Can’t Resist Appendix C: Problem Credits Index
CONTENTS IN DETAIL FOREWORD ACKNOWLEDGMENTS INTRODUCTION What We’ll Do New to the Second Edition Who This Book Is For Our Programming Language Why Use C? Static Keyword Include Files Freeing Memory Topic Selection Programming Judges
Anatomy of a Problem Description Starter Problem: Food Lines The Problem Solving the Problem Online Resources Notes 1 HASH TABLES Problem 1: Unique Snowflakes The Problem Simplifying the Problem Solving the Core Problem Solution 1: Pairwise Comparisons Solution 2: Doing Less Work Hash Tables
Hash Table Design Why Use Hash Tables? Problem 2: Login Mayhem The Problem Solution 1: Looking at All Passwords Solution 2: Using a Hash Table Problem 3: Spelling Check The Problem Thinking About Hash Tables An Ad Hoc Solution Summary Notes 2 TREES AND RECURSION Problem 1: Halloween Haul
The Problem Binary Trees Solving the Sample Instance Representing Binary Trees Collecting All the Candy A Completely Different Solution Walking the Minimum Number of Streets Reading the Input Why Use Recursion? Problem 2: Descendant Distance The Problem Reading the Input Number of Descendants from One Node Number of Descendants from All Nodes Sorting Nodes
Outputting the Information The main Function Summary Notes 3 MEMOIZATION AND DYNAMIC PROGRAMMING Problem 1: Burger Fervor The Problem Forming a Plan Characterizing Optimal Solutions Solution 1: Recursion Solution 2: Memoization Solution 3: Dynamic Programming Memoization and Dynamic Programming Step 1: Structure of Optimal Solutions
Step 2: Recursive Solution Step 3: Memoization Step 4: Dynamic Programming Problem 2: Moneygrubbers The Problem Characterizing Optimal Solutions Solution 1: Recursion The main Function Solution 2: Memoization Problem 3: Hockey Rivalry The Problem About Rivalries Characterizing Optimal Solutions Solution 1: Recursion Solution 2: Memoization
Solution 3: Dynamic Programming A Space Optimization Summary Notes 4 ADVANCED MEMOIZATION AND DYNAMIC PROGRAMMING Problem 1: The Jumper The Problem Working Through an Example Solution 1: Backward Formulation Solution 2: Forward Formulation Problem 2: Ways to Build The Problem Working Through an Example Solution 1: Using “Exactly” Subproblems
Comments 0
Loading comments...
Reply to Comment
Edit Comment