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.

410 Chap. 12 Lists <strong>and</strong> Arrays RevisitedAnother approach is to implement the m<strong>at</strong>rix as an orthogonal list. Considerthe following sparse m<strong>at</strong>rix:10 23 0 0 0 0 1945 5 0 93 0 0 00 0 0 0 0 0 00 0 0 0 0 0 040 0 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 00 32 0 12 0 0 7The corresponding orthogonal array is shown in Figure 12.7. Here we have alist of row headers, each of which contains a pointer to a list of m<strong>at</strong>rix records.A second list of column headers also contains pointers to m<strong>at</strong>rix records. Eachnon-zero m<strong>at</strong>rix element stores pointers to its non-zero neighbors in the row, bothfollowing <strong>and</strong> preceding it. Each non-zero element also stores pointers to its nonzeroneighbors following <strong>and</strong> preceding it in the column. Thus, each non-zeroelement stores its own value, its position within the m<strong>at</strong>rix, <strong>and</strong> four pointers. Nonzeroelements are found by traversing a row or column list. Note th<strong>at</strong> the firstnon-zero element in a given row could be in any column; likewise, the neighboringnon-zero element in any row or column list could be <strong>at</strong> any (higher) row or columnin the array. Thus, each non-zero element must also store its row <strong>and</strong> columnposition explicitly.To find if a particular position in the m<strong>at</strong>rix contains a non-zero element, wetraverse the appropri<strong>at</strong>e row or column list. For example, when looking for theelement <strong>at</strong> Row 7 <strong>and</strong> Column 1, we can traverse the list either for Row 7 or forColumn 1. When traversing a row or column list, if we come to an element withthe correct position, then its value is non-zero. If we encounter an element witha higher position, then the element we are looking for is not in the sparse m<strong>at</strong>rix.In this case, the element’s value is zero. For example, when traversing the listfor Row 7 in the m<strong>at</strong>rix of Figure 12.7, we first reach the element <strong>at</strong> Row 7 <strong>and</strong>Column 1. If this is wh<strong>at</strong> we are looking for, then the search can stop. If we arelooking for the element <strong>at</strong> Row 7 <strong>and</strong> Column 2, then the search proceeds along theRow 7 list to next reach the element <strong>at</strong> Column 3. At this point we know th<strong>at</strong> noelement <strong>at</strong> Row 7 <strong>and</strong> Column 2 is stored in the sparse m<strong>at</strong>rix.Insertion <strong>and</strong> deletion can be performed by working in a similar way to insertor delete elements within the appropri<strong>at</strong>e row <strong>and</strong> column lists.Each non-zero element stored in the sparse m<strong>at</strong>rix represent<strong>at</strong>ion takes muchmore space than an element stored in a simple n × n m<strong>at</strong>rix. When is the sparsem<strong>at</strong>rix more space efficient than the st<strong>and</strong>ard represent<strong>at</strong>ion? To calcul<strong>at</strong>e this, weneed to determine how much space the st<strong>and</strong>ard m<strong>at</strong>rix requires, <strong>and</strong> how much

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

Saved successfully!

Ooh no, something went wrong!