how to calculate the shortest path between two nodes in a graph where the source and target are the same?
Graph:
A->B(5)
A->D(5)
A->E(7)
B->C(4)
C->D(8)
C->E(2)
D->C(8)
D->E(6)
E->B(3)
for example what is the shortest path between B and B? I tried to use dijkstra but didn’t work, it always return B->B in the first step.
correct ans: B->C->E->B
Split the vertex into two: B1 and B2, where all edges that started in B in the original graph will start in B1; all edges that terminated in B will terminate in B2.
After that, you can run Dijkstra to find shortest path from B1 to B2 in the modified graph.
Note: if you want to preserve all paths in the graph, add an additional edge B2->B1. That will not change the result for your cycle search, and all the paths in the original and modified graph will be equivalent