Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Magnus Lie Hetland Python Algorithms Mastering Basic Algorithms in the Python Language, Notas de estudo de Engenharia Elétrica

Magnus Lie Hetland Python Algorithms Mastering Basic Algorithms in the Python Language

Tipologia: Notas de estudo

2016

Compartilhado em 21/10/2016

heitor-galvao-12
heitor-galvao-12 🇧🇷

4.6

(317)

384 documentos

1 / 337

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.5" 336 page count 692 ppi
Hetland
Python Algorithms
THE EXPERT’S VOICE® IN OPEN SOURCE
Python
Algorithms
Mastering Basic Algorithms in the
Python Language
CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Magnus Lie Hetland
Companion
eBook
Available
Learn to implement classic algorithms and design
new problem-solving algorithms using Python
BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
Magnus Lie Hetland,
Author of
Beginning Python: From
Novice to Professional,
Second Edition
Shelve in:
Programming / Python
User level:
Intermediate – Advanced
THE APRESS ROADMAP
Pro
Python
Python
Algorithms
Beginning
Python Visualization
Beginning Python,
Second Edition
THE APRESS ROADMAP
Pro
Python
Python
Algorithms
Beginning
Python Visualization
Beginning Python,
Second Edition
www.apress.com
SOURCE CODE ONLINE
Companion eBook
See last page for details
on $10 eBook version
ISBN 978-1-4302-3237-7
9781430 232377
54999
Python Algorithms: Mastering Basic
Algorithms in the Python Language
Dear Reader,
Python Algorithms explains the Python approach to algorithm analysis and
design. Written by Magnus Lie Hetland, author of Beginning Python, this book
is sharply focused on classical algorithms, but also gives a solid understanding
of fundamental algorithmic problem-solving techniques.
Python Algorithms deals with some of the most important and challenging
areas of programming and computer science in a highly pedagogic and read-
able manner. It covers both algorithmic theory and programming practice,
demonstrating how theory is reflected in real Python programs.
Python Algorithms explains well-known algorithms and data structures built
into the Python language, and shows you how to implement and evaluate others.
You’ll learn how to:
•Transformnewproblemstowell-knownalgorithmicproblemswithefficient
solutions, or formally show that a solution is unfeasible.
•AnalyzealgorithmsandPythonprogramsbothusingmathematicaltoolsand
basic experiments and benchmarks.
•Provecorrectness,optimality,orboundsonapproximationerrorforPython
programs and their underlying algorithms.
•Understandseveralclassicalalgorithmsanddatastructuresindepth,andlearn
to implement these efficiently in Python.
•Designandimplementnewalgorithmsfornewproblems,usingtime-tested
design principles and techniques.
Whether youre a Python programmer who needs to learn about algorithmic prob-
lem-solving, or a student of Computer Science, this book will help you to under-
stand and implement classic algorithms, and it will help you create new ones.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Magnus Lie Hetland Python Algorithms Mastering Basic Algorithms in the Python Language e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

THE EXPERT’S VOICE®^ IN OPEN SOURCE

Python

Algorithms

Mastering Basic Algorithms in the

Python Language

Magnus Lie Hetland

Learn to implement classic algorithms and design new problem-solving algorithms using Python

Python Algorithms: Mastering Basic Algorithms in the Python Language Copyright © 2010 by Magnus Lie Hetland 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. ISBN-13 (pbk): 978-1-4302-3237- ISBN-13 (electronic): 978-1-4302-3238- Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Frank Pohlmann Development Editor: Douglas Pundick Technical Reviewer: Alex Martelli Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Compositor: Mary Sudul Indexer: Brenda Miller Artist: April Milne Cover Designer: Anna Ishchenko Photo Credit: Kai T. Dragland Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 this work. The source code for this book is available to readers at www.apress.com

For my students. May your quest for knowledge be richly rewarded.

■ CONTENTS

v

Contents at a Glance

