Skip to content

Commit

Permalink
Dijkstra
Browse files Browse the repository at this point in the history
Dijkstra Tests And Implementation Finished
  • Loading branch information
OmidRezaei committed May 13, 2019
1 parent 84c8cf6 commit 6ad0074
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
22 changes: 11 additions & 11 deletions Graph/src/algorithms/Dijkstra/DijkstraAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public static PathDataObject FindShortestPath(GraphObject graph, NodeGraphObject
ArrayList<NodeGraphObject> navigatedNodes = new ArrayList<>();
PathDataObject pathData = new PathDataObject(nodes, sourceNode, targetNode);

NodeGraphObject leastDistantNode = pathData.GetClosestNode(nodes);

nodes.remove(leastDistantNode);
navigatedNodes.add(leastDistantNode);

for (NodeGraphObject adjacentNode : leastDistantNode.getAttachedNodes().keySet()) {
double currentDistance = pathData.GetDistanceToNode(leastDistantNode) + leastDistantNode.getAttachedNodes().get(adjacentNode).getWeight();
if (currentDistance < pathData.GetDistanceToNode(adjacentNode)) {
pathData.distances.put(adjacentNode, currentDistance);
adjacentNode.setPreviousNodeInPath(leastDistantNode);
do {
NodeGraphObject leastDistantNode = pathData.GetClosestNode(nodes);
nodes.remove(leastDistantNode);
navigatedNodes.add(leastDistantNode);
for (NodeGraphObject adjacentNode : leastDistantNode.getAttachedNodes().keySet()) {
double currentDistance = pathData.GetDistanceToNode(leastDistantNode) + leastDistantNode.getAttachedNodes().get(adjacentNode).getWeight();
if (currentDistance < pathData.GetDistanceToNode(adjacentNode)) {
pathData.distances.put(adjacentNode, currentDistance);
adjacentNode.setPreviousNodeInPath(leastDistantNode);
}
}
}
} while (!nodes.isEmpty());

return pathData;
}
Expand Down
20 changes: 20 additions & 0 deletions Graph/src/algorithms/Dijkstra/DijkstraAlgorithmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ void GraphWith3Nodes_StartIs0_DistanceIs2_All() {
Assertions.assertEquals(0, pathData.GetDistanceToNode(graph.getNode("0")));
Assertions.assertEquals(2, pathData.GetDistanceToNode(graph.getNode("1")));
Assertions.assertEquals(2, pathData.GetDistanceToNode(graph.getNode("2")));
}


@Test
void GraphWith6Nodes_StartIs1To5_Some() {
String[] sourceIDs = {"1", "1", "0", "4", "3", "3", "3", "2", "2"};
String[] targetIDs = {"0", "3", "4", "0", "0", "4", "5", "3", "5"};
double[] weights = {4, 1, 2, 3, 5, 2, 1, 3, 4};
graph = GraphBuilder.Build().WithNodes(6).SomeConnected(sourceIDs, targetIDs, weights).getGraph();
PathDataObject pathData = DijkstraAlgorithm.FindShortestPath(graph, "1", "5");

Assertions.assertEquals(6, pathData.distances.size());
Assertions.assertEquals(Double.POSITIVE_INFINITY, pathData.GetDistanceToNode(graph.getNode("2")));
Assertions.assertEquals(4, pathData.GetDistanceToNode(graph.getNode("0")));
Assertions.assertEquals(0, pathData.GetDistanceToNode(graph.getNode("1")));
Assertions.assertEquals(1, pathData.GetDistanceToNode(graph.getNode("3")));
Assertions.assertEquals(3, pathData.GetDistanceToNode(graph.getNode("4")));
Assertions.assertEquals(2, pathData.GetDistanceToNode(graph.getNode("5")));
NodeGraphObject[] nodes = {graph.getNode("1"), graph.getNode("3"), graph.getNode("5")};
Assertions.assertArrayEquals(nodes, pathData.GetPathNodesToNode(graph.getNode("5")).toArray());
Assertions.assertArrayEquals(nodes, pathData.GetPathNodesToTargetNode().toArray());
}
}
2 changes: 1 addition & 1 deletion Graph/src/algorithms/Dijkstra/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public GraphBuilder AllConnected(double weight) {
return this;
}

public GraphBuilder SomeConnected(String[] sourceIDs, String[] targetIDs, int... weights) {
public GraphBuilder SomeConnected(String[] sourceIDs, String[] targetIDs, double... weights) {
if (sourceIDs.length != targetIDs.length || targetIDs.length != weights.length)
throw new IllegalArgumentException("Lengths Are Not Equal");

Expand Down

0 comments on commit 6ad0074

Please sign in to comment.