Consequently, this book presents the various data structures and algorithms ascomplete C# program fragments.. Chapter introduces the foundational data structures--the array and the linke
Trang 2Copyright © 2004 by Bruno R Preiss
All rights reserved No part of this publication may be reproduced, stored in aretrieval system, or transmitted, in any form or by any means, electronic,mechanical, photocopying, recording, or otherwise, without the prior writtenpermission of the author
This book was prepared with LaTeX and reproduced from camera-ready copysupplied by the author The book is typeset using the Computer Modern fontsdesigned by Donald E Knuth with various additional glyphs designed by theauthor and implemented using METAFONT
Trang 6This paradigm shift is both evolutionary and revolutionary On the one hand, theknowledge base grows incrementally as programmers and researchers inventnew algorithms and data structures On the other hand, the proper use of object-oriented techniques requires a fundamental change in the way the programs aredesigned and implemented Programmers who are well schooled in the
Trang 8The primary goal of this book is to promote object-oriented design using C# and
to illustrate the use of the emerging object-oriented design patterns Experienced
object-oriented programmers find that certain ways of doing things work bestand that these ways occur over and over again The book shows how these
A secondary goal of the book is to present mathematical tools just in time.
Analysis techniques and proofs are presented as needed and in the proper
context In the past when the topics in this book were taught at the graduatelevel, an author could rely on students having the needed background in
mathematics However, because the book is targeted for second- and third-yearstudents, it is necessary to fill in the background as needed To the extent
possible without compromising correctness, the presentation fosters intuitiveunderstanding of the concepts rather than mathematical rigor
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.
Trang 10One cannot learn to program just by reading a book It is a skill that must bedeveloped by practice Nevertheless, the best practitioners study the works ofothers and incorporate their observations into their own practice I firmly believethat after learning the rudiments of program writing, students should be exposed
to examples of complex, yet well-designed program artifacts so that they canlearn about the designing good software
Consequently, this book presents the various data structures and algorithms ascomplete C# program fragments All the program fragments presented in thisbook have been extracted automatically from the source code files of workingand tested programs It has been my experience that by developing the properabstractions, it is possible to present the concepts as fully functional programs
without resorting to pseudo-code or to hand-waving.
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.
Trang 12computer Chapter develops several models and illustrates with examples howthese models predict performance Both average-case and worst-case analyses ofrunning time are considered Recursive algorithms are discussed and it is shownhow to solve a recurrence using repeated substitution This chapter also reviewsarithmetic and geometric series summations, Horner's rule and the properties ofharmonic numbers
Chapter introduces asymptotic (big-oh) notation and shows by comparing withChapter that the results of asymptotic analysis are consistent with models ofhigher fidelity In addition to , this chapter also covers other asymptotic
notations ( , , and ) and develops the asymptotic properties of
polynomials and logarithms
Chapter introduces the foundational data structures the array and the linked
list Virtually all the data structures in the rest of the book can be implementedusing either one of these foundational structures This chapter also covers multi-dimensional arrays and matrices
Chapter deals with abstraction and data types It presents the recurring designpatterns used throughout the text as well a unifying framework for the data
structures presented in the subsequent chapters In particular, all of the data
structures are viewed as abstract containers.
Chapter discusses stacks, queues, and deques This chapter presents
implementations based on both foundational data structures (arrays and linkedlists) Applications for stacks and queues are presented
Trang 13Chapter introduces trees and describes their many forms Both depth-first andbreadth-first tree traversals are presented Completely generic traversal
algorithms based on the use of the visitor design pattern are presented, thereby illustrating the power of algorithmic abstraction This chapter also shows how
trees are used to represent mathematical expressions and illustrates the
relationships between traversals and the various expression notations (prefix,infix, and postfix)
Chapter addresses trees as searchable containers Again, the power of
algorithmic abstraction is demonstrated by showing the relationships between
simple algorithms and balancing algorithms This chapter also presents averagecase performance analyses and illustrates the solution of recurrences by
telescoping
Chapter presents several priority queue implementations, including binaryheaps, leftist heaps, and binomial queues In particular this chapter illustrateshow a more complicated data structure (leftist heap) extends an existing one(tree) Discrete-event simulation is presented as an application of priority
queues
Chapter covers sets and multisets Also covered are partitions and disjoint setalgorithms The latter topic illustrates again the use of algorithmic abstraction
Garbage collection is discussed in Chapter This is a topic that is not foundoften in texts of this sort However, because the C# language relies on garbagecollection, it is important to understand how it works and how it affects the
running times of programs
Chapter force and greedy algorithms, backtracking algorithms (including branch-and-bound), divide-and-conquer algorithms, and dynamic programming An object-
Trang 14Finally, Chapter presents an overview of graphs and graph algorithms Bothdepth-first and breadth-first graph traversals are presented Topological sort isviewed as yet another special kind of traversal Generic traversal algorithms
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.
Trang 16This text may be used in either a one semester or a two semester course Thecourse which I teach at Waterloo is a one-semester course that comprises 36lecture hours on the following topics:
1 Review of the fundamentals of programming in C# and an overview ofobject-oriented programming with C# (Appendix ) [4 lecture hours]
2 Models of the computer, algorithm analysis, and asymptotic notation
(Chapters and ) [4 lecture hours]
3 Foundational data structures, abstraction, and abstract data types (Chapters and ) [4 lecture hours]
4 Stacks, queues, ordered lists, and sorted lists (Chapters and ) [3 lecturehours]
Trang 18Additional material supporting this book can be found on the world-wide web atthe URL:
http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus6
In particular, you will find there the source code for all the program fragments inthis book as well as an errata list
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.
Trang 22VariablesValue Types and Reference TypesParameter Passing
Classes and ObjectsInheritance
Interfaces and PolymorphismOther Features
How This Book Is Organized
Models and Asymptotic AnalysisFoundational Data StructuresAbstract Data Types and the Class HierarchyData Structures
AlgorithmsAlgorithm Analysis
Trang 25Abstract Comparable ObjectsComparison Operators
Wrappers for Value Types
Containers
Abstract ContainersVisitors
The IsDone PropertyAbstract VisitorsThe AbstractContainer Class ToString MethodEnumerable Collections and Enumerators
Enumerators and foreach
Searchable Containers
Abstract Searchable ContainersAssociations
Push and Pop Methods, Top Property
Accept Method
GetEnumerator MethodLinked-List Implementation
FieldsConstructor and Purge Methods
Push and Pop Methods, Top Property
Accept Method
GetEnumerator MethodApplications
Evaluating Postfix ExpressionsImplementation
Queues
Array Implementation
FieldsConstructor and Purge Methods
Enqueue and Dequeue Methods, Head Property
Trang 26FieldsConstructor and Purge Methods
Enqueue and Dequeue Methods, Head PropertyApplications
ImplementationDeques
Array Implementation
The ``Head'' OperationsThe ``Tail'' OperationsLinked List Implementation
The ``Head'' OperationsThe ``Tail'' OperationsDoubly-Linked and Circular Lists
Removing Items from a ListPositions of Items in a ListFinding the Position of an Item and Accessing by PositionInserting an Item at an Arbitrary Position
Removing Arbitrary Items by PositionLinked-List Implementation
FieldsInserting and Accessing Items in a ListFinding Items in a List
Removing Items from a ListPositions of Items in a ListFinding the Position of an Item and Accessing by PositionInserting an Item at an Arbitrary Position
Removing Arbitrary Items by PositionPerformance Comparison: OrderedListAsArray vs
ListAsLinkedList
Applications
Trang 27Array Implementation
Inserting Items in a Sorted ListLocating Items in an Array-Binary SearchFinding Items in a Sorted List
Removing Items from a ListLinked-List Implementation
Inserting Items in a Sorted ListOther Operations on Sorted ListsPerformance Comparison: SortedListAsArray vs
SortedListAsList
Applications
ImplementationAnalysis
Trang 28Average Case Analysis
Scatter Tables
Chained Scatter Table
ImplementationConstructor, Length Property, and Purge MethodsInserting and Finding an Item
Removing ItemsWorst-Case Running TimeAverage Case Analysis
Trang 29Implementing Trees
Tree Traversals
Depth-First TraversalPreorder, Inorder, and Postorder TraversalsBreadth-First Traversal
Accept MethodTree Enumerators
Constructor
MoveNext Method and Current PropertyGeneral Trees
FieldsConstructor and Purge Methods
Key Property and GetSubtree Method
AttachSubtree and DetachSubtree Methods
N-ary Trees
FieldsConstructors
IsEmpty Property
Key Property, AttachKey and DetachKey Methods
GetSubtree, AttachSubtree and DetachSubtree MethodsBinary Trees
FieldsConstructors
Purge MethodBinary Tree Traversals
Comparing Trees
Applications
ImplementationExercises
Trang 31Removing Items from a Binary HeapLeftist Heaps
Leftist Trees
Implementation
FieldsMerging Leftist Heaps
Binomial QueuesFields
AddTree and RemoveTree MinTree and Min PropertiesMerging Binomial Queues
Putting Items into a Binomial Queue
Removing an Item from a Binomial QueueApplications
Discrete Event Simulation
Implementation
Trang 32Basic OperationsUnion, Intersection, and DifferenceMultisets
Array Implementation
Basic OperationsUnion, Intersection, and DifferenceLinked-List Implementation
UnionIntersectionPartitions
Representing Partitions
Implementing a Partition using a ForestImplementation
Reduce, Reuse, Recycle
ReduceReuseRecycleHelping the Garbage Collector
Reference Counting Garbage Collection
Trang 33Case 2 ( )Case 3 ( )SummaryExample-Matrix Multiplication
Bottom-Up Algorithms: Dynamic
Programming
Trang 34Example-Computing Binomial Coefficients
Application: Typesetting Problem
ExampleImplementationRandomized Algorithms
Generating Random Numbers
The Minimal Standard Random Number GeneratorImplementation
Random Variables
A Simple Random VariableUniformly Distributed Random VariablesExponentially Distributed Random VariablesMonte Carlo Methods
Example-Computing Simulated Annealing
Example-Balancing ScalesExercises
Worst-Case Running TimeBest-Case Running TimeAverage Running Time
Selecting the Pivot
Selection Sorting
Trang 35ImplementationBuilding the Heap
Running Time AnalysisThe Sorting PhaseMerge Sorting
Implementation
Merging
Two-Way Merge SortingRunning Time Analysis
A Lower Bound on Sorting
Distribution Sorting
Bucket Sort
ImplementationRadix Sort
ImplementationPerformance Data
Terminology
Labeled Graphs
Representing Graphs
Adjacency MatricesSparse vs Dense GraphsAdjacency Lists
Implementing Graphs
Vertices
EnumeratorsEdges
Graphs and Digraphs
Trang 36Single-Source Shortest Path
Dijkstra's Algorithm
Data Structures for Dijkstra's AlgorithmImplementation
Trang 37Prim's Algorithm
ImplementationKruskal's Algorithm
ImplementationRunning Time AnalysisApplication: Critical Path Analysis
Member Access ControlOperator Overloading
Nested Classes
Inheritance and Polymorphism
Derivation and Inheritance
Derivation and Access ControlPolymorphism
InterfacesAbstract Methods and Abstract ClassesMethod Resolution
Abstract Classes and Concrete ClassesAlgorithmic Abstraction
Multiple Inheritance
Trang 38Run-Time Type Information and CastsExceptions
Trang 42This book is about the fundamentals of data structures and algorithms the basic
elements from which large and complex software artifacts are built To develop asolid understanding of a data structure requires three things: First, you mustlearn how the information is arranged in the memory of the computer Second,you must become familiar with the algorithms for manipulating the informationcontained in the data structure And third, you must understand the performancecharacteristics of the data structure so that when called upon to select a suitabledata structure for a particular application, you are able to make an appropriatedecision
This book also illustrates object-oriented design and it promotes the use of
common, object-oriented design patterns The algorithms and data structures inthe book are presented in the C# programming language Virtually all the datastructures are presented in the context of a single class hierarchy This
Trang 44Traditional approaches to the design of software have been either data oriented
or process oriented Data-oriented methodologies emphasize the representation
of information and the relationships between the parts of the whole The actionswhich operate on the data are of less significance On the other hand, process-oriented design methodologies emphasize the actions performed by a softwareartifact; the data are of lesser importance
It is now commonly held that object-oriented methodologies are more effective
for managing the complexity which arises in the design of large and complexsoftware artifacts than either data-oriented or process-oriented methodologies
Trang 45Abstraction can be thought of as a mechanism for suppressing irrelevant detailswhile at the same time emphasizing relevant ones An important benefit ofabstraction is that it makes it easier for the programmer to think about the
There are also many different levels of abstraction The lower the levels of
abstraction expose more of the details of an implementation whereas the higherlevels hide more of the details
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.
Trang 46Encapsulation aids the software designer by enforcing information hiding.
Objects encapsulate data and the procedures for manipulating that data In a sense, the object hides the details of the implementation from the user of that
object
There are two very real benefits from encapsulation conceptual and physical
independence Conceptual independence results from hiding the implementation
of an object from the user of that object Consequently, the user is preventedfrom doing anything with an object that depends on the implementation of thatobject This is desirable because it allows the implementation to be changedwithout requiring the modification of the user's code
Physical independence arises from the fact that the behavior of an object isdetermined by the object itself The behavior of an object is not determined bysome external entity As a result, when we perform an operation on an object,there are no unwanted side-effects
Copyright © 2001 by Bruno R Preiss, P.Eng All rights
reserved.