ebook img

Problem Solving with Algorithms and Data Structures PDF

240 Pages·2014·4.16 MB·English
by  
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

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 Problem Solving with Algorithms and Data Structures, Release 3.0 Control constructs allow algorithmic steps to be represented in a convenient yet unambiguous way. Ataminimum,algorithmsrequireconstructsthatperformsequentialprocessing,selection fordecision-making,anditerationforrepetitivecontrol. Aslongasthelanguageprovidesthese basicstatements,itcanbeusedforalgorithmrepresentation. Alldataitemsinthecomputerarerepresentedasstringsofbinarydigits. Inordertogivethese strings meaning, we need to have data types. Data types provide an interpretation for this binary data so that we can think about the data in terms that make sense with respect to the problembeingsolved. Theselow-level,built-indatatypes(sometimescalledtheprimitivedata types)providethebuildingblocksforalgorithmdevelopment. For example, most programming languages provide a data type for integers. Strings of binary digits in the computer’s memory can be interpreted as integers and given the typical meanings thatwecommonlyassociatewithintegers(e.g. 23,654,and−19). Inaddition,adatatypealso provides a description of the operations that the data items can participate in. With integers, operations such as addition, subtraction, and multiplication are common. We have come to expectthatnumerictypesofdatacanparticipateinthesearithmeticoperations. The difficulty that often arises for us is the fact that problems and their solutions are very complex. These simple, language-provided constructs and data types, although certainly suf- ficient to represent complex solutions, are typically at a disadvantage as we work through the problem-solvingprocess. Weneedwaystocontrolthiscomplexityandassistwiththecreation ofsolutions. 1.3.2 Why Study Data Structures and Abstract Data Types? To manage the complexity of problems and the problem-solving process, computer scientists use abstractions to allow them to focus on the “big picture” without getting lost in the details. By creating models of the problem domain, we are able to utilize a better and more efficient problem-solving process. These models allow us to describe the data that our algorithms will manipulateinamuchmoreconsistentwaywithrespecttotheproblemitself. Earlier, we referred to procedural abstraction as a process that hides the details of a particular functiontoallowtheuserorclienttoviewitataveryhighlevel. Wenowturnourattentiontoa similar idea, that of data abstraction. An abstract data type, sometimes called an ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented. This means that we are concerned only with what the data is representing and not with how it will eventually be constructed. By providing this level of abstraction,wearecreatinganencapsulationaroundthedata. Theideaisthatbyencapsulating the details of the implementation, we are hiding them from the user’s view. This is called informationhiding. Figure 1.2 shows a picture of what an abstract data type is and how it operates. The user interacts with the interface, using the operations that have been specified by the abstract data type. The abstract data type is the shell that the user interacts with. The implementation is hiddenoneleveldeeper. Theuserisnotconcernedwiththedetailsoftheimplementation. Theimplementationofanabstractdatatype,oftenreferredtoasadatastructure,willrequire that we provide a physical view of the data using some collection of programming constructs and primitive data types. As we discussed earlier, the separation of these two perspectives will 6 Chapter 1. Introduction

Description:
Problem Solving with Algorithms and Data Structures, Release 3.0. CONTENTS. 1 To review the Python programming language that we provide a physical view of the data using some collection of programming constructs.
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.