0

We were encountering this Graph problem and we needed to implement Floyd Warshall, So we did. Although we kinda disliked the algorithm because it is very slow.

We would like to know if it's possible to apply parallel linq on the second loop, so we can speed up the algorithm a little

Question: Speed up the For loop with var i

private int[,] FloydWarshall(int[,] matrix)
   {
     var loopCount = matrix.GetLength(0);
     var next = CreatePredecessorMatrix(matrix);

        for (var k = 0; k < loopCount; k++)
        {
            for (var i = 0; i < loopCount; i++)
            {
                for (var j = 0; j < loopCount; j++)
                {
                    if (matrix[i, j] > matrix[i, k] + matrix[k, j])
                    {
                        matrix[i, j] = matrix[i, k] + matrix[k, j];
                        next[i, j] = next[k, j];
                    }
                }
            }
        }
        return next;
   }
barthr
  • 430
  • 7
  • 15
  • Try to write in your browser Floyd Warshall parallel implementation and click on a few of many links you will see. It will made your question obsolete. – Salvador Dali Jan 12 '16 at 20:12

2 Answers2

1

Does a Parallel.For in place of that "var i" for-loop not achieve the same speed up as the Parallel Linq?

Viider Storm
  • 189
  • 6
0

Very good suggestion by Viider Storm about a Parallel For

   private int[,] FloydWarshall(int[,] matrix)
    {
        var loopCount = matrix.GetLength(0);
        var next = CreatePredecessorMatrix(matrix);
        for (var k = 0; k < loopCount; k++)
        {
            Console.WriteLine(k);

            Parallel.For(0, loopCount, i =>
            {
                for (var j = 0; j < loopCount; j++)
                {
                    if (matrix[i, j] > matrix[i, k] + matrix[k, j])
                    {
                        matrix[i, j] = matrix[i, k] + matrix[k, j];
                        next[i, j] = next[k, j];
                    }
                }
            });
        }
        return next;
    }

Thanks for the input!

barthr
  • 430
  • 7
  • 15