Пример #1
0
        /**
         * Do the work
         */
        protected void DetermineShortestPaths(BaseVertex sourceVertex,
                                              BaseVertex sinkVertex, bool isSource2sink)
        {
            // 0. clean up variables
            Clear();

            // 1. initialize members
            BaseVertex endVertex   = isSource2sink ? sinkVertex : sourceVertex;
            BaseVertex startVertex = isSource2sink ? sourceVertex : sinkVertex;

            startVertexDistanceIndex.Add(startVertex, 0d);
            startVertex.SetWeight(0d);
            vertexCandidateQueue.add(startVertex, startVertex.GetWeight());

            // 2. start searching for the shortest path
            while (!vertexCandidateQueue.isEmpty())
            {
                BaseVertex curCandidate = vertexCandidateQueue.poll();

                if (curCandidate.Equals(endVertex))
                {
                    break;
                }

                determinedVertexSet.Add(curCandidate);

                UpdateVertex(curCandidate, isSource2sink);
            }
        }
        /**
         * Note that this may not be used externally, because some other members in the class
         * should be updated at the same time.
         *
         * @param startVertexId
         * @param endVertexId
         * @param weight
         */
        protected void AddEdge(int startVertexId, int endVertexId, double weight)
        {
            // actually, we should make sure all vertices ids must be correct.
            if (!idVertexIndex.ContainsKey(startVertexId) ||
                !idVertexIndex.ContainsKey(endVertexId) ||
                startVertexId == endVertexId)
            {
                throw new ArgumentException("The edge from " + startVertexId +
                                            " to " + endVertexId + " does not exist in the graph.");
            }

            // update the adjacent-list of the graph
            ISet <BaseVertex> fanoutVertexSet = new HashSet <BaseVertex>();

            if (fanoutVerticesIndex.ContainsKey(startVertexId))
            {
                fanoutVertexSet = fanoutVerticesIndex[startVertexId];
            }
            fanoutVertexSet.Add(idVertexIndex[endVertexId]);
            fanoutVerticesIndex.AddOrReplace(startVertexId, fanoutVertexSet);
            //
            ISet <BaseVertex> faninVertexSet = new HashSet <BaseVertex>();

            if (faninVerticesIndex.ContainsKey(endVertexId))
            {
                faninVertexSet = faninVerticesIndex[endVertexId];
            }
            faninVertexSet.Add(idVertexIndex[startVertexId]);
            faninVerticesIndex.AddOrReplace(endVertexId, faninVertexSet);
            // store the new edge
            vertexPairWeightIndex.Add(
                new Pair <int, int>(startVertexId, endVertexId),
                weight);
            ++edgeNum;
        }
 /**
  * @author Tomas Johansson, added this method as a refactoring, by extracting code from method 'importFromFile' into this method.
  * Fork: https://github.com/TomasJohansson/k-shortest-paths-java-version
  */
 protected void SetNumberOfVertices(int numberOfVertices)
 {
     vertexNum = numberOfVertices;
     for (int i = 0; i < vertexNum; ++i)
     {
         BaseVertex vertex = new Vertex();
         vertexList.Add(vertex);
         idVertexIndex.Add(vertex.GetId(), vertex);
     }
 }