chore(CSharp): added floyd warshall (#513)

pull/516/head
Waqar Hassan Khan 2021-10-03 00:06:05 +06:00 committed by GitHub
parent 1a8762eb19
commit 1bcf9483da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 0 deletions

View File

@ -38,3 +38,4 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
- [Depth First Search](src/Graph/depth-first-search.cs) - [Depth First Search](src/Graph/depth-first-search.cs)
- [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.cs) - [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.cs)
- [Dijkstras Algorithm to Find Shortest Path](src/Graph/dijkstra.cs) - [Dijkstras Algorithm to Find Shortest Path](src/Graph/dijkstra.cs)
- [Floyd Warshalls Algorithm to Find All Pair Shortest Path](src/Graph/floyd-warshall-algorithm.cs)

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
namespace Algorithms.Graph
{
public class FloydWarshallAlgorithm
{
const int INFINITY = (int)1e9;
public static int[,] AllPairShortestPath(int totalNode, bool isDirected, List<Tuple<int, int, int>> edges)
{
int[,] distance = new int[totalNode + 1, totalNode + 1];
for (int i = 1; i <= totalNode; i++)
{
for (int j = 1; j <= totalNode; j++)
{
distance[i, j] = i == j ? 0 : INFINITY;
distance[j, i] = i == j ? 0 : INFINITY;
}
}
foreach (var edge in edges)
{
distance[edge.Item1, edge.Item2] = Math.Min(edge.Item3, distance[edge.Item1, edge.Item2]);
distance[edge.Item2, edge.Item1] = isDirected ? distance[edge.Item2, edge.Item1] : Math.Min(edge.Item3, distance[edge.Item2, edge.Item1]);
}
for (int k = 1; k <= totalNode; k++)
{
for (int i = 1; i <= totalNode; i++)
{
for (int j = 1; j <= totalNode; j++)
{
distance[i, j] = Math.Min(distance[i, j], distance[i, k] + distance[k, j]);
}
}
}
return distance;
}
public static void Main()
{
List<Tuple<int, int, int>> edges = new List<Tuple<int, int, int>>()
{
new Tuple<int, int, int>(1, 4, 10),
new Tuple<int, int, int>(1, 2, 5),
new Tuple<int, int, int>(2, 3, 3),
new Tuple<int, int, int>(3, 4, 1)
};
var results = AllPairShortestPath(4, true, edges);
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (results[i, j] >= INFINITY)
{
Console.Write("inf ");
}
else
{
Console.Write($"{results[i, j]} ");
}
}
Console.WriteLine();
}
}
}
}

View File

@ -0,0 +1,64 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace Algorithms.Tests.Graph
{
[TestFixture]
public class FloydWarshallAlgorithm
{
static object[] TestCasesForAllPaiShortestPath =
{
new object[]
{
4,
true,
new List<Tuple<int, int, int>>()
{
new Tuple<int, int, int>(1, 4, 10),
new Tuple<int, int, int>(1, 2, 5),
new Tuple<int, int, int>(2, 3, 3),
new Tuple<int, int, int>(3, 4, 1)
},
"0 5 8 9 INF 0 3 4 INF INF 0 1 INF INF INF 0"
},
new object[]
{
4,
false,
new List<Tuple<int, int, int>>()
{
new Tuple<int, int, int>(1, 4, 10),
new Tuple<int, int, int>(1, 2, 5),
new Tuple<int, int, int>(2, 3, 3),
new Tuple<int, int, int>(3, 4, 1)
},
"0 5 8 9 5 0 3 4 8 3 0 1 9 4 1 0"
},
};
[TestCaseSource(nameof(TestCasesForAllPaiShortestPath))]
public void TestFloydWarshallAlgorithm_ShouldGetExpectedResult(int totalNode, bool isDirected, List<Tuple<int, int, int>> edges, string expected)
{
var results = Algorithms.Graph.FloydWarshallAlgorithm.AllPairShortestPath(totalNode, isDirected, edges);
string distances = "";
const int INFINITY = (int)1e9;
for (int i = 1; i <= totalNode; i++)
{
for (int j = 1; j <= totalNode; j++)
{
if (!(i == 1 && j == 1))
{
distances += " ";
}
distances += results[i, j] >= INFINITY ? "INF" : results[i, j].ToString();
}
}
Assert.AreEqual(expected, distances);
}
}
}