11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 1.1 A Philosophy of <strong>D<strong>at</strong>a</strong> <strong>Structures</strong> 5Given sufficient space to store a collection of d<strong>at</strong>a items, it is always possible tosearch for specified items within the collection, print or otherwise process the d<strong>at</strong>aitems in any desired order, or modify the value of any particular d<strong>at</strong>a item. Thus,it is possible to perform all necessary oper<strong>at</strong>ions on any d<strong>at</strong>a structure. However,using the proper d<strong>at</strong>a structure can make the difference between a program runningin a few seconds <strong>and</strong> one requiring many days.A solution is said to be efficient if it solves the problem within the requiredresource constraints. Examples of resource constraints include the total spaceavailable to store the d<strong>at</strong>a — possibly divided into separ<strong>at</strong>e main memory <strong>and</strong> diskspace constraints — <strong>and</strong> the time allowed to perform each subtask. A solution issometimes said to be efficient if it requires fewer resources than known altern<strong>at</strong>ives,regardless of whether it meets any particular requirements. The cost of a solution isthe amount of resources th<strong>at</strong> the solution consumes. Most often, cost is measuredin terms of one key resource such as time, with the implied assumption th<strong>at</strong> thesolution meets the other resource constraints.It should go without saying th<strong>at</strong> people write programs to solve problems. However,it is crucial to keep this truism in mind when selecting a d<strong>at</strong>a structure to solvea particular problem. Only by first analyzing the problem to determine the performancegoals th<strong>at</strong> must be achieved can there be any hope of selecting the right d<strong>at</strong>astructure for the job. Poor program designers ignore this analysis step <strong>and</strong> apply ad<strong>at</strong>a structure th<strong>at</strong> they are familiar with but which is inappropri<strong>at</strong>e to the problem.The result is typically a slow program. Conversely, there is no sense in adoptinga complex represent<strong>at</strong>ion to “improve” a program th<strong>at</strong> can meet its performancegoals when implemented using a simpler design.When selecting a d<strong>at</strong>a structure to solve a problem, you should follow thesesteps.1. Analyze your problem to determine the basic oper<strong>at</strong>ions th<strong>at</strong> must be supported.Examples of basic oper<strong>at</strong>ions include inserting a d<strong>at</strong>a item into thed<strong>at</strong>a structure, deleting a d<strong>at</strong>a item from the d<strong>at</strong>a structure, <strong>and</strong> finding aspecified d<strong>at</strong>a item.2. Quantify the resource constraints for each oper<strong>at</strong>ion.3. Select the d<strong>at</strong>a structure th<strong>at</strong> best meets these requirements.This three-step approach to selecting a d<strong>at</strong>a structure oper<strong>at</strong>ionalizes a d<strong>at</strong>acenteredview of the design process. The first concern is for the d<strong>at</strong>a <strong>and</strong> the oper<strong>at</strong>ionsto be performed on them, the next concern is the represent<strong>at</strong>ion for thosed<strong>at</strong>a, <strong>and</strong> the final concern is the implement<strong>at</strong>ion of th<strong>at</strong> represent<strong>at</strong>ion.Resource constraints on certain key oper<strong>at</strong>ions, such as search, inserting d<strong>at</strong>arecords, <strong>and</strong> deleting d<strong>at</strong>a records, normally drive the d<strong>at</strong>a structure selection process.Many issues rel<strong>at</strong>ing to the rel<strong>at</strong>ive importance of these oper<strong>at</strong>ions are addressedby the following three questions, which you should ask yourself wheneveryou must choose a d<strong>at</strong>a structure:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!