Skip to content

Commit ecf7186

Browse files
committed
generic tsp
1 parent f6f7561 commit ecf7186

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/Advanced.Algorithms/Graph/ShortestPath/TravellingSalesman.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using Advanced.Algorithms.DataStructures.Graph.AdjacencyList;
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56

@@ -9,21 +10,25 @@ namespace Advanced.Algorithms.Graph
910
/// Uses dynamic programming for a
1011
/// psuedo-polynomial time runTime complexity for this NP hard problem.
1112
/// </summary>
12-
public class TravellingSalesman
13+
public class TravellingSalesman<T, W> where W : IComparable
1314
{
14-
public static int FindMinWeight(WeightedDiGraph<int, int> graph)
15+
IShortestPathOperators<W> @operator;
16+
17+
public W FindMinWeight(WeightedDiGraph<T, W> graph, IShortestPathOperators<W> @operator)
1518
{
19+
this.@operator = @operator;
20+
1621
return findMinWeight(graph.ReferenceVertex, graph.ReferenceVertex,
1722
graph.VerticesCount,
18-
new HashSet<WeightedDiGraphVertex<int, int>>(),
19-
new Dictionary<string, int>());
23+
new HashSet<WeightedDiGraphVertex<T, W>>(),
24+
new Dictionary<string, W>());
2025
}
2126

22-
private static int findMinWeight(WeightedDiGraphVertex<int, int> currentVertex,
23-
WeightedDiGraphVertex<int, int> tgtVertex,
27+
private W findMinWeight(WeightedDiGraphVertex<T, W> currentVertex,
28+
WeightedDiGraphVertex<T, W> tgtVertex,
2429
int remainingVertexCount,
25-
HashSet<WeightedDiGraphVertex<int, int>> visited,
26-
Dictionary<string, int> cache)
30+
HashSet<WeightedDiGraphVertex<T, W>> visited,
31+
Dictionary<string, W> cache)
2732
{
2833
var cacheKey = $"{currentVertex.Value}-{remainingVertexCount}";
2934

@@ -34,7 +39,7 @@ private static int findMinWeight(WeightedDiGraphVertex<int, int> currentVertex,
3439

3540
visited.Add(currentVertex);
3641

37-
var results = new List<int>();
42+
var results = new List<W>();
3843

3944
foreach (var vertex in currentVertex.OutEdges)
4045
{
@@ -50,9 +55,9 @@ private static int findMinWeight(WeightedDiGraphVertex<int, int> currentVertex,
5055
{
5156
var result = findMinWeight(vertex.Key, tgtVertex, remainingVertexCount - 1, visited, cache);
5257

53-
if (result != int.MaxValue)
58+
if (!result.Equals(@operator.MaxValue))
5459
{
55-
results.Add(result + vertex.Value);
60+
results.Add(@operator.Sum(result, vertex.Value));
5661
}
5762

5863
}
@@ -62,7 +67,7 @@ private static int findMinWeight(WeightedDiGraphVertex<int, int> currentVertex,
6267

6368
if (results.Count == 0)
6469
{
65-
return int.MaxValue;
70+
return @operator.MaxValue;
6671
}
6772

6873
var min = results.Min();

tests/Advanced.Algorithms.Tests/Graph/ShortestPath/TravellingSalesman_Tests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,39 @@ public void TravellingSalesman_Smoke_Test()
3737
graph.AddEdge(3, 1, 4);
3838
graph.AddEdge(3, 2, 8);
3939

40-
Assert.AreEqual(21, TravellingSalesman.FindMinWeight(graph));
40+
var tsp = new TravellingSalesman<int, int>();
41+
Assert.AreEqual(21, tsp.FindMinWeight(graph, new TSPShortestPathOperators()));
42+
}
43+
}
44+
45+
/// <summary>
46+
/// generic operations for int type
47+
/// </summary>
48+
public class TSPShortestPathOperators : IShortestPathOperators<int>
49+
{
50+
public int DefaultValue
51+
{
52+
get
53+
{
54+
return 0;
55+
}
56+
57+
58+
}
59+
60+
public int MaxValue
61+
{
62+
get
63+
{
64+
return int.MaxValue;
65+
}
66+
}
67+
68+
public int Sum(int a, int b)
69+
{
70+
return checked(a + b);
4171
}
4272
}
4373
}
74+
75+

0 commit comments

Comments
 (0)