From 1bcf9483dab8303b51fe6f65febcf78ab1224ecf Mon Sep 17 00:00:00 2001 From: Waqar Hassan Khan Date: Sun, 3 Oct 2021 00:06:05 +0600 Subject: [PATCH] chore(CSharp): added floyd warshall (#513) --- algorithms/CSharp/README.md | 1 + .../src/Graph/floyd-warshall-algorithm.cs | 72 +++++++++++++++++++ .../test/Graph/floyd-warshall-algorithm.cs | 64 +++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 algorithms/CSharp/src/Graph/floyd-warshall-algorithm.cs create mode 100644 algorithms/CSharp/test/Graph/floyd-warshall-algorithm.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index af05ac0c..1ad10476 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -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) - [Kruskals Algorithm to Find Minimum Spanning Tree](src/Graph/kruskals-algorithm.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) \ No newline at end of file diff --git a/algorithms/CSharp/src/Graph/floyd-warshall-algorithm.cs b/algorithms/CSharp/src/Graph/floyd-warshall-algorithm.cs new file mode 100644 index 00000000..c732457c --- /dev/null +++ b/algorithms/CSharp/src/Graph/floyd-warshall-algorithm.cs @@ -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> 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> edges = new List>() + { + new Tuple(1, 4, 10), + new Tuple(1, 2, 5), + new Tuple(2, 3, 3), + new Tuple(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(); + } + } + } +} diff --git a/algorithms/CSharp/test/Graph/floyd-warshall-algorithm.cs b/algorithms/CSharp/test/Graph/floyd-warshall-algorithm.cs new file mode 100644 index 00000000..454764b5 --- /dev/null +++ b/algorithms/CSharp/test/Graph/floyd-warshall-algorithm.cs @@ -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>() + { + new Tuple(1, 4, 10), + new Tuple(1, 2, 5), + new Tuple(2, 3, 3), + new Tuple(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>() + { + new Tuple(1, 4, 10), + new Tuple(1, 2, 5), + new Tuple(2, 3, 3), + new Tuple(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> 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); + } + } +}