-1

A binary matrix of size n x n is given.

At each step a function checks whether each row and each column of the given matrix has at least one 1. If not, a purely random coordinate is chosen, say i, j where 1 <= i, j <= n, and it is marked as 1 if it's 0 else the 1 is retained.

The process is repeated until the matrix has each row and column having at least one 1.

Please tell what are the "expected number" of moves in this algorithm.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ABcDexter
  • 2,751
  • 4
  • 28
  • 40
  • 2
    This sounds like a homework problem, not programming related at all... – Krease Mar 01 '14 at 17:23
  • 2
    This question appears to be off-topic because it is about [math.se]. – Bernhard Barker Mar 01 '14 at 17:27
  • 1
    @Chris this is related to programming. Using rand() or srand() of C++ I'm trying to generate random value for checking answer by simulation. Please tell if you know any method. – ABcDexter Mar 01 '14 at 17:28
  • 2
    @user3209112 that is not the question you have actually written. Are you trying to solve this analytically or simulate it? – jonrsharpe Mar 01 '14 at 17:29
  • I'm trying to simulate it... But the problem is that expected value is meant for infinite number of trials... I just want if it has a direct formula or something so that I can simulate to reach near the answer. – ABcDexter Mar 01 '14 at 17:32
  • You should simulate the same `n` multiple times (e.g. 1000) and take the average of the value; this will tend towards the expected value. – jonrsharpe Mar 01 '14 at 17:42
  • but will 1000 be enough, won't it have some precision error ?! – ABcDexter Mar 01 '14 at 17:45
  • 1
    @user3209112 You should experiment and find out! – jonrsharpe Mar 01 '14 at 17:49

2 Answers2

2
for n = 1, 10 do

   -- prepare matrix of zeroes
   local P = {}
   for i = 0, n do
      P[i] = {}
      for j = 0, n do
         P[i][j] = 0
      end
   end
   -- set matrix element at (0,0) = 1
   P[0][0] = 1

   local E = 0  -- expected value of number of steps
   for move = 1, 1000000 do  -- emulate one million steps
      for x = n, 1, -1 do
      for y = n, 1, -1 do
         -- calculate probabilities after next move
         P[x][y] = (
            P[x][y]    *x      *y +
            P[x-1][y]  *(n+1-x)*y +
            P[x][y-1]  *x      *(n+1-y) +
            P[x-1][y-1]*(n+1-x)*(n+1-y)
         )/(n*n)
      end
      end
      E = E + P[n][n]*move
      P[0][0] = 0
      P[n][n] = 0
   end

   print(n, E)

end

Results (n, E):

1   1
2   3.6666666666667
3   6.8178571428571
4   10.301098901099
5   14.039464751085
6   17.982832900812
7   22.096912050614
8   26.357063600653
9   30.744803580639
10  35.245774455244

Exact value of E may be calculated, but it would require inversion of matrix N*N, where N=n*n

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
2

You can use heuristics and simulate over random filed to get approximate output.
You can create an output file over this, which will ensure you have simulated over a lot of data to ensure your approximate answer is close to optimized one.

Dexter
  • 36
  • 1
  • 3