12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

84 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>The analysis techniques used <strong>to</strong> measure space requirements are similar <strong>to</strong> thoseused <strong>to</strong> measure time requirements. However, while time requirements are normallymeasured for an algorithm that manipulates a particular data structure, spacerequirements are normally determined for the data structure itself. The concepts ofasymp<strong>to</strong>tic analysis for growth rates on input size apply completely <strong>to</strong> measuringspace requirements.Example 3.16 What are the space requirements for an array of n integers?If each integer requires c bytes, then the array requires cn bytes,which is Θ(n).Example 3.17 Imagine that we want <strong>to</strong> keep track of friendships betweenn people. We can do this with an array of size n × n. Each row of the arrayrepresents the friends of an individual, with the columns indicating who hasthat individual as a friend. For example, if person j is a friend of personi, then we place a mark in column j of row i in the array. Likewise, weshould also place a mark in column i of row j if we assume that friendshipworks both ways. For n people, the <strong>to</strong>tal size of the array is Θ(n 2 ).A data structure’s primary purpose is <strong>to</strong> s<strong>to</strong>re data in a way that allows efficientaccess <strong>to</strong> those data. To provide efficient access, it may be necessary <strong>to</strong> s<strong>to</strong>re additionalinformation about where the data are within the data structure. For example,each node of a linked list must s<strong>to</strong>re a pointer <strong>to</strong> the next value on the list. All suchinformation s<strong>to</strong>red in addition <strong>to</strong> the actual data values is referred <strong>to</strong> as overhead.Ideally, overhead should be kept <strong>to</strong> a minimum while allowing maximum access.The need <strong>to</strong> maintain a balance between these opposing goals is what makes thestudy of data structures so interesting.One important aspect of algorithm design is referred <strong>to</strong> as the space/time tradeoffprinciple. The space/time tradeoff principle says that one can often achieve areduction in time if one is willing <strong>to</strong> sacrifice space or vice versa. Many programscan be modified <strong>to</strong> reduce s<strong>to</strong>rage requirements by “packing” or encoding information.“Unpacking” or decoding the information requires additional time. Thus, theresulting program uses less space but runs slower. Conversely, many programs canbe modified <strong>to</strong> pre-s<strong>to</strong>re results or reorganize information <strong>to</strong> allow faster runningtime at the expense of greater s<strong>to</strong>rage requirements. Typically, such changes in time<strong>and</strong> space are both by a constant fac<strong>to</strong>r.

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

Saved successfully!

Ooh no, something went wrong!