public bool Equals(Path path2) { if (path2 == null) { return(false); } java.util.LinkedList <Edge> edges2 = path2.GetEdges(); int numEdges1 = edges.size(); int numEdges2 = edges2.size(); if (numEdges1 != numEdges2) { return(false); } for (int i = 0; i < numEdges1; i++) { Edge edge1 = edges.get(i); Edge edge2 = edges2.get(i); if (!edge1.GetFromNode().Equals(edge2.GetFromNode())) { return(false); } if (!edge1.GetToNode().Equals(edge2.GetToNode())) { return(false); } } return(true); }
public void YenKShortestPathsTest() { const double deltaValue = 0.0000001; var graph = new Graph(); graph.AddEdge("A", "B", 5); graph.AddEdge("A", "C", 6); graph.AddEdge("B", "C", 7); graph.AddEdge("B", "D", 8); graph.AddEdge("C", "D", 9); Yen yenAlgorithm = new Yen(); IList <Path> paths = yenAlgorithm.Ksp(graph, "A", "D", 5); Assert.AreEqual(3, paths.Count); Path path1 = paths[0]; Path path2 = paths[1]; Path path3 = paths[2]; Assert.AreEqual(13, path1.GetTotalCost(), deltaValue); Assert.AreEqual(15, path2.GetTotalCost(), deltaValue); Assert.AreEqual(21, path3.GetTotalCost(), deltaValue); java.util.LinkedList <String> nodes1 = path1.GetNodes(); Assert.AreEqual(3, nodes1.size()); Assert.AreEqual("A", nodes1.get(0)); Assert.AreEqual("B", nodes1.get(1)); Assert.AreEqual("D", nodes1.get(2)); java.util.LinkedList <String> nodes2 = path2.GetNodes(); Assert.AreEqual(3, nodes2.size()); Assert.AreEqual("A", nodes2.get(0)); Assert.AreEqual("C", nodes2.get(1)); Assert.AreEqual("D", nodes2.get(2)); java.util.LinkedList <String> nodes3 = path3.GetNodes(); Assert.AreEqual(4, nodes3.size()); Assert.AreEqual("A", nodes3.get(0)); Assert.AreEqual("B", nodes3.get(1)); Assert.AreEqual("C", nodes3.get(2)); Assert.AreEqual("D", nodes3.get(3)); }
public override String ToString() { StringBuilder sb = new StringBuilder(); int numEdges = edges.size(); sb.Append(totalCost); sb.Append(": ["); if (numEdges > 0) { for (int i = 0; i < edges.size(); i++) { sb.Append(edges.get(i).GetFromNode().ToString()); sb.Append("-"); } sb.Append(edges.getLast().GetToNode().ToString()); } sb.Append("]"); return(sb.ToString()); }
/** * Binary search is exploited to find the right position * of the new element. * @param weight * @return the position of the new element */ private int BinLocatePos(double weight, bool isIncremental) { int mid = 0; int low = 0; int high = elementWeightPairList.size() - 1; // while (low <= high) { mid = (low + high) / 2; if (elementWeightPairList.get(mid).GetWeight() == weight) { return(mid + 1); } if (isIncremental) { if (elementWeightPairList.get(mid).GetWeight() < weight) { high = mid - 1; } else { low = mid + 1; } } else { if (elementWeightPairList.get(mid).GetWeight() > weight) { high = mid - 1; } else { low = mid + 1; } } } return(low); }
public void Test_ForEach_LinkedList() { java.lang.SystemJ.outJ.println("LinkedList foreach test"); java.util.LinkedList <String> testArray = new java.util.LinkedList <String>(); testArray.add("VampireAPI"); testArray.add("supports"); testArray.add("foreach statements"); testArray.add("! Yeah!!!"); // for count loop java.lang.SystemJ.outJ.println("for count loop: "); for (int i = 0; i < testArray.size(); i++) { java.lang.SystemJ.outJ.print(testArray.get(i)); java.lang.SystemJ.outJ.print(" "); } java.lang.SystemJ.outJ.println(); // for iterator loop java.lang.SystemJ.outJ.println("for iterator loop: "); for (java.util.Iterator <String> it = testArray.iterator(); it.hasNext();) { java.lang.SystemJ.outJ.print(it.next()); java.lang.SystemJ.outJ.print(" "); } java.lang.SystemJ.outJ.println(); // foreach loop java.lang.SystemJ.outJ.println("foreach loop: "); foreach (String text in testArray) { java.lang.SystemJ.outJ.print(text); java.lang.SystemJ.outJ.print(" "); } java.lang.SystemJ.outJ.println(); Assert.True(true, "compiler OK"); }
/** * Get the shortest path among all that connecting source with targe. * * @return */ public Path Next() { //3.1 prepare for removing vertices and arcs Path curPath = pathCandidates.Poll(); resultList.Add(curPath); BaseVertex curDerivation = pathDerivationVertexIndex[curPath]; int curPathHash = curPath.GetVertexList().subList(0, curPath.GetVertexList().indexOf(curDerivation)).GetHashCode(); int count = resultList.Count; //3.2 remove the vertices and arcs in the graph for (int i = 0; i < count - 1; ++i) { Path curResultPath = resultList[i]; int curDevVertexId = curResultPath.GetVertexList().indexOf(curDerivation); if (curDevVertexId < 0) { continue; } // Note that the following condition makes sure all candidates should be considered. /// The algorithm in the paper is not correct for removing some candidates by mistake. int pathHash = curResultPath.GetVertexList().subList(0, curDevVertexId).GetHashCode(); if (pathHash != curPathHash) { continue; } BaseVertex curSuccVertex = curResultPath.GetVertexList().get(curDevVertexId + 1); graph.DeleteEdge(new Pair <int, int>( curDerivation.GetId(), curSuccVertex.GetId())); } int pathLength = curPath.GetVertexList().size(); java.util.LinkedList <BaseVertex> curPathVertexList = curPath.GetVertexList(); for (int i = 0; i < pathLength - 1; ++i) { graph.DeleteVertex(curPathVertexList.get(i).GetId()); graph.DeleteEdge(new Pair <int, int>( curPathVertexList.get(i).GetId(), curPathVertexList.get(i + 1).GetId())); } //3.3 calculate the shortest tree rooted at target vertex in the graph DijkstraShortestPathAlg reverseTree = new DijkstraShortestPathAlg(graph); reverseTree.GetShortestPathFlower(targetVertex); //3.4 recover the deleted vertices and update the cost and identify the new candidate results bool isDone = false; for (int i = pathLength - 2; i >= 0 && !isDone; --i) { //3.4.1 get the vertex to be recovered BaseVertex curRecoverVertex = curPathVertexList.get(i); graph.RecoverDeletedVertex(curRecoverVertex.GetId()); //3.4.2 check if we should stop continuing in the next iteration if (curRecoverVertex.GetId() == curDerivation.GetId()) { isDone = true; } //3.4.3 calculate cost using forward star form Path subPath = reverseTree.UpdateCostForward(curRecoverVertex); //3.4.4 get one candidate result if possible if (subPath != null) { ++generatedPathNum; //3.4.4.1 get the prefix from the concerned path double cost = 0; IList <BaseVertex> prePathList = new List <BaseVertex>(); reverseTree.CorrectCostBackward(curRecoverVertex); for (int j = 0; j < pathLength; ++j) { BaseVertex curVertex = curPathVertexList.get(j); if (curVertex.GetId() == curRecoverVertex.GetId()) { j = pathLength; } else { cost += graph.GetEdgeWeightOfGraph(curPathVertexList.get(j), curPathVertexList.get(j + 1)); prePathList.Add(curVertex); } } prePathList.AddAll(subPath.GetVertexList()); //3.4.4.2 compose a candidate subPath.SetWeight(cost + subPath.GetWeight()); subPath.GetVertexList().clear(); subPath.GetVertexList().addAll(prePathList); //3.4.4.3 put it in the candidate pool if new if (!pathDerivationVertexIndex.ContainsKey(subPath)) { pathCandidates.Add(subPath); pathDerivationVertexIndex.Add(subPath, curRecoverVertex); } } //3.4.5 restore the edge BaseVertex succVertex = curPathVertexList.get(i + 1); graph.RecoverDeletedEdge(new Pair <int, int>( curRecoverVertex.GetId(), succVertex.GetId())); //3.4.6 update cost if necessary double cost1 = graph.GetEdgeWeight(curRecoverVertex, succVertex) + reverseTree.GetStartVertexDistanceIndex()[succVertex]; if (reverseTree.GetStartVertexDistanceIndex()[curRecoverVertex] > cost1) { reverseTree.GetStartVertexDistanceIndex().AddOrReplace(curRecoverVertex, cost1); reverseTree.GetPredecessorIndex().AddOrReplace(curRecoverVertex, succVertex); reverseTree.CorrectCostBackward(curRecoverVertex); } } //3.5 restore everything graph.RecoverDeletedEdges(); graph.RecoverDeletedVertices(); return(curPath); }