Contents...................................................................................................................vi About the Author ...................................................................................................xiii About the Technical Reviewer ............................................................................... xiv Acknowledgments .................................................................................................. xv Preface .................................................................................................................. xvi ■ Chapter 1: Introduction........................................................................................ ■ Chapter 2: The Basics .......................................................................................... ■ Chapter 3: Counting 101 .................................................................................... ■ Chapter 4: Induction and Recursion … and Reduction...................................... ■ Chapter 5: Traversal: The Skeleton Key of Algorithmics ................................. ■ Chapter 6: Divide, Combine, and Conquer........................................................ ■ Chapter 7: Greed Is Good? Prove It!................................................................. ■ Chapter 8: Tangled Dependencies and Memoization ....................................... ■ Chapter 9: From A to B with Edsger and Friends............................................. ■ Chapter 10: Matchings, Cuts, and Flows ......................................................... ■ Chapter 11: Hard Problems and (Limited) Sloppiness ..................................... ■ Appendix A: Pedal to the Metal: Accelerating Python ..................................... ■ Appendix B: List of Problems and Algorithms ................................................. ■ Appendix C: Graph Terminology....................................................................... ■ Appendix D: Hints for Exercises....................................................................... ■ Index ................................................................................................................

■ CONTENTS

vi

Contents

Contents at a Glance.................................................................................................v About the Author ...................................................................................................xiii About the Technical Reviewer ............................................................................... xiv Acknowledgments .................................................................................................. xv Preface .................................................................................................................. xvi ■ Chapter 1: Introduction........................................................................................ What’s All This, Then? ..................................................................................................... Why Are You Here? .......................................................................................................... Some Prerequisites ......................................................................................................... What’s in This Book ......................................................................................................... Summary ......................................................................................................................... If You’re Curious … ......................................................................................................... Exercises ......................................................................................................................... References....................................................................................................................... ■ Chapter 2: The Basics .......................................................................................... Some Core Ideas in Computing ....................................................................................... Asymptotic Notation ...................................................................................................... It’s Greek to Me! ................................................................................................................................... Rules of the Road ................................................................................................................................. Taking the Asymptotics for a Spin........................................................................................................ Three Important Cases ......................................................................................................................... Empirical Evaluation of Algorithms.......................................................................................................

■ CONTENTS

■ CONTENTS

■ CONTENTS

