Advertisement
Guest User

heldkarp

a guest
Nov 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace PEA_TSP_1.Algorithms
  8. {
  9.     internal class HeldKarpAlgorithm : IAlgorithm
  10.     {
  11.         private readonly Graph _graph;
  12.         private AlgorithmResult _result;
  13.         private readonly int _startVertex;
  14.         private readonly Dictionary<string, int> _weightOfSets;
  15.  
  16.         public AlgorithmResult Result => _result;
  17.  
  18.         public string Name { get; set; }
  19.  
  20.         public HeldKarpAlgorithm(Graph graph, int startVertex)
  21.         {
  22.             _graph = graph;
  23.             _result = new AlgorithmResult();
  24.             _startVertex = startVertex;
  25.             _weightOfSets = new Dictionary<string, int>();            
  26.         }
  27.  
  28.         public void Invoke()
  29.         {
  30.             _weightOfSets.Clear();
  31.             var tempGraph = _graph.Vertices.ToHashSet();
  32.             tempGraph.Remove(_startVertex);
  33.  
  34.             _result = HeldKarp(_startVertex, tempGraph);
  35.              _result.Path.Reverse();
  36.         }
  37.  
  38.         private AlgorithmResult HeldKarp(int end, HashSet<int> verticesSet)
  39.         {
  40.             if (!verticesSet.Any())
  41.             {
  42.                 var result = new AlgorithmResult()
  43.                 {
  44.                     Weight = _graph.GetWeight(end, _startVertex)
  45.                 };
  46.                 result.Path.Add(_startVertex);
  47.                 result.Path.Add(end);
  48.                 return result;
  49.             }
  50.  
  51.             int totalWeight = Int32.MaxValue;
  52.             var reccurResult = new AlgorithmResult();
  53.  
  54.             foreach (var vertex in verticesSet)
  55.             {
  56.                 //set without current vertex
  57.                 var tempSet = new HashSet<int>(verticesSet);
  58.                 tempSet.Remove(vertex);
  59.                 var otherResult = new AlgorithmResult();
  60.                 string Path;
  61.  
  62.                 Path = $"{_startVertex},";
  63.                 if (tempSet.Any())
  64.                 {
  65.                     Path += string.Join(",", tempSet) + ",";
  66.                 }              
  67.                 Path += $"{vertex},";
  68.  
  69.                 if (!_weightOfSets.TryGetValue(Path, out var weightFromMemory))
  70.                 {
  71.                     otherResult = HeldKarp(vertex, tempSet);
  72.  
  73.                     var innerPath = string.Join(",", otherResult.Path) + ",";
  74.                     _weightOfSets[innerPath] = otherResult.Weight;
  75.                 }
  76.                 else
  77.                 {
  78.                     otherResult.Weight = weightFromMemory;
  79.                     otherResult.Path = new List<int>();
  80.                     otherResult.Path.Add(_startVertex);        
  81.                     otherResult.Path.AddRange(tempSet);
  82.                     otherResult.Path.Add(vertex);
  83.                 }
  84.  
  85.                 int weight = _graph.GetWeight(end, vertex);
  86.                 int currentWeight = weight + otherResult.Weight;
  87.  
  88.                 if (currentWeight < totalWeight)
  89.                 {
  90.                     totalWeight = currentWeight;
  91.                     reccurResult.Weight = currentWeight;
  92.                     reccurResult.Path = otherResult.Path;
  93.                     reccurResult.Path.Add(end);
  94.                 }
  95.             }
  96.  
  97.             return reccurResult;
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement