Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Table shuffling challenge

by code-ninja (Scribe)
on Aug 24, 2013 at 06:43 UTC ( [id://1050773]=note: print w/replies, xml ) Need Help??


in reply to Table shuffling challenge

ok, I'm at my work place so cannot submit the code right now but I'll post my algorithm at least because my boss is not around and this post got me ticking... ;).
I'll upload my rendition of the code once I get home.

A Sparse Matrix

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
    (critical analysis of this assumption is welcome) ... using a hash makes random access faster as compared to arrays

    Not so. Both hashes and arrays are classed as O(1) for access; but that 1 is much higher for hashes than for arrays:

    @array = 1..100;; @hash{ 0 .. 99 } = @array;; cmpthese -1,{ a=>q[my$t=0; $t+=$_ for @array;], b=>q[my$t=0; $t+=$hash{$_} for keys %hash;] };; Rate b a b 35255/s -- -66% a 103177/s 193% --

    Sparse matrix are only really useful when the ratio of zeros to non-zeros is greater than say 8:1 and the size is 100s of millions.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1050773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found