Skip to content

Commit

Permalink
Update Dijkstra functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AramZahedi committed Jul 18, 2019
1 parent 4821bcf commit 2aa5815
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
38 changes: 31 additions & 7 deletions Graph/src/algorithms/Dijkstra/DijkstraAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,39 @@
public class DijkstraAlgorithm {

/**
* @return
* Find shortest path path data.
*
* @param graph the graph
* @param sourceNodeIdentifier the source node identifier
* @param targetNodeIdentifier the target node identifier
* @return path data
* @throws ArrayStoreException the array store exception
*/
public static PathData findShortestPath(Graph graph, String sourceNodeIdentifier, String targetNodeIdentifier) throws ArrayStoreException {
return findShortestPath(graph, graph.getNode(sourceNodeIdentifier), graph.getNode(targetNodeIdentifier));
}

/**
* @return
* Find shortest path path data object.
*
* @param graph the graph
* @param sourceNodeIdentifier the source node identifier
* @param targetNodeIdentifier the target node identifier
* @return path data object
* @throws ArrayStoreException the array store exception
*/
public static PathDataObject findShortestPath(GraphObject graph, String sourceNodeIdentifier, String targetNodeIdentifier) throws ArrayStoreException {
return findShortestPath(graph, graph.getNode(sourceNodeIdentifier), graph.getNode(targetNodeIdentifier));
}

/**
* @return
* Find shortest path path data.
*
* @param graph the graph
* @param sourceNode the source node
* @param targetNode the target node
* @return path data
* @throws ArrayStoreException the array store exception
*/
public static PathData findShortestPath(Graph graph, GraphNode sourceNode, GraphNode targetNode) throws ArrayStoreException {
if (graph.getNodes().size() == 0) throw new ArrayStoreException();
Expand All @@ -37,12 +55,12 @@ public static PathData findShortestPath(Graph graph, GraphNode sourceNode, Graph
PathData pathData = new PathData(nodes, sourceNode, targetNode);

do {
GraphNode leastDistantNode = pathData.GetClosestNode(nodes);
GraphNode leastDistantNode = pathData.getClosestNode(nodes);
nodes.remove(leastDistantNode);
navigatedNodes.add(leastDistantNode);
for (GraphNode adjacentNode : leastDistantNode.getOutgoingNodes().keySet()) {
double currentDistance = pathData.GetDistanceToNode(leastDistantNode) + leastDistantNode.getOutgoingNodes().get(adjacentNode).getWeight();
if (currentDistance < pathData.GetDistanceToNode(adjacentNode)) {
double currentDistance = pathData.getDistanceToNode(leastDistantNode) + leastDistantNode.getOutgoingNodes().get(adjacentNode).getWeight();
if (currentDistance < pathData.getDistanceToNode(adjacentNode)) {
pathData.distances.put(adjacentNode, currentDistance);
adjacentNode.setPreviousNodeInPath(leastDistantNode);
}
Expand All @@ -53,7 +71,13 @@ public static PathData findShortestPath(Graph graph, GraphNode sourceNode, Graph
}

/**
* @return
* Find shortest path path data object.
*
* @param graph the graph
* @param sourceNode the source node
* @param targetNode the target node
* @return path data object
* @throws ArrayStoreException the array store exception
*/
public static PathDataObject findShortestPath(GraphObject graph, NodeGraphObject sourceNode, NodeGraphObject targetNode) throws ArrayStoreException {
if (graph.getNodes().size() == 0) throw new ArrayStoreException();
Expand Down
56 changes: 46 additions & 10 deletions Graph/src/algorithms/Dijkstra/PathData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
* A Type that contains and returns the path and distance from the source node to any other node in the graph that the source node is connected to(Including Target Node).
*/
public class PathData {
GraphNode sourceNode;
GraphNode targetNode;
private GraphNode sourceNode;
private GraphNode targetNode;
HashMap<GraphNode, Double> distances;

/**
* Instantiates a new Path data.
*
* @param nodes the nodes
* @param sourceNode the source node
* @param targetNode the target node
*/

public PathData(ArrayList<GraphNode> nodes, GraphNode sourceNode, GraphNode targetNode) {
this.sourceNode = sourceNode;
this.targetNode = targetNode;
Expand All @@ -24,16 +32,23 @@ public PathData(ArrayList<GraphNode> nodes, GraphNode sourceNode, GraphNode targ
}

/**
* @return
* Gets path nodes to target node.
*
* @return path nodes to target node
*/
public ArrayList<GraphNode> GetPathNodesToTargetNode() {
return GetPathNodesToNode(targetNode);

public ArrayList<GraphNode> getPathNodesToTargetNode() {
return getPathNodesToNode(targetNode);
}

/**
* @return
* Gets path nodes to node.
*
* @param node the node
* @return path nodes to node
*/
public ArrayList<GraphNode> GetPathNodesToNode(GraphNode node) {

public ArrayList<GraphNode> getPathNodesToNode(GraphNode node) {
ArrayList<GraphNode> nodesInPath = new ArrayList<>();
do {
nodesInPath.add(0, node);
Expand All @@ -44,13 +59,34 @@ public ArrayList<GraphNode> GetPathNodesToNode(GraphNode node) {


/**
* @return
* Gets distance to node.
*
* @param node the node
* @return distance to node
*/
public double GetDistanceToNode(GraphNode node) {

public double getDistanceToNode(GraphNode node) {
return distances.get(node);
}

public GraphNode GetClosestNode(ArrayList<GraphNode> nodes) {
/**
* Gets shortest distance
*
* @return the shortest distance
*/

public double getShortestDistance() {
return getDistanceToNode(targetNode);
}

/**
* Gets closest node.
*
* @param nodes the nodes
* @return the closest node
*/

public GraphNode getClosestNode(ArrayList<GraphNode> nodes) {
double minDistance = Double.POSITIVE_INFINITY;

GraphNode closestNode = nodes.get(0);
Expand Down

0 comments on commit 2aa5815

Please sign in to comment.