■ CONTENTS

  • Implementing Graphs and Trees.................................................................................... vii
    • Adjacency Lists and the Like ................................................................................................................
    • Adjacency Matrices ..............................................................................................................................
    • Implementing Trees..............................................................................................................................
    • A Multitude of Representations ............................................................................................................
  • Beware of Black Boxes..................................................................................................
    • Hidden Squares ....................................................................................................................................
    • The Trouble with Floats ........................................................................................................................
  • Summary .......................................................................................................................
  • If You’re Curious … .......................................................................................................
  • Exercises .......................................................................................................................
  • References.....................................................................................................................
  • ■ Chapter 3: Counting 101 ....................................................................................
    • The Skinny on Sums ......................................................................................................
      • More Greek ...........................................................................................................................................
      • Working with Sums ..............................................................................................................................
    • A Tale of Two Tournaments ...........................................................................................
      • Shaking Hands......................................................................................................................................
      • The Hare and the Tortoise ....................................................................................................................
    • Subsets, Permutations, and Combinations....................................................................
    • Recursion and Recurrences...........................................................................................
      • Doing It by Hand ...................................................................................................................................
      • A Few Important Examples...................................................................................................................
      • Guessing and Checking ........................................................................................................................
      • The Master Theorem: A Cookie-Cutter Solution ...................................................................................
    • So What Was All That About? ........................................................................................
    • Summary .......................................................................................................................
    • If You’re Curious … .......................................................................................................
    • Exercises .......................................................................................................................
    • References..................................................................................................................... viii
  • ■ Chapter 4: Induction and Recursion … and Reduction......................................
    • Oh, That’s Easy!.............................................................................................................
    • One, Two, Many .............................................................................................................
    • Mirror, Mirror .................................................................................................................
    • Designing with Induction (and Recursion) .....................................................................
      • Finding a Maximum Permutation .........................................................................................................
      • The Celebrity Problem ..........................................................................................................................
      • Topological Sorting...............................................................................................................................
    • Stronger Assumptions ...................................................................................................
    • Invariants and Correctness............................................................................................
    • Relaxation and Gradual Improvement............................................................................
    • Reduction + Contraposition = Hardness Proof ..............................................................
    • Problem Solving Advice .................................................................................................
    • Summary .......................................................................................................................
    • If You’re Curious … .......................................................................................................
    • Exercises .......................................................................................................................
    • References.....................................................................................................................
  • ■ Chapter 5: Traversal: The Skeleton Key of Algorithmics .................................
    • A Walk in the Park .......................................................................................................
      • No Cycles Allowed ..............................................................................................................................
      • How to Stop Walking in Circles ..........................................................................................................
    • Go Deep! ......................................................................................................................
      • Depth-First Timestamps and Topological Sorting (Again) ..................................................................
    • Infinite Mazes and Shortest (Unweighted) Paths.........................................................
    • Strongly Connected Components ................................................................................
    • Summary .....................................................................................................................
    • If You’re Curious … ..................................................................................................... ix
    • Exercises .....................................................................................................................
    • References...................................................................................................................
  • ■ Chapter 6: Divide, Combine, and Conquer........................................................
    • Tree-Shaped Problems: All About the Balance............................................................
    • The Canonical D&C Algorithm......................................................................................
    • Searching by Halves ....................................................................................................
      • Traversing Search Trees … with Pruning ..........................................................................................
      • Selection.............................................................................................................................................
    • Sorting by Halves.........................................................................................................
      • How Fast Can We Sort? ......................................................................................................................
    • Three More Examples ..................................................................................................
      • Closest Pair.........................................................................................................................................
      • Convex Hull.........................................................................................................................................
      • Greatest Slice .....................................................................................................................................
    • Tree Balance … and Balancing...................................................................................
    • Summary .....................................................................................................................
    • If You’re Curious … .....................................................................................................
    • Exercises .....................................................................................................................
    • References...................................................................................................................
  • ■ Chapter 7: Greed Is Good? Prove It!.................................................................
    • Staying Safe, Step by Step ..........................................................................................
    • The Knapsack Problem ................................................................................................
      • Fractional Knapsack ...........................................................................................................................
      • Integer Knapsack................................................................................................................................
    • Huffman’s Algorithm....................................................................................................
      • The Algorithm .....................................................................................................................................
      • The First Greedy Choice......................................................................................................................
      • Going the Rest of the Way x
      • Optimal Merging
    • Minimum spanning trees.
      • The Shortest Edge
      • What About the Rest?
      • Kruskal’s Algorithm
      • Prim’s Algorithm...................................................................................................................................
    • Greed Works. But When?.
      • Keeping Up with the Best
      • No Worse Than Perfect.........................................................................................................................
      • Staying Safe
    • Summary
    • If You’re Curious …
    • Exercises
    • References...................................................................................................................
  • ■ Chapter 8: Tangled Dependencies and Memoization .......................................
    • Don’t Repeat Yourself
    • Shortest Paths in Directed Acyclic Graphs
    • Longest Increasing Subsequence.
    • Sequence Comparison.
    • The Knapsack Strikes Back
    • Binary Sequence Partitioning
    • Summary
    • If You’re Curious …
    • Exercises
    • References...................................................................................................................
  • ■ Chapter 9: From A to B with Edsger and Friends............................................. xi
    • Propagating Knowledge...............................................................................................
    • Relaxing like Crazy ......................................................................................................
    • Finding the Hidden DAG...............................................................................................
    • All Against All...............................................................................................................
    • Far-Fetched Subproblems ...........................................................................................
    • Meeting in the Middle..................................................................................................
    • Knowing Where You’re Going ......................................................................................
    • Summary .....................................................................................................................
    • If You’re Curious … .....................................................................................................
    • Exercises .....................................................................................................................
    • References...................................................................................................................
  • ■ Chapter 10: Matchings, Cuts, and Flows .........................................................
    • Bipartite Matching .......................................................................................................
    • Disjoint Paths...............................................................................................................
    • Maximum Flow ............................................................................................................
    • Minimum Cut ...............................................................................................................
    • Cheapest Flow and the Assignment Problem ..............................................................
    • Some Applications .......................................................................................................
    • Summary .....................................................................................................................
    • If You’re Curious … .....................................................................................................
    • Exercises .....................................................................................................................
    • References...................................................................................................................
  • ■ Chapter 11: Hard Problems and (Limited) Sloppiness .....................................
    • Reduction Redux..........................................................................................................
    • Not in Kansas Anymore?..............................................................................................
    • Meanwhile, Back in Kansas …....................................................................................
    • But Where Do You Start? And Where Do You Go from There?..................................... xii
    • A Ménagerie of Monsters.............................................................................................
      • Return of the Knapsack ......................................................................................................................
      • Cliques and Colorings.........................................................................................................................
      • Paths and Circuits ..............................................................................................................................
    • When the Going Gets Tough, the Smart Get Sloppy.....................................................
    • Desperately Seeking Solutions ....................................................................................
    • And the Moral of the Story Is …..................................................................................
    • Summary .....................................................................................................................
    • If You’re Curious … .....................................................................................................
    • Exercises .....................................................................................................................
    • References...................................................................................................................
  • ■ Appendix A: Pedal to the Metal: Accelerating Python .....................................
  • ■ Appendix B: List of Problems and Algorithms .................................................
  • ■ Appendix C: Graph Terminology.......................................................................
  • ■ Appendix D: Hints for Exercises.......................................................................
  • Index.....................................................................................................................

