Errata for Problem Solving with Algorithms and Data Structures using PythonNote: Throughout the text, some of the code listings contain a \ (backslash) character. This character is not a typo. It is used as a continuation character so that the line is not broken across physical lines in the text listing. They are necessary due to the maximum line size imposed by the page margins.Chapter 1 - Introduction
Chapter 2 - Stacks and Queues
Chapter 3 - Recursion
Chapter 4 - Analysis, Sorting, and Searching
1 def quickSort(alist): 2 quickSortHelper(alist,0,len(alist)-1) 3 4 def quickSortHelper(alist,first,last): 5 if first<last 6 splitpoint = partition(alist,first,last) 7 quickSortHelper(alist,first,splitpoint-1) 8 quickSortHelper(alist,splitpoint+1,last) 9 10 def partition(alist,first,last): 11 pivotvalue = alist[first] 12 leftmark = first+1 13 rightmark = last 14 15 done = False 16 17 while not done: 18 19 # while leftmark <= rightmark and alist[leftmark] < pivotvalue: 20 while leftmark <= rightmark and alist[leftmark] <= pivotvalue: 21 leftmark = leftmark + 1 22 23 # while alist[rightmark] > pivotvalue and rightmark >= leftmark: 24 while alist[rightmark] >= pivotvalue and rightmark >= leftmark: 25 rightmark = rightmark -1 26 27 if rightmark < leftmark: 28 done = True 29 else: 30 alist[leftmark],alist[rightmark]= alist[rightmark],alist[leftmark] 31 32 alist[first],alist[rightmark]= alist[rightmark],alist[first] 33 34 return rightmark 35 36 37 38 39 40 Chapter 5 - Trees
Page 221: Listing 5.28
1 def delete_key(self,key): 2 if self.key == key: 3 if not (self.leftChild or self.rightChild): # if no children 4 if self == self.parent.leftChild: # if I'm a left child 5 self.parent.leftChild = None 6 else: 7 self.parent.rightChild = None 8 elif (self.leftChild or self.rightChild) and \ 9 (not (self.leftChild and self.rightChild)): # if one child 10 if self.leftChild: # if I have only a left child 11 if self.parent: # if I'm not the root node 12 if self == self.parent.leftChild: 13 self.parent.leftChild = self.leftChild 14 else: 15 self.parent.rightChild = self.leftChild 16 self.leftChild.parent = self.parent 17 else: # if I am the root node, slide up the left child 18 self.key = self.leftChild.key 19 self.payload = self.leftChild.payload 20 self.leftChild = self.leftChild.leftChild 21 self.rightChild = self.leftChild.rightChild 22 if self.leftChild: 23 self.leftChild.parent = self 24 if self.rightChild: 25 self.rightChild.parent = self 26 else: # if I have only a right child 27 if self.parent: # if I'm not the root node 28 if self == self.parent.leftChild: 29 self.parent.leftChild = self.rightChild 30 else: 31 self.parent.rightChild = self.rightChild 32 self.rightChild.parent = self.parent 33 else: # if I am the root node, slide up the right child 34 self.key = self.rightChild.key 35 self.payload = self.rightChild.payload 36 self.leftChild = self.rightChild.leftChild 37 self.rightChild = self.rightChild.rightChild 38 if self.leftChild: 39 self.leftChild.parent = self 40 if self.rightChild: 41 self.rightChild.parent = self 42 else: # if two children, then replace self with successor 43 succ = self.findSuccessor() 44 succ.spliceOut() 45 self.key = succ.key 46 self.payload = succ.payload 47 else: # continue looking 48 if key < self.key: 49 if self.leftChild: 50 self.leftChild.delete_key(key) 51 else: 52 print "trying to remove a non-existant node" 53 else: 54 if self.rightChild: 55 self.rightChild.delete_key(key) 56 else: 57 print "trying to remove a non-existant node" 58
Chapter 6 - Graphs
Page 261: Listing 6.9Although the dfs algorithm in the text works fine, the following recursive version of DFS is more appropriate for use with the topological sort. 00001: # DFS implementation using adjGraph where nodes contain an adj list reference to other nodes 00002: 00003: time = 0 00004: 00005: def dfs(theGraph): 00006: for u in theGraph: 00007: u.setColor('white') 00008: u.setPred(-1) 00009: time = 0 00010: for u in theGraph: 00011: if u.getColor() == 'white': 00012: dfsvisit(u) 00013: 00014: def dfsvisit(u): 00015: global time 00016: u.setColor('gray') 00017: time = time+1 00018: u.setDiscovery(time) 00019: for v in u.getAdj(): 00020: if v.getColor() == 'white': 00021: v.setPred(u) 00022: dfsvisit(v) 00023: u.setColor('black') 00024: time = time + 1 00025: u.setFinish(time) 00026:
Page 276: Listing 6.11
00001: def prim(G,w,start): 00002: PQ = PriorityQueue() 00003: for v in G: 00004: v.setDistance(sys.maxint) 00005: v.setPred(None) 00006: start.setDistance(0) 00007: PQ.buildHeap([(v.getDistance(),v) for v in G]) 00008: while not PQ.isEmpty(): 00009: u = PQ.delMin() 00010: for v in u.getAdj(): 00011: if v in PQ and u.getCost(v) < v.getDistance(): 00012: if v.getPred() != None: 00013: G.findEdge(v,v.getPred()).remove() 00014: v.setPred(u) 00015: v.setDistance(u.getCost(v)) 00016: PQ.decreaseKey(v,u.getCost(v)) Chapter 7 |