Skip to content
Prev Previous commit
Next Next commit
Travelling Salesman problem
  • Loading branch information
justcoding121 committed Jan 17, 2018
commit 4f12076d253e22bf0ff2985244647614c0943aab
1 change: 1 addition & 0 deletions Advanced.Algorithms.Tests/Advanced.Algorithms.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="DataStructures\Tree\Tree_Tests.cs" />
<Compile Include="DataStructures\Tree\BinaryTree_Tests.cs" />
<Compile Include="Geometry\PointRotation_Tests.cs" />
<Compile Include="GraphAlgorithms\ShortestPath\TravellingSalesman_Tests.cs" />
<Compile Include="Miscellaneous\MatrixMultiplication_Tests.cs" />
<Compile Include="Geometry\ClosestPointPair_Tests.cs" />
<Compile Include="Geometry\ConvexHull_Tests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Advanced.Algorithms.DataStructures.Graph.AdjacencyList;
using Advanced.Algorithms.GraphAlgorithms;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Advanced.Algorithms.Tests.GraphAlgorithms.ShortestPath
{
/// <summary>
/// Problem details below
/// https://en.wikipedia.org/wiki/Travelling_salesman_problem
/// </summary>
[TestClass]
public class TravellingSalesman_Tests
{
[TestMethod]
public void TravellingSalesman_Smoke_Test()
{
var graph = new WeightedDiGraph<int, int>();

graph.AddVertex(0);
graph.AddVertex(1);
graph.AddVertex(2);
graph.AddVertex(3);

graph.AddEdge(0, 1, 1);
graph.AddEdge(0, 2, 15);
graph.AddEdge(0, 3, 6);

graph.AddEdge(1, 0, 2);
graph.AddEdge(1, 2, 7);
graph.AddEdge(1, 3, 3);

graph.AddEdge(2, 0, 9);
graph.AddEdge(2, 1, 6);
graph.AddEdge(2, 3, 12);

graph.AddEdge(3, 0, 10);
graph.AddEdge(3, 1, 4);
graph.AddEdge(3, 2, 8);

Assert.AreEqual(21, TravellingSalesman.GetMinWeight(graph));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Advanced.Algorithms.DataStructures.Graph.AdjacencyList;
using System.Collections.Generic;
using System.Linq;

namespace Advanced.Algorithms.GraphAlgorithms
{
/// <summary>
/// Problem details below
/// https://en.wikipedia.org/wiki/Travelling_salesman_problem
/// Uses dynamic programming and have
/// psuedo-polynomial time runtime complexity for this NP hard problem
/// </summary>
public class TravellingSalesman
{
public static int GetMinWeight(WeightedDiGraph<int, int> graph)
{
return GetMinWeight(graph.ReferenceVertex, graph.ReferenceVertex,
graph.VerticesCount,
new HashSet<WeightedDiGraphVertex<int, int>>(),
new Dictionary<string, int>());
}

public static int GetMinWeight(WeightedDiGraphVertex<int, int> currentVertex,
WeightedDiGraphVertex<int, int> tgtVertex,
int remainingVertexCount,
HashSet<WeightedDiGraphVertex<int, int>> visited,
Dictionary<string, int> cache)
{
var cacheKey = $"{currentVertex.Value}-{remainingVertexCount}";

if (cache.ContainsKey(cacheKey))
{
return cache[cacheKey];
}

visited.Add(currentVertex);

var results = new List<int>();

foreach (var vertex in currentVertex.OutEdges)
{
//base case
if (vertex.Key == tgtVertex
&& remainingVertexCount == 1)
{
results.Add(vertex.Value);
break;
}

if (!visited.Contains(vertex.Key))
{
var result = GetMinWeight(vertex.Key, tgtVertex, remainingVertexCount - 1, visited, cache);

if (result != int.MaxValue)
{
results.Add(result + vertex.Value);
}

}
}

visited.Remove(currentVertex);

if (results.Count == 0)
{
return int.MaxValue;
}

var min = results.Min();
cache.Add(cacheKey, min);
return min;
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Note: It is observed that among the implementations here in practice, with the e
- [X] Dijikstra's algorithm ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/GraphAlgorithms/ShortestPath/Dijikstra.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/GraphAlgorithms/ShortestPath/Dijikstras_Tests.cs)) using Fibornacci Heap.
- [X] Floyd-Warshall algorithm ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/GraphAlgorithms/ShortestPath/Floyd-Warshall.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/GraphAlgorithms/ShortestPath/FloydWarshall_Tests.cs))
- [X] Johnson's algorithm ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/GraphAlgorithms/ShortestPath/Johnsons.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/GraphAlgorithms/ShortestPath/Johnson_Tests.cs))
- [X] Travelling Salesman Problem ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/GraphAlgorithms/ShortestPath/TravellingSalesman.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/GraphAlgorithms/ShortestPath/TravellingSalesman_Tests.cs))

### Matching

Expand Down