Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Step 1: First I would initialize the matrix to zeros and calculate the required row and column totals.</p> <p>Step 2: Now pick a random row, weighted by the count of 1s that must be in that row (so a row with count 300 is more likely to be picked than a row with weight 5).</p> <p>Step 3: For this row, pick a random column, weighted by the count of 1s in that column (except ignore any cells that may already contain a 1 - more on this later).</p> <p>Step 4: Place a one in this cell and reduce both the row and column count for the appropriate row and column.</p> <p>Step 5: Go back to step 2 until no rows have non-zero count.</p> <p>The problem though is that this algorithm can fail to terminate because you may have a row where you need to place a one, and a column that needs a one, but you've already placed a one in that cell, so you get 'stuck'. I'm not sure how likely this is to happen, but I wouldn't be surprised if it happened very frequently - enough to make the algorithm unusable. If this is a problem I can think of two ways to fix it:</p> <p>a) Construct the above algorithm recursively and allow backtracking on failure.</p> <p>b) Allow a cell to contain a value greater than 1 if there is no other option and keep going. Then at the end you have a correct row and column count but some cells may contain numbers greater than 1. You can fix this by finding a grouping that looks like this:</p> <pre><code>2 . . . . 0 . . . . . . . . . . . . 0 . . . . 1 </code></pre> <p>and changing it to:</p> <pre><code>1 . . . . 1 . . . . . . . . . . . . 1 . . . . 0 </code></pre> <p>It should be easy to find such a grouping if you have many zeros. I think b) is likely to be faster.</p> <p>I'm not sure it's the best way, but it's probably faster than shuffling arrays. I'll be tracking this question to see what other people come up with.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload