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.

Sec. 12.2 Matrix Representations 427a 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 matrices. (a) A lower triangular matrix. (b) An uppertriangular matrix.12.2 Matrix RepresentationsSome applications must represent a large, two-dimensional matrix where many ofthe elements have a value of zero. One example is the lower triangular matrix thatresults from solving systems of simultaneous equations. A lower triangular matrixs<strong>to</strong>res zero values at positions [r, c] such that r < c, as shown in Figure 12.6(a).Thus, the upper-right triangle of the matrix is always zero. Another example isthe representation of undirected graphs in an adjacency matrix (see Project 11.2).Because all edges between Vertices i <strong>and</strong> j go in both directions, there is no need <strong>to</strong>s<strong>to</strong>re both. Instead we can just s<strong>to</strong>re one edge going from the higher-indexed vertex<strong>to</strong> the lower-indexed vertex. In this case, only the lower triangle of the matrix canhave non-zero values.We can take advantage of this fact <strong>to</strong> save space. Instead of s<strong>to</strong>ring n(n + 1)/2pieces of information in an n × n array, it would save space <strong>to</strong> use a list of lengthn(n + 1)/2. This is only practical if some means can be found <strong>to</strong> locate within thelist the element that would correspond <strong>to</strong> position [r, c] in the original matrix.To derive an equation <strong>to</strong> do this computation, note that row 0 of the matrixhas 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 <strong>to</strong>tal of ∑ rk=1 k = (r2 + r)/2 non-zero elements.Adding c <strong>to</strong> reach the cth position in the rth row yields the following equation <strong>to</strong>convert position [r, c] in the original matrix <strong>to</strong> the correct position in the list.matrix[r, c] = list[(r 2 + r)/2 + c].A similar equation can be used <strong>to</strong> s<strong>to</strong>re an upper triangular matrix, that is, a matrixwith zero values at positions [r, c] such that r > c, as shown in Figure 12.6(b). Foran n × n upper triangular matrix, the equation would be(b)matrix[r, c] = list[rn − (r 2 + r)/2 + c].A more difficult situation arises when the vast majority of values s<strong>to</strong>red in ann × n matrix 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 matrix.

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

Saved successfully!

Ooh no, something went wrong!