private static void BuildDistanceTable(IGraph <TValue> graph, INode <TValue> randomEdge)
        {
            // create distance table
            distanceTable = new Dictionary <INode <TValue>, Tuple <int, INode <TValue> > >();

            // populate distance table
            foreach (var edge in graph.GetEdges())
            {
                distanceTable[edge] = new Tuple <int, INode <TValue> >(-1, null);
            }

            // initalize distance table
            distanceTable[randomEdge] = new Tuple <int, INode <TValue> >(0, randomEdge);

            // create traverse collections
            var searched      = new List <INode <TValue> >();
            var priorityQueue = new PriorityQueue2 <INode <TValue>, int>();

            // initalize pq
            priorityQueue.Enqueue(randomEdge, 0);

            while (priorityQueue.Any())
            {
                // Dequeue min edge weight
                var currentEdge    = priorityQueue.DequeueMin();
                var currentEdgCost = distanceTable[currentEdge].Item1;

                // add to existing
                searched.Add(currentEdge);

                // check if visited
                if (searched.Contains(currentEdge))
                {
                    continue;
                }


                if (currentEdge != randomEdge)
                {
                    // stuff
                }

                // Lowest weighted Edge connecting an unvisited node.
                foreach (var adjacentEdge in currentEdge.GetAdjacentEdges())
                {
                    var edgeCost          = currentEdge.GetWeight(adjacentEdge);
                    var recordedCost      = distanceTable[adjacentEdge].Item1;
                    var cumlativeEdgeCost = currentEdgCost + edgeCost;

                    if (recordedCost == -1 || cumlativeEdgeCost < recordedCost)
                    {
                        distanceTable[adjacentEdge] = new Tuple <int, INode <TValue> >(cumlativeEdgeCost, currentEdge);
                        priorityQueue.Enqueue(adjacentEdge, cumlativeEdgeCost);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void buildDistanceTable(IGraph <TValue> graph, INode <TValue> originEdge)
        {
            // create distance table
            distanceTable = new Dictionary <INode <TValue>, Tuple <int, INode <TValue> > >();

            // populate distance table
            foreach (var edge in graph.GetEdges())
            {
                distanceTable[edge] = new Tuple <int, INode <TValue> >(-1, null);
            }

            // initalize distance table
            distanceTable[originEdge] = new Tuple <int, INode <TValue> >(0, originEdge);

            // create traverse collections
            priorityQueue = new PriorityQueue2 <INode <TValue>, int>();

            // initalize priority queue
            priorityQueue.Enqueue(originEdge, 0);

            // traverse graph
            while (priorityQueue.Any())
            {
                var currentEdge     = priorityQueue.DequeueMin();
                int currentDistance = distanceTable[currentEdge].Item1;

                foreach (var adjacentEdge in currentEdge.GetAdjacentEdges())
                {
                    var recordedDistance = distanceTable[adjacentEdge].Item1;
                    var totalDistance    = currentDistance + currentEdge.GetWeight(adjacentEdge);

                    if (recordedDistance == -1 || recordedDistance > totalDistance)
                    {
                        distanceTable[adjacentEdge] = new Tuple <int, INode <TValue> >(totalDistance, currentEdge);
                        priorityQueue.Enqueue(adjacentEdge, totalDistance);
                    }
                }
            }
        }