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. 12.2 M<strong>at</strong>rix Represent<strong>at</strong>ions 409a 00 0 0 0a 10 a 11 0 0a 20 a 21 a 22 0a 30 a 31 a 32 a 33(a)a 00 a 01 a 02 a 030 a 11 a 12 a 130 0 a 22 a 230 0 0 a 33Figure 12.6 Triangular m<strong>at</strong>rices. (a) A lower triangular m<strong>at</strong>rix. (b) An uppertriangular m<strong>at</strong>rix.the lower-indexed vertex. In this case, only the lower triangle of the m<strong>at</strong>rix canhave non-zero values.We can take advantage of this fact to save space. Instead of storing n(n + 1)/2pieces of inform<strong>at</strong>ion in an n × n array, it would save space to use a list of lengthn(n + 1)/2. This is only practical if some means can be found to loc<strong>at</strong>e within thelist the element th<strong>at</strong> would correspond to position [r, c] in the original m<strong>at</strong>rix.We will derive an equ<strong>at</strong>ion to convert position [r, c] to a position in a onedimensionallist to store the lower triangular m<strong>at</strong>rix. Note th<strong>at</strong> row 0 of the m<strong>at</strong>rixhas one non-zero value, row 1 has two non-zero values, <strong>and</strong> so on. Thus, row ris preceded by r rows with a total of ∑ rk=1 k = (r2 + r)/2 non-zero elements.Adding c to reach the cth position in the rth row yields the following equ<strong>at</strong>ion toconvert position [r, c] in the original m<strong>at</strong>rix to the correct position in the list.m<strong>at</strong>rix[r, c] = list[(r 2 + r)/2 + c].A similar equ<strong>at</strong>ion can be used to convert coordin<strong>at</strong>es in an upper triangular m<strong>at</strong>rix,th<strong>at</strong> is, a m<strong>at</strong>rix with zero values <strong>at</strong> positions [r, c] such th<strong>at</strong> r > c, as shown inFigure 12.6(b). For an n × n upper triangular m<strong>at</strong>rix, the equ<strong>at</strong>ion to convert fromm<strong>at</strong>rix coordin<strong>at</strong>es to list positions would be(b)m<strong>at</strong>rix[r, c] = list[rn − (r 2 + r)/2 + c].A more difficult situ<strong>at</strong>ion arises when the vast majority of values stored in ann × m m<strong>at</strong>rix are zero, but there is no restriction on which positions are zero <strong>and</strong>which are non-zero. This is known as a sparse m<strong>at</strong>rix.One approach to representing a sparse m<strong>at</strong>rix is to conc<strong>at</strong>en<strong>at</strong>e (or otherwisecombine) the row <strong>and</strong> column coordin<strong>at</strong>es into a single value <strong>and</strong> use this as a keyin a hash table. Thus, if we want to know the value of a particular position in them<strong>at</strong>rix, we search the hash table for the appropri<strong>at</strong>e key. If a value for this positionis not found, it is assumed to be zero. This is an ideal approach when all queries tothe m<strong>at</strong>rix are in terms of access by specified position. However, if we wish to findthe first non-zero element in a given row, or the next non-zero element below thecurrent one in a given column, then the hash table requires us to check sequentiallythrough all possible positions in some row or column.

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

Saved successfully!

Ooh no, something went wrong!