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.

412 Chap. 12 Lists <strong>and</strong> Arrays Revisitedare non-zero. Different values for the rel<strong>at</strong>ive sizes of d<strong>at</strong>a values, pointers, orm<strong>at</strong>rix indices can lead to a different break-even point for the two implement<strong>at</strong>ions.The time required to process a sparse m<strong>at</strong>rix should ideally depend on NNZ.When searching for an element, the cost is the number of elements preceding thedesired element on its row or column list. The cost for oper<strong>at</strong>ions such as addingtwo m<strong>at</strong>rices should be Θ(n + m) in the worst case when the one m<strong>at</strong>rix stores nnon-zero elements <strong>and</strong> the other stores m non-zero elements.Another represent<strong>at</strong>ion for sparse m<strong>at</strong>rices is sometimes called the Yale represent<strong>at</strong>ion.M<strong>at</strong>lab uses a similar represent<strong>at</strong>ion, with a primary difference beingth<strong>at</strong> the M<strong>at</strong>lab represent<strong>at</strong>ion uses column-major order. 1 The M<strong>at</strong>lab represent<strong>at</strong>ionstores the sparse m<strong>at</strong>rix using three lists. The first is simply all of the non-zeroelement values, in column-major order. The second list stores the start positionwithin the first list for each column. The third list stores the row positions for eachof the corresponding non-zero values. In the Yale represent<strong>at</strong>ion, the m<strong>at</strong>rix ofFigure 12.7 would appear as:Values: 10 45 40 23 5 32 93 12 19 7Column starts: 0 3 5 5 7 7 7 7Row positions: 0 1 4 0 1 7 1 7 0 7If the m<strong>at</strong>rix has c columns, then the total space required will be proportional toc + 2NNZ. This is good in terms of space. It allows fairly quick access to anycolumn, <strong>and</strong> allows for easy processing of the non-zero values along a column.However, it does not do a good job of providing access to the values along a row,<strong>and</strong> is terrible when values need to be added or removed from the represent<strong>at</strong>ion.Fortun<strong>at</strong>ely, when doing comput<strong>at</strong>ions such as adding or multiplying two sparsem<strong>at</strong>rices, the processing of the input m<strong>at</strong>rices <strong>and</strong> construction of the output m<strong>at</strong>rixcan be done reasonably efficiently.12.3 Memory ManagementMost d<strong>at</strong>a structures are designed to store <strong>and</strong> access objects of uniform size. Atypical example would be an integer stored in a list or a queue. Some applic<strong>at</strong>ionsrequire the ability to store variable-length records, such as a string of arbitrarylength. One solution is to store in the list or queue fixed-length pointers to thevariable-length strings. This is fine for d<strong>at</strong>a structures stored in main memory.But if the collection of strings is meant to be stored on disk, then we might needto worry about where exactly these strings are stored. And even when stored inmain memory, something has to figure out where there are available bytes to holdthe string. We could easily store variable-size records in a queue or stack, where1 Scientific packages tend to prefer column-oriented represent<strong>at</strong>ions for m<strong>at</strong>rices since this thedominant access need for the oper<strong>at</strong>ions to be performed.

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

Saved successfully!

Ooh no, something went wrong!