Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 20:14pm
Edited 29-Jan-12 0:34am
Comments
SAKryukov at 28-Jan-12 2:01am
   
What is "local minimum"? Do you think there is "global" and "local" minimum? :-)
--SA
Andreas Gieriet at 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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 235
1 Sergey Alexandrovich Kryukov 165
2 Afzaal Ahmad Zeeshan 152
3 Maciej Los 150
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 29 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100