in reply to Table shuffling challenge
A matrix populated mostly with 0s (or null for objects) is called a sparse matrix. Refer this. Keeping this in mind and re-reading your question, I can represent your data as a sparse matrix (critical analysis of this assumption is welcome).
To parse a sparse matrix, my algorithm is as follows:
# ALGORITHM: parseSparse (M) // M is the given matrix //first, we "incrementally" parse the matrix //this enables us to capture only non-zero entries //of each row. set sum = 0; foreach $row{ foreach $col{ if M($row, $col) != 0 $key = concatenate ($row, $col); $hash{$key} = M($row, $col); } } //now that my hash is ready, I can do random access faster /* implement what you want to implement, I will just sum all the n +on zero elements of M */ foreach $key (keys %hash) { sum += $hash{$key}; }
The algorithm essentially creates a 3-tuple (row, column, value) and using a hash makes random access faster as compared to arrays. The wiki link above elucidates more algorithms to represent a sparse matrix.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Table shuffling challenge
by BrowserUk (Patriarch) on Aug 24, 2013 at 07:11 UTC |