public HeldKarpAlgorithm(Graph graph, int startVertex)
 {
     _graph        = graph;
     _result       = new AlgorithmResult();
     _startVertex  = startVertex;
     _weightOfSets = new Dictionary <string, int>();
 }
 public BruteForceAlgorithm(Graph graph)
 {
     _graph  = graph;
     _result = new AlgorithmResult
     {
         Weight = Int32.MaxValue
     };
 }
        public void Invoke()
        {
            _weightOfSets.Clear();
            var tempGraph = _graph.Vertices.ToHashSet();

            tempGraph.Remove(_startVertex);

            _result = HeldKarp(_startVertex, tempGraph);
            _result.Path.Reverse();
        }
Example #4
0
        public void Invoke()
        {
            var init = TabuAlgorithmResult.GenerateResult(_graph.NumberOfCities);

            init.CalculateWeight(_graph);
            var result = TabuSearch(init);

            Result = new AlgorithmResult {
                Path = result.Path, Weight = result.Weight
            };
        }
        private AlgorithmResult HeldKarp(int end, HashSet <int> verticesSet)
        {
            if (!verticesSet.Any())
            {
                //jezeli zbior jest pusty to zwracamy wynik dla wierzchołka końcowego dla danego wywołania i startowego globalnego
                var result = new AlgorithmResult()
                {
                    Weight = _graph.GetWeight(end, _startVertex)
                };
                result.Path.Add(_startVertex);
                result.Path.Add(end);
                return(result);
            }

            int totalWeight  = Int32.MaxValue;
            var reccurResult = new AlgorithmResult();

            foreach (var vertex in verticesSet)
            {
                //zbior bez obecnego wierzchołka
                var tempSet = new HashSet <int>(verticesSet);
                tempSet.Remove(vertex);
                var    otherResult = new AlgorithmResult();
                string Path;

                //sciezka
                Path = $"{_startVertex},";
                if (tempSet.Any())
                {
                    Path += string.Join(",", tempSet) + ",";
                }
                Path += $"{vertex},";

                //jezeli waga dla sciezki bez obecnego wierzchołka znajduje sie w słowniku to ja pobieramy
                if (!_weightOfSets.TryGetValue(Path, out var weightFromMemory))
                {
                    //jezeli nie to wywołujemy rekurencję
                    otherResult = HeldKarp(vertex, tempSet);
                    //i zapisujemy do słownika
                    var innerPath = string.Join(",", otherResult.Path) + ",";
                    _weightOfSets[innerPath] = otherResult.Weight;
                }
                else
                {
                    //jezeli jest w słowniku to przepisujemy wagę
                    otherResult.Weight = weightFromMemory;
                    otherResult.Path   = new List <int>();
                    otherResult.Path.Add(_startVertex);
                    otherResult.Path.AddRange(tempSet);
                    otherResult.Path.Add(vertex);
                }

                int weight = _graph.GetWeight(end, vertex);
                //waga całego cyklu
                int currentWeight = weight + otherResult.Weight;

                //jezeli jest to najkrótsza z dotychczasowych sciezek to ja zapisuje do rozwiazania koncowego
                if (currentWeight < totalWeight)
                {
                    totalWeight         = currentWeight;
                    reccurResult.Weight = currentWeight;
                    reccurResult.Path   = otherResult.Path;
                    reccurResult.Path.Add(end);
                }
            }

            return(reccurResult);
        }