Esempio n. 1
0
        private PointItem GetMinimunLengthPoint()
        {
            int    min   = -1;
            string point = string.Empty;

            foreach (string s in shortestCollection.Keys)
            {
                foreach (MultplePrimKey key in input.Keys)
                {
                    if (s.Equals(key.Key1) &&
                        remainingCollection.Keys.Contains(key.Key2) &&
                        !shortestCollection.Keys.Contains(key.Key2) &&
                        remainingCollection[key.Key2] != -1)
                    {
                        if (min == -1 || min > remainingCollection[key.Key2])
                        {
                            min   = remainingCollection[key.Key2];
                            point = key.Key2;
                        }
                    }
                }
            }

            PointItem item = new PointItem()
            {
                Distance = min, Point = point
            };

            return(point == string.Empty ? null : item);
        }
Esempio n. 2
0
        private void Dijkstra(string start)
        {
            foreach (MultplePrimKey key in input.Keys)
            {
                if (start.Equals(key.Key1) &&
                    remainingCollection.Keys.Contains(key.Key2) &&
                    !shortestCollection.Keys.Contains(key.Key2))
                {
                    if (remainingCollection[key.Key2] == -1 || remainingCollection[key.Key2] > (shortestCollection[key.Key1] + input[key]))
                    {
                        remainingCollection[key.Key2] = shortestCollection[key.Key1] + input[key];
                    }
                }
            }

            PointItem nextPoint = GetMinimunLengthPoint();

            if (nextPoint == null)
            {
                return;
            }

            shortestCollection.Add(nextPoint.Point, nextPoint.Distance);
            remainingCollection.Remove(nextPoint.Point);

            Dijkstra(nextPoint.Point);
        }
        private PointItem GetMinimunLengthPoint()
        {
            int min = -1;
            string point = string.Empty;
            foreach (string s in shortestCollection.Keys)
            {
                foreach (MultplePrimKey key in input.Keys)
                {
                    if (s.Equals(key.Key1) &&
                        remainingCollection.Keys.Contains(key.Key2) &&
                        !shortestCollection.Keys.Contains(key.Key2) &&
                        remainingCollection[key.Key2] != -1)
                    {
                        if (min == -1 || min > remainingCollection[key.Key2])
                        {
                            min = remainingCollection[key.Key2];
                            point = key.Key2;
                        }
                    }
                }
            }

            PointItem item = new PointItem() { Distance = min, Point = point };
            return (point == string.Empty ? null : item);
        }