I'm writing a program counting average delay in given network. I use a JUNG library. On input my program reads information how many packets vertex x want to send to vertex y for second. My graph is unweighted and I assume that packets are sending by shortest path.
I use JUNG methods to get shortest path:
public class NetworkGraph {
protected final Graph graph;
protected Vertex[] vertices;
protected Random random;
protected double sumOfFlowStrengthMatrix;
protected final int[][] flowStrengthMatrix;
NetworkGraph(Input input, Graph graph) {
random = new Random();
this.graph = graph;
loadVertices(input);
loadEdges(input);
loadSumOfFlowStrengthMatrix(input);
flowStrengthMatrix = input.getFlowStrengthMatrix();
}
private void loadVertices(Input input) {
vertices = new Vertex[input.getNumberOfVertices()];
for (int i = 0; i < input.getNumberOfVertices(); i++) {
vertices[i] = new Vertex(i + 1);
graph.addVertex(vertices[i]);
}
}
private void loadEdges(Input input) {
for (int i = 0; i < input.getNumberOfVertices(); i++) {
for (int j = 0; j < input.getNumberOfVertices(); j++) {
if (input.getProbabilityOfDivulsionArray()[i][j] != 0) {
if (graph.findEdge(vertices[i], vertices[j]) == null) {
graph.addEdge(new Edge(input.getCapacityArray()[i][j], input.getProbabilityOfDivulsionArray()[i][j]), vertices[i], vertices[j]);
}
}
}
}
}
private void loadSumOfFlowStrengthMatrix(Input input) {
double sum = 0;
for (int i = 0; i < input.getNumberOfVertices(); i++) {
for (int j = 0; j < input.getNumberOfVertices(); j++) {
sum += input.getFlowStrengthMatrix()[i][j];
}
}
this.sumOfFlowStrengthMatrix = sum;
}
public double countAveragePacketDelayInNetwork() throws EdgeException {
double out = 0;
ArrayList<Edge> edges = new ArrayList<>(graph.getEdges());
recountFlows();
for (Edge e : edges) {
out += e.getAveragePacketDelay();
}
return round((out / sumOfFlowStrengthMatrix), 4);
}
protected void recountFlows() {
for (int i = 0; i < vertices.length; i++) {
for (int j = 0; j < vertices.length; j++) {
DijkstraShortestPath<Vertex, Edge> algorithm = new DijkstraShortestPath<>(graph);
List<Edge> edges = algorithm.getPath(vertices[i], vertices[j]);
for (Edge edge : edges) {
edge.addToFlowStrength(flowStrengthMatrix[i][j]);
}
}
}
}
}
I ran my program several times with the same sample graph. Unfortunately I got a different results - for each time I have different average delay - it's really annoying.
Probably it's caused by Dijkstra algorithm - I noticed that Dijkstra algorithm returns different results for the same input. I know that it can be many shortest path from x to y, but why Dijkstra algorithm returns different paths when the input and way of creating graph is exactly the same every time?
Is there any way to make this algorithm returns always the same shortest path for given x and y?