Undergraduate Topics in Computer Science Kent D. Lee Steve Hubbard Data Structures and Algorithms with Python CuuDuongThanCong.com https://fb.com/tailieudientucntt Undergraduate Topics in Computer Science This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Undergraduate Topics in Computer Science (UTiCS) delivers high-quality instructional content for undergraduates studying in all areas of computing and information science From core foundational and theoretical material to final-year topics and applications, UTiCS books take a fresh, concise, and modern approach and are ideal for self-study or for a one- or two-semester course The texts are all authored by established experts in their fields, reviewed by an international advisory board, and contain numerous examples and problems Many include fully worked solutions More information about this series at http://www.springer.com/series/7592 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Kent D Lee Steve Hubbard • Data Structures and Algorithms with Python 123 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Kent D Lee Steve Hubbard Luther College Decorah, IA USA Series editor Ian Mackie Advisory Board Samson Abramsky, University of Oxford, Oxford, UK Karin Breitman, Pontifical Catholic University of Rio de Janeiro, Rio de Janeiro, Brazil Chris Hankin, Imperial College London, London, UK Dexter Kozen, Cornell University, Ithaca, USA Andrew Pitts, University of Cambridge, Cambridge, UK Hanne Riis Nielson, Technical University of Denmark, Kongens Lyngby, Denmark Steven Skiena, Stony Brook University, Stony Brook, USA Iain Stewart, University of Durham, Durham, UK ISSN 1863-7310 ISBN 978-3-319-13071-2 DOI 10.1007/978-3-319-13072-9 ISSN 2197-1781 (electronic) ISBN 978-3-319-13072-9 (eBook) Library of Congress Control Number: 2014953918 Springer Cham Heidelberg New York Dordrecht London © Springer International Publishing Switzerland 2015 This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer Permissions for use may be obtained through RightsLink at the Copyright Clearance Center Violations are liable to prosecution under the respective Copyright Law The use of general descriptive names, registered names, trademarks, service marks, etc in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Preface Thanks for choosing Data Structures and Algorithms with Python This text was written based on classroom notes for two courses, an introductory data structures and algorithms course and an advanced data structures and algorithms course The material contained in this text can be taught in two semesters The early chapters in this text are intended as an introductory text for data structures and algorithms, while the later chapters cover advanced topics that are suitable for the second course in data structures and algorithms The Python language is used throughout the text and some familiarity with Python or some other object-oriented language is assumed However, the first chapter contains a Python primer for those coming from a different language background This text serves well as a follow-on text to Python Programming Fundamentals by Kent D Lee and published by Springer, but does not require you to have read that text In this text the next steps are taken to teach you how to handle large amounts of data efficiently A number of algorithms are introduced and the need for them is motivated through examples that bring meaning to the problems we face as computer programmers An algorithm is a well-defined procedure for accomplishing a task Algorithms are an important part of Computer Science and this text explores many algorithms to give you the background you need when writing programs of your own The goal is that having seen some of the sorts of algorithms presented in this text, you will be able to apply these techniques to other programs you write in the future Another goal of this text is to introduce you to the idea of computational complexity While there are many unique and interesting algorithms that we could explore, it is important to understand that some algorithms are more efficient than others While computers are very good at doing calculations quickly, an inefficient algorithm can make the fastest computer seem very slow or even make it appear to come to a halt This text will show you what can and cannot be computed efficiently The text builds this idea of efficiency from the most basic of facts giving you the tools you will need to determine just how efficient any algorithm is so you can make informed judgements about the programs you write v This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt vi Preface The text assumes that you have some prior experience in computer programming, probably from an introductory programming course where you learned to break simple problems into steps that could be solved by a computer The language you used may have been Python, but not necessarily Python is an excellent language for a text on data structures and algorithms whether you have used it before or not Python is an object-oriented programming language with operator overloading and dynamic typing Whether this is your first exposure to Python or you used it in your first course, you will learn more about the language from this text The first chapter of the text reviews some of the fundamentals of computer programming along with the basic syntax of Python to get you up to speed in the language Then subsequent chapters dive into more advanced topics and should be read in sequence At the beginning of every chapter the goals of the chapter are stated At the end of every chapter is a set of review questions that reinforce the goals of the chapter These review questions are followed in each chapter by a few programming problems that relate to the chapter goals by asking you to use the things you learned in the chapter and apply them to a computer program You can motivate your reading of a chapter by first consulting the review questions and then reading the chapter to answer them Along the way, there are lots of examples to illustrate the concepts being introduced We hope you enjoy the text! If you have any questions or comments please send them to kentdlee@luther.edu Kent D Lee Steve Hubbard This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt For Teachers A typical introductory data structures course covers the first seven chapters of this text Chapter introduces Python programming and the Tkinter module which is used in various places in the text Tkinter comes with Python, so no special libraries need be installed for students to use it Tkinter is used to visualize many of the results in this text Chapter introduces complexity analysis and depending on your needs, some of the material in Chap could be skipped in an introductory data structures course In particular, the material on Θ notation and amortized complexity can be skipped Big-Oh notation is enough for the first seven chapters Typically, Chap is covered lightly and near the end of a semester course It seems there is generally not enough time in a semester to cover graph theory in much detail Advanced courses in data structures and algorithms should start with Chap if students are unfamiliar with Python or Tkinter A brief refresher may not be bad even for those that have programmed using Python before Chapter should be covered in detail including the material on Θ notation and amortized complexity Some review of hashing as it is used in sets and maps in Chap may be good review earlier in the advanced course along with a brief discussion of binary search trees and tree traversals in Chap Depending on your needs, Chap would be a good chapter to cover next including the material on depth first search of a graph Chapter is where the advanced material begins with assumptions made that students understand the concepts presented in the earlier chapters The two introductory chapters along with Chaps 8–12 make a seven-chapter sequence that will fill a semeseter in an advanced course nicely This text is very project oriented Solutions for all projects are available from Kent D Lee You can contact Kent at kentdlee@luther.edu for instructor solutions You must provide proof (through a website or other reference) that you are an instructor at an educational institution to get access to the instructor materials vii This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt viii For Teachers If you have any suggestions or find any errors in the text, please let us know by emailing Kent at kentdlee@luther.edu Thanks and we hope you enjoy using the text in your course! Kent D Lee Steve Hubbard This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Credits Connect Four is referenced in Chaps 4, 12 and Appendix H Connect Four is a trademark of the Milton Bradley Company in the United States and other countries Chapter references Mac OS X Mac and Mac OS are registered trademarks of Apple Inc., registered in the U.S and other countries Microsoft Windows is also referenced in Chap Windows is a registered trademark of Microsoft Corporation in the United Stated and other countries ix This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 20.4 The PlotData Program 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 347 x P l a c e s = max(4−round ( math l o g ( x S i z e , ) ) , ) y P l a c e s = max(4−round ( math l o g ( y S i z e , ) ) , ) l a b e l Y V a l = maxY − ∗ y S i z e s c r e e n s e t w o r l d c o o r d i n a t e s ( minX −0.20 ∗ x S i z e , minY − ∗ y S i z e , \ maxX + ∗ x S i z e , maxY + ∗ y S i z e ) t h eT u r t l e ht ( ) t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e penup ( ) goto ( minX , minY ) pendown ( ) goto ( maxX , minY ) penup ( ) goto ( minX , minY ) pendown ( ) goto ( minX , maxY ) penup ( ) t h e T u r t l e goto ( x C e n t e r , minY − y S i z e ∗ ) t h e T u r t l e w r i t e ( x A x i s L a b e l , a l i g n = " c e n t e r " , f o n t = ( " A r i a l " ,1 , " bold " ) ) t h e T u r t l e goto ( minX , maxY + 0 ∗ y S i z e ) t h e T u r t l e w r i t e ( y A x i s L a b e l , a l i g n = " c e n t e r " , f o n t = ( " A r i a l " ,1 , " bold " ) ) f o r i i n range ( , 1 , ) : x = minX + x S i z e ∗ i / 0 y = minY + y S i z e ∗ i / 0 t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e penup ( ) goto ( x , minY+ y S i z e ∗ 0 ) pendown ( ) goto ( x , minY−y S i z e ∗ 0 ) penup ( ) goto ( x , minY−y S i z e ∗ ) t h e T u r t l e w r i t e ( ( " %1 " + s t r ( x P l a c e s ) + " f " )%x , a l i g n = " c e n t e r " , f o n t = ( " A r i a l " ,1 , " normal " ) ) t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e t h eT u r t l e penup ( ) goto ( minX+ x S i z e ∗ 0 , y ) pendown ( ) goto ( minX−x S i z e ∗ 0 , y ) goto ( minX−x S i z e ∗ 0 , y ) w r i t e ( ( " %1 " + s t r ( y P l a c e s ) + " f " )%y , a l i g n = " r i g h t " , f o n t = ( " A r i a l " ,1 , " normal " ) ) sequences = p l o t E l e m e n t getElementsByTagName ( " Sequence " ) f o r sequence i n sequences : a t t r = sequence a t t r i b u t e s l a b e l = a t t r [ " t i t l e " ] value s t r i p ( ) color = a t t r [ " color " ] value t h eT u r t l e color ( color ) t h e T u r t l e penup ( ) t h e T u r t l e goto ( x C e n t e r , l a b e l Y V a l ) l a b e l Y V a l = l a b e l Y V a l − ∗ y S i z e t h e T u r t l e w r i t e ( l a b e l , a l i g n = " c e n t e r " , f o n t = ( " A r i a l " ,1 , " bold " ) ) d a t a P o i n t s = sequence getElementsByTagName ( " D a t a P o i n t " ) f i r s t = dataPoints [0] attr = f i r s t attributes x = float ( a t t r [ " x " ] value ) y = float ( a t t r [ " y " ] value ) t h e T u r t l e goto ( x , y ) t h e T u r t l e dot ( ) t h e T u r t l e pendown ( ) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 348 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 20 Appendix H: Complete Programs for dataPoint in dataPoints : a t t r = dataPoint a t t r i b u t e s x = float ( a t t r [ " x " ] value ) y = float ( a t t r [ " y " ] value ) t h e T u r t l e goto ( x , y ) t h e T u r t l e dot ( ) s c r e e n update ( ) f i l e M e n u add_command ( l a b e l = " Load P l o t Data " ,command= l o a d F i l e ) f i l e M e n u add_command ( l a b e l = " E x i t " ,command= s e l f m a s t e r q u i t ) b a r add_cascade ( l a b e l = " F i l e " ,menu= f i l e M e n u ) s e l f m a s t e r c o n f i g ( menu= b a r ) c a n v a s = t k i n t e r Canvas ( s e l f , width =1000 , h e i g h t =800) c a n v a s pack ( s i d e = t k i n t e r LEFT ) t h e T u r t l e = t u r t l e R a wT u r t l e ( c a n v a s ) screen = t h eT u r t l e getscreen ( ) screen t r a c e r ( ) i f s e l f d a t a f i l e ! = None : loadFile ( self datafile strip ( ) ) def main ( ) : r o o t = t k i n t e r Tk ( ) d a t a f i l e = None i f len ( s y s argv ) > : d a t a f i l e = sys argv [ ] plotApp = P l o t A p p l i c a t i o n ( r o o t , d a t a f i l e ) plotApp mainloop ( ) p r i n t ( " Program E x e c u t i o n Completed " ) i f name == " main " : main ( ) 20.5 The Tic Tac Toe Application This is the starter code for an exercise in constructing a tic tac toe game This program can be downloaded from the text’s website 10 11 12 13 14 15 16 from t u r t l e import ∗ import t k i n t e r messagebox import t k i n t e r import random import math import d a t e t i m e import time import s y s screenMin = screenMax = 300 Human = −1 Computer = c l a s s Board : # When a board i s c o n s t r u c t e d , you may want t o make a copy o f t h e board This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 20.5 The Tic Tac Toe Application 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 349 # T h i s can be a s h a l l o w copy o f t h e board b e c a u s e T u r t l e o b j e c t s a r e # Immutable from t h e p e r s p e c t i v e o f a board o b j e c t def _ _ i n i t _ _ ( s e l f , board=None , s c r e e n =None ) : s e l f screen = screen i f s c r e e n == None : i f board ! = None : s e l f s c r e e n = board s c r e e n s e l f items = [ ] f o r i i n range ( ) : rowlst = [ ] f o r j i n range ( ) : i f board==None : r o w l s t append (Dummy ( ) ) else : r o w l s t append ( board [ i ] [ j ] ) s e l f i t e m s append ( r o w l s t ) # A c c e s s o r method f o r t h e s c r e e n def g e t s c r e e n ( s e l f ) : return s e l f screen # The g e t i t e m method i s u s e d # r e t u r n a row o f t h e board # a l i s t ) s o a c c e s s i n g a row # board [ row ] [ column ] b e c a u s e def _ _ g e t i t e m _ _ ( s e l f , i n d e x ) : return s e l f items [ index ] t o i n d e x i n t o t h e board I t s h o u l d T h a t row i t s e l f i s i n d e x a b l e ( i t i s j u s t and column i n t h e board can be w r i t t e n o f t h i s method # T h i s method s h o u l d r e t u r n t r u e i f t h e two b o a r d s , s e l f and o t h e r , # r e p r e s e n t e x a c t l y t h e same s t a t e # READER E X E R C I S E : YOU MUST COMPLETE T H I S FUNCTION def eq ( s e l f , o t h e r ) : pass # T h i s method w i l l mutate t h i s board t o c o n t a i n a l l dummy # t u r t l e s T h i s way t h e board can be r e s e t when a new game # i s s e l e c t e d I t s h o u l d NOT be u s e d e x c e p t when s t a r t i n g # a new game def r e s e t ( s e l f ) : s e l f screen t r a c e r ( ) f o r i i n range ( ) : f o r j i n range ( ) : s e l f i t e m s [ i ] [ j ] goto ( −100 ,−100) s e l f i t e m s [ i ] [ j ] = Dummy ( ) s e l f screen t r a c e r ( ) # T h i s method s h o u l d r e t u r n an i n t e g e r r e p r e s e n t i n g t h e # s t a t e o f t h e board I f t h e c o m p u t e r has won , r e t u r n # I f t h e human has won , r e t u r n −1 O t h e r w i s e , r e t u r n # READER E X E R C I S E : YOU MUST COMPLETE T H I S FUNCTION def e v a l ( s e l f ) : pass # T h i s method s h o u l d r e t u r n T r u e i f t h e board # i s c o m p l e t e l y f i l l e d up ( no dummy t u r t l e s ) # Otherwise , i t should r e t u r n F a l s e # READER E X E R C I S E : YOU MUST COMPLETE T H I S FUNCTION def f u l l ( s e l f ) : pass # T h i s method s h o u l d draw t h e X ’ s and O ’ s # Of t h i s board on t h e s c r e e n def drawXOs ( s e l f ) : This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 350 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 20 Appendix H: Complete Programs f o r row i n range ( ) : f o r c o l i n range ( ) : i f s e l f [ row ] [ c o l ] e v a l ( ) ! = : s e l f [ row ] [ c o l ] s t ( ) s e l f [ row ] [ c o l ] goto ( c o l ∗100+50 ,row ∗100+50) s e l f s c r e e n update ( ) # T h i s c l a s s i s j u s t f o r p l a c e h o l d e r o b j e c t s when no move has been made # y e t a t a p o s i t i o n i n t h e board H a v i n g e v a l ( ) r e t u r n i s c o n v e n i e n t when no # move has been made c l a s s Dummy : def _ _ i n i t _ _ ( s e l f ) : pass def e v a l ( s e l f ) : return def goto ( s e l f , x , y ) : pass # # # # # # I n t h e X and O c l a s s e s below t h e c o n s t r u c t o r b e g i n s by i n i t i a l i z i n g t h e R a w T u r t l e p a r t o f t h e o b j e c t w i t h t h e c a l l t o s u p e r ( ) _ _ i n i t _ _ ( c a n v a s ) The s u p e r ( ) c a l l r e t u r n s t h e c l a s s o f t h e s u p e r c l a s s ( t h e c l a s s above t h e X o r O i n t h e c l a s s h i e r a r c h y ) I n t h i s c a s e , t h e s u p e r c l a s s i s R a w T u r t l e Then , c a l l i n g _ _ i n i t _ _ on t h e s u p e r c l a s s i n i t i a l i z e s t h e p a r t o f t h e o b j e c t t h a t i s a R a wT u r t l e c l a s s X ( R a wT u r t l e ) : def _ _ i n i t _ _ ( s e l f , c a n v a s ) : super ( ) _ _ i n i t _ _ ( c a n v a s ) s e l f ht ( ) s e l f g e t s c r e e n ( ) r e g i s t e r _ s h a p e ( " X " ,( ( − , − ) ,( − , − 4 ) ,( , − ) ,( , − 4 ) ,( , − ) , \ ( ,0 ) ,( ,3 ) ,( ,4 ) ,( ,4 ) ,( − ,4 ) ,( − ,3 ) ,( − ,0 ) ,( − , − ) ) ) s e l f shape ( " X " ) s e l f penup ( ) s e l f speed ( ) s e l f goto ( −100 ,−100) def e v a l ( s e l f ) : r e t u r n Computer c l a s s O ( R a wT u r t l e ) : def _ _ i n i t _ _ ( s e l f , c a n v a s ) : super ( ) _ _ i n i t _ _ ( c a n v a s ) s e l f ht ( ) s e l f shape ( " c i r c l e " ) s e l f penup ( ) s e l f speed ( ) s e l f goto ( −100 ,−100) def e v a l ( s e l f ) : r e t u r n Human # The minimax f u n c t i o n i s g i v e n a p l a y e r ( = Computer , −1 = Human ) and a # board o b j e c t When t h e p l a y e r = Computer , minimax r e t u r n s t h e maximum # v a l u e o f a l l p o s s i b l e moves t h a t t h e Computer c o u l d make When t h e p l a y e r = # Human t h e n minimax r e t u r n s t h e minimum v a l u e o f a l l p o s s i b l e moves t h e Human # c o u l d make Minimax w o r k s by a s s u m i n g t h a t a t each move t h e Computer w i l l p i c k # i t s b e s t move and t h e Human w i l l p i c k i t s b e s t move I t d o e s t h i s by making a # move f o r t h e p l a y e r whose t u r n i t i s , and t h e n r e c u r s i v e l y c a l l i n g minimax # The b a s e c a s e r e s u l t s when , g i v e n t h e s t a t e o f t h e board , someone has won o r # t h e board i s f u l l # READER E X E R C I S E : YOU MUST COMPLETE T H I S FUNCTION def minimax ( p l a y e r , board ) : pass This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 20.5 The Tic Tac Toe Application 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 351 c l a s s T i cT a cT o e ( t k i n t e r Frame ) : def _ _ i n i t _ _ ( s e l f , m a s t e r =None ) : super ( ) _ _ i n i t _ _ ( m a s t e r ) s e l f pack ( ) s e l f buildWindow ( ) s e l f paused = F a l s e s e l f stop = F a l s e s e l f running = F a l s e s e l f t u r n = Human s e l f locked = False def buildWindow ( s e l f ) : cv = S c r o l l e d C a n v a s ( s e l f ,6 0 ,6 0 ,6 0 ,6 0 ) cv pack ( s i d e = t k i n t e r LEFT ) t = R a wT u r t l e ( cv ) screen = t getscreen ( ) screen t r a c e r (100000) s c r e e n s e t w o r l d c o o r d i n a t e s ( screenMin , screenMin , screenMax , screenMax ) screen bgcolor ( " white " ) t ht ( ) frame = t k i n t e r Frame ( s e l f ) frame pack ( s i d e = t k i n t e r RIGHT , f i l l = t k i n t e r BOTH ) board = Board ( None , s c r e e n ) def d r a w G r i d ( ) : screen c l e a r ( ) screen t r a c e r (1000000) s c r e e n s e t w o r l d c o o r d i n a t e s ( screenMin , screenMin , screenMax , screenMax ) screen bgcolor ( " white " ) screen t r a c e r ( ) t = R a wT u r t l e ( cv ) t ht ( ) t pu ( ) t width ( ) t c o l o r ( " green " ) f o r i i n range ( ) : t penup ( ) t goto ( i ∗ 0 + 0 ,1 ) t pendown ( ) t goto ( i ∗ 0 + 0 ,2 ) t penup ( ) t goto ( , i ∗100+100) t pendown ( ) t goto ( , i ∗100+100) s c r e e n update ( ) def newGame ( ) : # drawGrid ( ) s e l f t u r n = Human board r e s e t ( ) s e l f locked =False s c r e e n update ( ) def s t a r t H a n d l e r ( ) : newGame ( ) drawGrid ( ) s t a r t B u t t o n = t k i n t e r B u t t o n ( frame , t e x t = " New Game " , command= s t a r t H a n d l e r ) s t a r t B u t t o n pack ( ) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 352 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 20 Appendix H: Complete Programs def q u i t H a n d l e r ( ) : s e l f master q u i t ( ) q u i t B u t t o n = t k i n t e r B u t t o n ( frame , t e x t = " Q u i t " , command= q u i t H a n d l e r ) q u i t B u t t o n pack ( ) def computerTurn ( ) : # The l o c k e d v a r i a b l e p r e v e n t s a n o t h e r e v e n t from b e i n g # p r o c e s s e d w h i l e t h e c o m p u t e r i s making up i t s mind s e l f locked = True # # # # # # C a l l Minimax t o f i n d t h e b e s t move t o make READER E X E R C I S E : YOU MUST COMPLETE T H I S CODE A f t e r w r i t i n g t h i s code , t h e maxMove t u p l e s h o u l d c o n t a i n t h e b e s t move f o r t h e c o m p u t e r F o r i n s t a n c e , i f t h e b e s t move i s i n t h e f i r s t row and t h i r d column t h e n maxMove would be ( , ) row , c o l = maxMove board [ row ] [ c o l ] = X ( cv ) s e l f locked = False def m o u s e C l i c k ( x , y ) : i f not s e l f l o c k e d : row = i n t ( y / / 0 ) col = int ( x / / 100) board [ row ] [ c o l ] = O ( cv ) s e l f t u r n = Computer board drawXOs ( ) i f not board f u l l ( ) and not abs ( board e v a l ( ) ) = = : computerTurn ( ) s e l f t u r n = Human board drawXOs ( ) else : s e l f locked = True i f board e v a l ( ) == : t k i n t e r messagebox showwarning ( " Game Over " , " X wins ! ! ! " ) i f board e v a l ( ) == −1: t k i n t e r messagebox showwarning ( " Game Over " , "O wins How d i d t h a t happen ? " ) i f board f u l l ( ) : t k i n t e r messagebox showwarning ( " Game Over " , " I t was a t i e " ) screen o n c l i c k ( mouseClick ) screen l i s t e n ( ) def main ( ) : r o o t = t k i n t e r Tk ( ) r o o t t i t l e ( " T i c T a c Toe " ) a p p l i c a t i o n = T i cT a cT o e ( r o o t ) a p p l i c a t i o n mainloop ( ) i f name == " main " : main ( ) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 20.6 The Connect Four Front-End 20.6 353 The Connect Four Front-End This provides the GUI front-end to the connect four game presented in the last chapter of the text This can serve as a front-end for a computer opponent back-end This program can be downloaded from the text’s website 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 import import import import import # # # # # # # turtle subprocess tkinter sys time The f o l l o w i n g program w i l l p l a y c o n n e c t f o u r T h i s program and a a n o t h e r program communicate t h r o u g h p i p e s ( b o t h i n p u t and o u t p u t ) a c c o r d i n g t o t h i s a r c h i t e c t u r e When a command i s s e n t i t i s i n d i c a t e d w i t h a r i g h t a r r o w i n d i c a t i n g s o m e t h i n g i s w r i t t e n t o t h e o t h e r program ’ s s t a n d a r d i n p u t When t h e o t h e r program s e n d s s o m e t h i n g t o t h i s P y t h o n Program i t i s i n d i c a t e d w i t h a l e f t a r r o w T h a t means i t i s w r i t t e n t o t h e s t a n d a r d o u t p u t o f t h e o t h e r program # Python Other # −−−−−−−−−−−> # # # # # = and m a t r i x [ row ] [ c o l ] i s v i s i b l e ( ) : This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 356 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 20 Appendix H: Complete Programs row = row − i f row < : # Then we c l i c k e d i n a column t h a t was a l r e a d y f u l l s e l f running = True return v a l = row ∗ + c o l # Do t h e Human T u r n toOther write ( " 2\n " ) toOther f l u s h ( ) toOther write ( s t r ( v a l ) + " \n " ) toOther f l u s h ( ) s t a t u s = fromOther r e a d l i n e ( ) s t r i p ( ) # print ( " Status is " , status ) m a t r i x [ row ] [ c o l ] s e t S h a p e ( Human , s c r e e n ) s c r e e n update ( ) # Check t h e s t a t u s o f t h e game status = checkStatus ( ) i f s t a t u s == : # Do a Computer T u r n ComputerTurn ( ) checkStatus ( ) s e l f running = F a l s e matrix = [ ] f o r i i n range ( ) : row = [ ] f o r j i n range ( ) : t = T i l e ( canvas , i , j , s e l f ) row append ( t ) m a t r i x append ( row ) s c r e e n update ( ) s c r e e n o n c l i c k ( HumanTurn ) s i d e B a r = t k i n t e r Frame ( s e l f , padx = , pady = , r e l i e f = t k i n t e r RAISED , b o r d e r w i d t h = " pt " ) s i d e B a r pack ( s i d e = t k i n t e r RIGHT , f i l l = t k i n t e r BOTH ) def NewGame ( ) : toOther write ( " 0\n " ) toOther f l u s h ( ) s t a t u s = i n t ( fromOther r e a d l i n e ( ) s t r i p ( ) ) f o r row i n m a t r i x : f o r token i n row : token h t ( ) s c r e e n update ( ) kb = t k i n t e r B u t t o n ( s i d e B a r , t e x t = " P a s s " ,command=ComputerTurn ) kb pack ( ) ng = t k i n t e r B u t t o n ( s i d e B a r , t e x t = " New Game " ,command=NewGame ) ng pack ( ) p r o c = s u b p r o c e s s Popen ( [ " c l i s p " , " c4 f a s " ] , s t d o u t = s u b p r o c e s s P I P E , \ s t d i n = s u b p r o c e s s P I P E , u n i v e r s a l _ n e w l i n e s =T r u e ) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 20.6 The Connect Four Front-End 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 357 fromOther = proc stdout toOther = proc s t d i n # # # # To w r i t e t o t h e o t h e r program you s h o u l d u s e commands l i k e t h i s toOther write ( val +"\n " ) Don ’ t f o r g e t t o f l u s h t h e b u f f e r toOther flus h ( ) # To r e a d from t h e o t h e r program you w r i t e # l i n e = fromOther r e a d l i n e ( ) s t r i p ( ) def main ( ) : r o o t = t k i n t e r Tk ( ) animApp = C o n n e c t A p p l i c a t i o n ( r o o t ) animApp mainloop ( ) p r i n t ( " Program E x e c u t i o n Completed " ) i f name == " main " : main ( ) This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Bibliography Adelson-Veskii G, Landis EM (1962) An algorithm for the organization of information Proc USSR Acad Sci 146:263–266 Carlis J, Maguire J (2000) Mastering data modeling: a user-driven approach Addison-Wesley http://www.amazon.com/Mastering-Data-Modeling-User-Driven-Approach/dp/020170045X/ ref=sr_1_1?s=books&ie=UTF8&qid=1404178333&sr=1-1 Coppin B (2004) Artificial intelligence illuminated Jones and Bartlett, USA Dijkstra EW (1959) A note on two problems in connexion with graphs Nume Math 1:269–271 Kruskal JB (1956) On the shortest spanning tree of a graph and the traveling salesman problem Proc Am Math Soc 7:48–50 Lutz M (2013) Learning Python O’Reilly Media http://www.amazon.com/LearningPython-Edition-Mark-Lutz/dp/1449355730/ref=sr_1_1?ie=UTF8&qid=1398871248&sr=8-1 &keywords=learning+python+lutz Tharp A (1988) File Organization and Processing Wiley, New York Wikipedia (2014) Bloom filter Wikipedia.org http://en.wikipedia.org/wiki/Bloom_filter © Springer International Publishing Switzerland 2015 K.D Lee and S Hubbard, Data Structures and Algorithms with Python, Undergraduate Topics in Computer Science, DOI 10.1007/978-3-319-13072-9 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 359 Index A Algorithm, 1, 54–56 recursive, 67, 68, 76 sorting, 54, 56, 82, 100–102 Arithmetic modulo, 83, 206 E Error run-time, syntax, 2, 168 Expression postfix, 166, 167, 169, 180 B Backtrack, 177, 179, 189, 192 Binary, 138, 143, 145, 166, 167 Bipartite, 204 Bloom filter, 205, 206 B-tree, 261 F File, 3, 13–16 Float, 3–6, 8, 50 operators, 301 Function, 2, 3, 10–13 C Canvas, 25–27, 115 Class, 3, 5, Complexity, 41 amortized, 58, 60, 62, 93 Computational complexity, 41, 50, 53, 54 Constructor, 6, 37, 93–95, 120 D Database, 261, 263 Dict operators and methods, 309 Dictionary, 3, 4, 36, 38, 154–156, 159, 183, 237 operators and methods, 309 key, 4, 11, 27, 36, 38 G Grammar, 33, 35, 168, 169 tokens, 128, 129, 138, 168 Graph, 3, 11, 14, 15, 17, 20, 185 bipartite, 204 cycle, 187–189, 191 dijkstra’s algorithm, 185, 186, 197, 198 edge, 186, 187 kruskal’s algorithm, 185, 186, 190, 191 path, 185 vertex, 186–189 weighted, 188, 190, 197, 199 H Hash, 56, 139, 144, 145 Hashtable map, 154–156, 159, 183, 237 © Springer International Publishing Switzerland 2015 K.D Lee and S Hubbard, Data Structures and Algorithms with Python, Undergraduate Topics in Computer Science, DOI 10.1007/978-3-319-13072-9 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt 361 362 Index set, 146–149 Heap, 42, 67, 72, 73, 80, 87, 215 scope, 67, 69, 71 I Ide, 1, 2, Import, 2, 11 Index, 48, 64, 86, 90 Int, operators, 299 Integer, 3, 8, 38, 43, 49 Integrated development environment, 1, 2, Interpreter, 2, 67, 69, 72, 73 Iterator, 97, 150, 176, 183, 240 yield, 97, 150, 165, 176, 237, 239 Q Queue, 91, 125, 131 priority, 138, 199, 215, 235, 236 Quicksort pivot, 109–111 R Relational database, 261, 263, 264 Run-time stack, 2, 42, 67, 68, 72, 73 activation record, 73 O Object, 3–5 Operator, 8, 10, 38 S Scope, 67, 69, 71 Screen operators and methods, 323 Search, 56, 64, 72, 82, 93, 98 best first, 281, 291, 292, 295 breadth first, 185, 190, 202, 281, 284 depth first, 177–179 heuristic, 117, 235, 281 hill climbing, 281, 286–288 linear, 98, 102, 120, 147, 148 Sequence, 5, 13, 26, 35, 91 Sequential, 96, 163, 261, 267, 271 Set, 4, 38, 139, 143 hashset, 146–149 union, 143, 144, 183, 192 Sort merge, 105–107 radix, 131, 132 Sorting algorithm heapsort, 215, 222, 225, 229, 232, 234 merge sort, 105–108 quicksort, 107, 109–111 selection sort, 102–104, 135 Stack, 2, 25, 42, 67, 68, 72 push, 73 String, 4, 5, 10, 34 operators and methods, 303 Syntax, 1, 2, 164–166 P Path, 177, 185, 187–189 Postfix, 166, 167, 169, 180 Prefix, 168 Program T Tic tac toe, 113, 115, 117, 137, 140 Tree, 163, 164 AVL, 237, 239, 240 balance, 237–239 L Lexicographical, 100, 101, 131 List append, 3, 5, 8, 11, 20, 37 array, 59, 62, 206, 207 length, 16, 17, 22, 37, 39, 83 linked, 91, 117, 118, 120, 121 operators and methods, 307 M Map, 139 hashmap, 154–156, 159, 183, 237 Matrix, 113, 114, 135, 140, 151 Memoization, 139, 156–159 Method, 5, Minimax, 116 Module import, 2, 11 Modulo, 83, 206 N Node, 32, 36, 117, 118, 120 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Index 363 binary search, 170, 172–174, 176 b-tree, 176, 179, 261, 269 child, 164, 170, 215 depth, 177–179 leaf, 217, 229, 232, 249, 250 parent, 167, 217 root, 163, 165, 166, 170, 172 rotation, 243–245 spanning, 185, 191–193 splay, 176, 179, 237, 250, 251 Trie, 209 Turtle, 3, 11, 14 methods, 311 Turtle screen operators and methods, 323 Type, 2, 3, This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt ... University of Durham, Durham, UK ISSN 186 3-7 310 ISBN 97 8-3 -3 1 9-1 307 1-2 DOI 10.1007/97 8-3 -3 1 9-1 307 2-9 ISSN 219 7-1 781 (electronic) ISBN 97 8-3 -3 1 9-1 307 2-9 (eBook) Library of Congress Control Number:... 11 B-Trees 11.1 Chapter Goals 11.2 Relational Databases 11.3 B-Tree Organization 11.4 The Advantages of B-Trees 11.5 B-Tree Implementation 11.6 B-Tree... Structures and Algorithms with Python, Undergraduate Topics in Computer Science, DOI 10.1007/97 8-3 -3 1 9-1 307 2-9 _1 This copy belongs to 'acha04' CuuDuongThanCong.com https://fb.com/tailieudientucntt Python