does weight works on "diGraph.algorithms().findShortestPath"?
Closed this issue · 3 comments
normally call "diGraph.algorithms().findShortestPath" it works. But,when i add edge with weight , it does not choose the more weight path.
my test code
@Test public void selfShortest() { Graph<Integer> diGraph = new DirectedGraph<>(); diGraph.addVertices(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); diGraph.addEdge(2, 4); diGraph.addEdge(4, 5); diGraph.addEdge(4, 6); diGraph.addEdge(4, 7); diGraph.addEdge(2, 8, 2.0f); diGraph.addEdge(6, 8, 50); diGraph.addEdge(3, 8); diGraph.addEdge(8, 9); assertTrue(diGraph.edgeExists(2, 4)); Path<Integer> shortestPath = diGraph.algorithms().findShortestPath(2, 9, integerSearchStep -> { Integer stepTarget = integerSearchStep.edge().getB(); System.err.println(integerSearchStep.edge().getA() + "_" + stepTarget); Node<Integer> stepTargetNode = integerSearchStep.edge().getInternalNodeB(); int stepTargetInDegree = stepTargetNode.getInDegree(); if (stepTargetInDegree > 1) { stepTargetNode.inEdges.forEach(conn -> { System.err.println(">>>>>>" + conn.getA() + ">>>>>>" + conn.getB() + ">>>>多入度,可以考虑判断是否可达"); }); } }); System.err.println(shortestPath); assertTrue(shortestPath.size > 0); }
althought [2, 8, 9] is the Shortest ,but it's under the specific scene "every edge with default weight" , when i set " diGraph.addEdge(6, 8, 50);" , i wish get the result "[2,4,6,8,9]"
does anyone has any idea ?
Hi, I'm not sure what you're expecting to happen. Your code outputs:
2_4
2_8
>>>>>>2>>>>>>8>>>>多入度,可以考虑判断是否可达
>>>>>>6>>>>>>8>>>>多入度,可以考虑判断是否可达
>>>>>>3>>>>>>8>>>>多入度,可以考虑判断是否可达
4_7
4_6
4_5
[2, 8, 9]
which is what it's supposed to. The shortest path in your example is [2, 8, 9]
which has a length of 2+1 = 3
, whereas the path [2,4,6,8,9]
has a length of 1+1+50+1 = 53
(the length of a path is the sum of all the weights of each edge in the path).
What are you trying to achieve/what result are you expecting?
Haha, my fault , what i think is "the weight is bigger , it should be more shorter " , last night i was thought ,maybe it ”negative direction“ ,now i got your response ! i should be one more try before ask the question !
I tried, it works! thanks a lot!
could you upload the jar to https://mvnrepository.com/ too , my project is build by maven. or else i only choose use source code or myself's nexus .
this repo is great, it should be known by others. @earlygrey