Fast counting of 2D sub-matrices withing a large, dense 2D matrix?

前端 未结 4 1538
庸人自扰
庸人自扰 2020-12-16 06:29

What\'s a good algorithm for counting submatrices within a larger, dense matrix? If I had a single line of data, I could use a suffix tree, but I\'m not sure if generalizing

4条回答
  •  生来不讨喜
    2020-12-16 06:55

    I don't have a ready answer but here's how I would start:

    -- You want very fast lookup, how much (time) can you spend on building index structures? When brute-force isn't fast enough you need indexes.

    -- What do you know about your data that you haven't told us? Are all the values in all your matrices single-digit integers?

    -- If they are single-digit integers (or anything else you can represent as a single character or index value), think about linearising your 2D structures. One way to do this would be to read the matrix along a diagonal running top-right to bottom-left and scanning from top-left to bottom-right. Difficult to explain in words, but read the matrix:

    1234
    5678
    90ab
    cdef
    

    as 125369470c8adbef

    (get it?)

    Now you can index your super-matrix to whatever depth your speed and space requirements demand; in my example key 1253... points to element (1,1), key abef points to element (3,3). Not sure if this works for you, and you'll have to play around with the parameters to your solution. Choose your favourite method for storing the key-value pairs: a hash, a list, or even build some indexes into the index if things get wild.

    Regards

    Mark

提交回复
热议问题