■ CONTENTS

xiv

About the Technical Reviewer

Alex Martelli was born and grew up in Italy and holds a Laurea in Ingeneria Elettronica from the Universitá di Bologna. He wrote Python in a Nutshell and coedited the Python Cookbook. He’s a member of the PSF and won the 2002 Activators’ Choice Award and the 2006 Frank Willison Award for contributions tothe Python community. He currently lives in California and works as senior staff engineer for Google. His detailed profile is at www.google.com/profiles/aleaxit; a summary bio is at http://en.wikipedia.org/wiki/Alex_Martelli.

■ INTRODUCTION

xv

Acknowledgments

Thanks to everyone who contributed to this book, either directly or indirectly. This certainly includes my algorithm mentors, Arne Halaas and Bjørn Olstad, as well as the entire crew at Apress and my brilliant tech editor, Alex. Thanks to Nils Grimsmo, Jon Marius Venstad, Ole Edsberg, Rolv Seehuus, and JorgRødsjø for useful input; to my parents, Kjersti Lie and Tor M. Hetland, and my sister, Anne Lie-Hetland, for their interest and support; and to my uncle Axel, for checking my French. Finally, a big thank-you to the Python Software Foundation for their permission to reproduce parts of the Python standard library and to Randall Munroe for letting me include some of his wonderful XKCD comics.

C H A P T E R 1

1

Introduction

_1. Write down the problem.

  1. Think real hard.
  2. Write down the solution._ “The Feynman Algorithm” as described by Murray Gell-Mann

Consider the following problem. You are to visit all the cities, towns, and villages of, say, Sweden and then return to your starting point. This might take a while (there are 24 978 locations to visit, after all), soyou want to minimize your route. You plan on visiting each location exactly once, following the shortest route possible. As a programmer, you certainly don’t want to plot the route by hand. Rather, you try to write some code that will plan your trip for you. For some reason, however, you can’t seem to get it right. A straightforward program works well for a smaller number of towns and cities but seems to run foreveron the actual problem, and improving the program turns out to be surprisingly hard. How come? Actually, in 2004, a team of five researchers 1 found such a tour of Sweden, after a number of other research teams had tried and failed. The five-man team used cutting-edge software with lots of clever optimizations and tricks of the trade, running on a cluster of 96 Xeon 2.6 GHz workstations. Theirsoftware ran from March 2003 until May 2004, before it finally printed out the optimal solution. Taking various interruptions into account, the team estimated that the total CPU time spent was about 85 years! Consider a similar problem: You want to get from Kashgar, in the westernmost regions of China, to Ningbo, on the east coast, following the shortest route possible. Now, China has 3 583 715 km ofroadways and 77 834 km of railways, with millions of intersections to consider and a virtually unfathomable number of possible routes to follow. It might seem that this problem is related to the previous one, yet this shortest path problem is one solved routinely, with no appreciable delay, by GPS software and online map services. If you give those two cities to your favorite map service, you shouldget the shortest route in mere moments. What’s going on here? You will learn more about both of these problems later in the book; the first one is called the traveling salesman (or salesrep ) problem and is covered in Chapter 11, while so-called shortest path problems are primarily dealt with in Chapter 9. I also hope you will gain a rather deep insight into whyone problem seems like such a hard nut to crack while the other admits several well-known, efficient solutions. More importantly, you will learn something about how to deal with algorithmic and computational problems in general, either solving them efficiently, using one of the several techniques and algorithms you encounter in this book, or showing that they are too hard and that approximatesolutions may be all you can hope for. This chapter briefly describes what the book is about—what you

