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. 3.9 Space Bounds 79A d<strong>at</strong>a structure’s primary purpose is to store d<strong>at</strong>a in a way th<strong>at</strong> allows efficientaccess to those d<strong>at</strong>a. To provide efficient access, it may be necessary to store additionalinform<strong>at</strong>ion about where the d<strong>at</strong>a are within the d<strong>at</strong>a structure. For example,each node of a linked list must store a pointer to the next value on the list. All suchinform<strong>at</strong>ion stored in addition to the actual d<strong>at</strong>a values is referred to as overhead.Ideally, overhead should be kept to a minimum while allowing maximum access.The need to maintain a balance between these opposing goals is wh<strong>at</strong> makes thestudy of d<strong>at</strong>a structures so interesting.One important aspect of algorithm design is referred to as the space/time tradeoffprinciple. The space/time tradeoff principle says th<strong>at</strong> one can often achieve areduction in time if one is willing to sacrifice space or vice versa. Many programscan be modified to reduce storage requirements by “packing” or encoding inform<strong>at</strong>ion.“Unpacking” or decoding the inform<strong>at</strong>ion requires additional time. Thus, theresulting program uses less space but runs slower. Conversely, many programs canbe modified to pre-store results or reorganize inform<strong>at</strong>ion to allow faster runningtime <strong>at</strong> the expense of gre<strong>at</strong>er storage requirements. Typically, such changes in time<strong>and</strong> space are both by a constant factor.A classic example of a space/time tradeoff is the lookup table. A lookup tablepre-stores the value of a function th<strong>at</strong> would otherwise be computed each time it isneeded. For example, 12! is the gre<strong>at</strong>est value for the factorial function th<strong>at</strong> can bestored in a 32-bit int variable. If you are writing a program th<strong>at</strong> often computesfactorials, it is likely to be much more time efficient to simply pre-compute <strong>and</strong>store the 12 values in a table. Whenever the program needs the value of n! it cansimply check the lookup table. (If n > 12, the value is too large to store as an intvariable anyway.) Compared to the time required to compute factorials, it may bewell worth the small amount of additional space needed to store the lookup table.Lookup tables can also store approxim<strong>at</strong>ions for an expensive function such assine or cosine. If you compute this function only for exact degrees or are willingto approxim<strong>at</strong>e the answer with the value for the nearest degree, then a lookuptable storing the comput<strong>at</strong>ion for exact degrees can be used instead of repe<strong>at</strong>edlycomputing the sine function. Note th<strong>at</strong> initially building the lookup table requiresa certain amount of time. Your applic<strong>at</strong>ion must use the lookup table often enoughto make this initializ<strong>at</strong>ion worthwhile.Another example of the space/time tradeoff is typical of wh<strong>at</strong> a programmermight encounter when trying to optimize space. Here is a simple code fragment forsorting an array of integers. We assume th<strong>at</strong> this is a special case where there are nintegers whose values are a permut<strong>at</strong>ion of the integers from 0 to n − 1. This is anexample of a Binsort, which is discussed in Section 7.7. Binsort assigns each valueto an array position corresponding to its value.for (i=0; i

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

Saved successfully!

Ooh no, something went wrong!