Beispiel #1
0
        public void CalcShortestDistSum()
        {
            ICityStorage queue         = new CityStorage();
            ICityStorage resultStorage = new CityStorage();

            queue.Add(this);
            _distSums.SetData(_id, 0);

            while (!queue.isEmpty())
            {
                int   index           = 0;
                ICity currentCity     = GetMinFromTabAndStor(_distSums, queue, ref index);
                int   CurrentCityDist = _distSums.GetInfo(currentCity.Id);
                for (int i = 0; i < currentCity.GetCntOfNeighbors(); i++)
                {
                    ICity currentNeighbor = currentCity.GetNeighbor(i);
                    int   tmpDist         = currentCity.GetWeightToNeighbor(currentNeighbor.Id) + CurrentCityDist;

                    if (tmpDist < _distSums.GetInfo(currentNeighbor.Id))
                    {
                        _distSums.SetData(currentNeighbor.Id, tmpDist);
                        queue.Add(currentNeighbor);//new
                    }
                    if (!CheckIsItHere(resultStorage, currentNeighbor) &&
                        !CheckIsItHere(queue, currentNeighbor))
                    {
                        queue.Add(currentNeighbor);
                    }
                }
                resultStorage.Add(currentCity);
                queue.Delete(index);
            }
        }
Beispiel #2
0
        public string GetPath()
        {
            int   firstId    = _rows[_from].GetId();
            int   secondId   = _rows[_from].GetElement(_to).GetId();
            ICity firstCity  = null;
            ICity secondCity = null;

            for (int i = 0; i < _graph.Count; i++)
            {
                ICity currentCity1 = _graph[i];
                if (currentCity1.Id == firstId)
                {
                    firstCity = currentCity1;
                }
                if (currentCity1.Id == secondId)
                {
                    secondCity = currentCity1;
                }
            }
            if (firstCity == null | secondCity == null)
            {
                return("");
            }
            int sum = firstCity.GetDistTo(secondCity.Id);

            if (sum >= 5000)
            {
                return("Отсутствует какой-либо путь между данными вершинами");
            }
            string        path        = "";
            ICityStorage  qeue        = secondCity.GetNeighbors();//new
            ICity         currentCity = secondCity;
            List <string> pathList    = new List <string>();

            while (sum > 0)
            {
                for (int i = 0; i < qeue.Count; i++)
                {
                    ICity tmpCity = qeue[i];
                    int   weight  = tmpCity.GetWeightToNeighbor(currentCity.Id);
                    if (firstCity.GetDistTo(tmpCity.Id) == firstCity.GetDistTo(currentCity.Id) - weight)
                    {
                        //path += currentCity.GetId() + " <- ";
                        pathList.Add(currentCity.Id.ToString());
                        sum        -= weight;
                        currentCity = tmpCity;
                        break;
                    }
                }
                qeue = currentCity.GetNeighbors(); //new
            }
            for (int i = pathList.Count - 1; i >= 0; i--)
            {
                path += " -> " + pathList[i];
            }
            return(firstCity.Id.ToString() + path);
        }