12,299,755 members (59,367 online)
Rate this:
See more:
How do I find the local minimum of a NxN matrix without and O N^2 algorithm as below. The time takes for ever when N grows large. I need the algorithm to b order N as its worst case.

```int row = 0;
int column = 0;
Random random = new Random();

int[,] matrix = new int[10, 10];
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
int randomNumber = random.Next(0, 100);
matrix[r, c] = randomNumber;
}
}

int min = 100;
Stopwatch timer = new Stopwatch();
timer.Start();

for (int i = 0; i < matrix.GetLongLength(0); i++)
{
for (int j = 0; j < matrix.GetLongLength(1); j++)
{
if (matrix[i, j] < min)
{
min = matrix[i, j];
}

}
}
timer.Stop();
double time = timer.ElapsedMilliseconds;
Console.WriteLine(time);
```
Posted 27-Jan-12 19:14pm
Edited 28-Jan-12 23:34pm
v2
SAKryukov 28-Jan-12 2:01am

What is "local minimum"? Do you think there is "global" and "local" minimum? :-)
--SA
Andreas Gieriet 28-Jan-12 16:47pm

If speed really matters, you might consider programming this in some better suited language. Maybe C/C++? The complexity is still NxN, but there might be less overhead.

Rate this:

## Solution 1

Short answer is: there is no such thing as miracle.

The answer is already contained in the question. 1) The algorithm you show is really of the O(N2) complexity; 2) It can be improved but the complexity cannot grow lower then O(N2).

First, let's see why the complexity is O(N2). If you test all N2 elements of the matrix, it gives you the said complexity. If you test not all of them, there is a chance of incorrect answer (false minimum) because any of the untested values can be less then the found value.

The real speed depends on the test set of values. That said, there is some probability that there is no less values, because you could sooner find the value of `int.MinValue`. In my solution below, this is taken into account. This line commented "questionable check" slightly reduce the complexity, but increase absolute time of the algorithm. Overall, it is useful only if the probability of `int.MinValue` in data is high enough. Generally, the real performance of any algorithm depends on the method of generation of data set.

Now, performance of your code could be improved by one mysterious trick: `++j` makes better performance then `j++`. I avoided `for` loops by using `foreach`, please see below. Believe or not, it works correctly for multidimensional arrays.

Also, you need to fix a bug. You 100 is a bug, not even a bug, just foolishness. Real solution is using `int.MaxValue`. For floating point types, you should have used `+Inf` which correctly compares with other floating point values.

So, here is the fixed solution:

```internal static int Minimum(int[,] matrix) {
int result = int.MaxValue; //sic!
foreach(int value in matrix) {
if (value == int.MinValue) return value; // questionable check
if (value < result)
result = value;
} //loop
return result;
} //Minumum```

—SA
Rate this:

## Solution 2

As SAKryukov said, the problem is of complexity O(N2).

Sometimes it helps to view the problem from a different angle:
• It might help if you determine that min property while constructing the matrix and cache it somehow... assuming the matrices are constant once they are constructed.
• Or even more weird: store all the values in a sorted structure and only refer from the matrix to these values (maybe with some reference counting mechanism)... assuming you have huge matrices with not many differing values).
• ...

• Cheers

Andi

Top Experts
Last 24hrsThis month
 ppolymorphe 335 OriginalGriff 277 CHill60 200 Dave Kreskowiak 160 Karthik Bangalore 155
 OriginalGriff 9,778 ppolymorphe 5,861 CHill60 5,487 Karthik Bangalore 5,116 Richard MacCutchan 4,939