/** * Update the distance from the source to the concerned vertex. * @param vertex */ private void UpdateVertex(BaseVertex vertex, bool isSource2sink) { // 1. get the neighboring vertices ISet <BaseVertex> neighborVertexList = isSource2sink ? graph.GetAdjacentVertices(vertex) : graph.GetPrecedentVertices(vertex); // 2. update the distance passing on current vertex foreach (BaseVertex curAdjacentVertex in neighborVertexList) { // 2.1 skip if visited before if (determinedVertexSet.Contains(curAdjacentVertex)) { continue; } // 2.2 calculate the new distance double distance = startVertexDistanceIndex.ContainsKey(vertex)? startVertexDistanceIndex[vertex] : Graph.DISCONNECTED; distance += isSource2sink ? graph.GetEdgeWeight(vertex, curAdjacentVertex) : graph.GetEdgeWeight(curAdjacentVertex, vertex); // 2.3 update the distance if necessary if (!startVertexDistanceIndex.ContainsKey(curAdjacentVertex) || startVertexDistanceIndex[curAdjacentVertex] > distance) { startVertexDistanceIndex.AddOrReplace(curAdjacentVertex, distance); predecessorIndex.AddOrReplace(curAdjacentVertex, vertex); curAdjacentVertex.SetWeight(distance); vertexCandidateQueue.add(curAdjacentVertex, curAdjacentVertex.GetWeight()); } } }
/** * 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; }