(^1) David Applegate, Robert Bixby, Vašek Chvátal, William Cook, and Keld Helsgaun

CHAPTER 1 ■ INTRODUCTION

2

can expect and what is expected of you. It also outlines the specific contents of the various chapters to come in case you want to skip around.

What’s All This, Then? This is a book about algorithmic problem solving for Python programmers. Just like books on, say,object-oriented patterns, the problems it deals with are of a general nature—as are the solutions. Your task as an algorist will, in many cases, be more than simply to implement or execute an existing algorithm, as you would, for example, in solving an algebra problem. Instead, you are expected to come up with new algorithms—new general solutions to hitherto unseen, general problems. In this book, you are going to learn principles for constructing such solutions.This may not be your typical algorithm book, though. Most of the authoritative books on the subject (such as the Knuth’s classics or the industry-standard textbook by Cormen et al.) have a heavy formal and theoretical slant, even though some of them (such as the one by Kleinberg and Tardos) lean more inthe direction of readability. Instead of trying to replace any of these excellent books, I’d like to supplement them. Building on my experience from teaching algorithms, I try to explain as clearly as possible how the algorithms work and what common principles underlie many of them. For a programmer, these explanations are probably enough. Chances are you’ll be able to understand why thealgorithms are correct and how to adapt them to new problems you may come to face. If, however, you need the full depth of the more formalistic and encyclopedic textbooks, I hope the foundation you get in this book will help you understand the theorems and proofs you encounter there. kind, where theThere is another genre of algorithm books as well: the “(Data Structures and) Algorithms in blank is the author’s favorite programming language. There are quite a few of these^ blank ” (especially for blank = Java, it seems), but many of them focus on relatively basic data structures, to the detriment of the more meaty stuff. This is understandable if the book is designed to be used in a basic course on data structures, for example, but for a Python programmer, learning about singly and doublylinked lists may not be all that exciting (although you will hear a bit about those in the next chapter). And even though techniques such as hashing are highly important, you get hash tables for free in the form of Python dictionaries; there’s no need to implement them from scratch. Instead, I focus on more high- level algorithms. Many important concepts that are available as black-box implementations either in thePython language itself or in the standard library (such as sorting, searching, and hashing) are explained more briefly, in special “black box” sidebars throughout the text. There is, of course, another factor that separates this book from those in the “Algorithms in Java/C/C++/C#” genre, namely, that thelanguage-independent books (such as those by Knuth, blank is Python. This places the book one step closer to the 2 Cormen et al., and Kleinberg and Tardos, for example), which often use pseudocode , the kind of fake programming language that is designed to be readable rather than executable. One of Python’s distinguishing features is its readability; it is, more or less, executable pseudocode. Even if you’ve never programmed in Python, you could probably decipherthe meaning of most basic Python programs. The code in this book is designed to be readable exactly in this fashion—you need not be a Python expert to understand the examples (although you might need to look up some built-in functions and the like). And if you want to pretend the examples are actually pseudocode, feel free to do so. To sum up …

(^2) Knuth is also well-known for using assembly code for an abstract computer of his own design.