ebook img

Problem Solving with Algorithms and Data Structures Using Python. Release 3.0 PDF

241 Pages·4.918 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Problem Solving with Algorithms and Data Structures Using Python. Release 3.0

Problem Solving with Algorithms and Data Structures Release 3.0 Brad Miller, David Ranum September 22, 2013 CONTENTS 1 Introduction 3 1.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 GettingStarted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 WhatIsComputerScience? . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 ReviewofBasicPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.6 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.7 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2 AlgorithmAnalysis 41 2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.2 WhatIsAlgorithmAnalysis? . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.3 PerformanceofPythonDataStructures . . . . . . . . . . . . . . . . . . . . . 52 2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.5 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.6 DiscussionQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.7 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3 BasicDataStructures 61 3.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.2 WhatAreLinearStructures? . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.3 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.4 TheStackAbstractDataType . . . . . . . . . . . . . . . . . . . . . . . . . . 64 3.5 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.6 Deques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 3.7 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 3.8 TheUnorderedListAbstractDataType . . . . . . . . . . . . . . . . . . . . . 98 3.9 ImplementinganUnorderedList: LinkedLists . . . . . . . . . . . . . . . . . 98 3.10 TheOrderedListAbstractDataType . . . . . . . . . . . . . . . . . . . . . . 108 3.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 3.12 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 3.13 DiscussionQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 3.14 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 4 Recursion 117 4.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 4.2 WhatisRecursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 i 4.3 StackFrames: ImplementingRecursion . . . . . . . . . . . . . . . . . . . . . 123 4.4 VisualisingRecursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 4.5 ComplexRecursiveProblems . . . . . . . . . . . . . . . . . . . . . . . . . . 133 4.6 ExploringaMaze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 4.8 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 4.9 DiscussionQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 4.10 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 5 SortingandSearching 147 5.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.2 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.3 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 5.5 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 5.6 DiscussionQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 5.7 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 6 TreesandTreeAlgorithms 185 6.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 6.2 ExamplesOfTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 6.3 VocabularyandDefinitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.4 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 6.5 PriorityQueueswithBinaryHeaps . . . . . . . . . . . . . . . . . . . . . . . 198 6.6 BinaryTreeApplications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 6.7 TreeTraversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 6.8 BinarySearchTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 6.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 6.10 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 6.11 DiscussionQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 6.12 ProgrammingExercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 7 JSON 235 7.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 7.2 WhatisJSON? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 7.3 TheJSONSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 ii Problem Solving with Algorithms and Data Structures, Release 3.0 CONTENTS 1 Problem Solving with Algorithms and Data Structures, Release 3.0 2 CONTENTS CHAPTER ONE INTRODUCTION 1.1 Objectives • Toreviewtheideasofcomputerscience,programming,andproblem-solving. • Tounderstandabstractionandtheroleitplaysintheproblem-solvingprocess. • Tounderstandandimplementthenotionofanabstractdatatype. • ToreviewthePythonprogramminglanguage. 1.2 Getting Started Thewaywethinkaboutprogramminghasundergonemanychangesintheyearssincethefirst electronic computers required patch cables and switches to convey instructions from human to machine. As is the case with many aspects of society, changes in computing technology providecomputerscientistswithagrowingnumberoftoolsandplatformsonwhichtopractice their craft. Advances such as faster processors, high-speed networks, and large memory ca- pacities have created a spiral of complexity through which computer scientists must navigate. Throughout all of this rapid evolution, a number of basic principles have remained constant. Thescienceofcomputingisconcernedwithusingcomputerstosolveproblems. You have no doubt spent considerable time learning the basics of problem-solving and hope- fullyfeelconfidentinyourabilitytotakeaproblemstatementanddevelopasolution. Youhave also learned that writing computer programs is often hard. The complexity of large problems and the corresponding complexity of the solutions can tend to overshadow the fundamental ideasrelatedtotheproblem-solvingprocess. Thischapteremphasizestwoimportantareasfortherestofthetext. First,itreviewstheframe- work within which computer science and the study of algorithms and data structures must fit, in particular, the reasons why we need to study these topics and how understanding these top- ics helps us to become better problem solvers. Second, we review the Python programming language. Althoughwecannotprovideadetailed,exhaustivereference,wewillgiveexamples and explanations for the basic constructs and ideas that will occur throughout the remaining chapters. 3 Problem Solving with Algorithms and Data Structures, Release 3.0 1.3 What Is Computer Science? Computer science is often difficult to define. This is probably due to the unfortunate use of the word “computer” in the name. As you are perhaps aware, computer science is not simply the study of computers. Although computers play an important supporting role as a tool in the discipline,theyarejustthat–tools. Computer science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process. Given a problem, a computer scientist’s goal is to develop an algorithm,astep-by-steplistofinstructionsforsolvinganyinstanceoftheproblemthatmight arise. Algorithms are finite processes that if followed will solve the problem. Algorithms are solutions. Computersciencecanbethoughtofasthestudyofalgorithms. However,wemustbecarefulto include the fact that some problems may not have a solution. Although proving this statement is beyond the scope of this text, the fact that some problems cannot be solved is important for those who study computer science. We can fully define computer science, then, by including both types of problems and stating that computer science is the study of solutions to problems aswellasthestudyofproblemswithnosolutions. It is also very common to include the word computable when describing problems and solu- tions. We say that a problem is computable if an algorithm exists for solving it. An alternative definition for computer science, then, is to say that computer science is the study of problems that are and that are not computable, the study of the existence and the nonexistence of algo- rithms. In any case, you will note that the word “computer” did not come up at all. Solutions areconsideredindependentfromthemachine. Computer science, as it pertains to the problem-solving process itself, is also the study of abstraction. Abstraction allows us to view the problem and solution in such a way as to separate the so-called logical and physical perspectives. The basic idea is familiar to us in a commonexample. Consider the automobile that you may have driven to school or work today. As a driver, a user ofthecar,youhavecertaininteractionsthattakeplaceinordertoutilizethecarforitsintended purpose. You get in, insert the key, start the car, shift, brake, accelerate, and steer in order to drive. Fromanabstractionpointofview,wecansaythatyouareseeingthelogicalperspective oftheautomobile. Youareusingthefunctionsprovidedbythecardesignersforthepurposeof transporting you from one location to another. These functions are sometimes also referred to astheinterface. On the other hand, the mechanic who must repair your automobile takes a very different point of view. She not only knows how to drive but must know all of the details necessary to carry out all the functions that we take for granted. She needs to understand how the engine works, how the transmission shifts gears, how temperature is controlled, and so on. This is known as thephysicalperspective,thedetailsthattakeplace“underthehood.” The same thing happens when we use computers. Most people use computers to write docu- ments,sendandreceiveemail,surftheweb,playmusic,storeimages,andplaygameswithout anyknowledgeofthedetailsthattakeplacetoallowthosetypesofapplicationstowork. They viewcomputersfromalogicaloruserperspective. Computerscientists,programmers,technol- ogy support staff, and system administrators take a very different view of the computer. They 4 Chapter 1. Introduction Problem Solving with Algorithms and Data Structures, Release 3.0 Figure1.1: ProceduralAbstraction must know the details of how operating systems work, how network protocols are configured, andhowtocodevariousscriptsthatcontrolfunction. Theymustbeabletocontrolthelow-level detailsthatausersimplyassumes. The common point for both of these examples is that the user of the abstraction, sometimes also called the client, does not need to know the details as long as the user is aware of the way the interface works. This interface is the way we as users communicate with the underlying complexities of the implementation. As another example of abstraction, consider the Python mathmodule. Onceweimportthemodule,wecanperformcomputationssuchas >>> import math >>> math.sqrt(16) 4.0 >>> This is an example of procedural abstraction. We do not necessarily know how the square root is being calculated, but we know what the function is called and how to use it. If we perform the import correctly, we can assume that the function will provide us with the correct results. Weknowthatsomeoneimplementedasolutiontothesquarerootproblembutweonly need to know how to use it. This is sometimes referred to as a “black box” view of a process. We simply describe the interface: the name of the function, what is needed (the parameters), andwhatwillbereturned. Thedetailsarehiddeninside(seeFigure1.1). 1.3.1 What Is Programming? Programming is the process of taking an algorithm and encoding it into a notation, a pro- gramming language, so that it can be executed by a computer. Although many programming languages and many different types of computers exist, the important first step is the need to havethesolution. Withoutanalgorithmtherecanbenoprogram. Computer science is not the study of programming. Programming, however, is an important part of what a computer scientist does. Programming is often the way that we create a repre- sentation for our solutions. Therefore, this language representation and the process of creating itbecomesafundamentalpartofthediscipline. Algorithms describe the solution to a problem in terms of the data needed to represent the problem instance and the set of steps necessary to produce the intended result. Programming languages must provide a notational way to represent both the process and the data. To this end,languagesprovidecontrolconstructsanddatatypes. 1.3. What Is Computer Science? 5

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.