-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented PathData And Dijkstra Algorithm but not tested yet.
- Loading branch information
1 parent
29e2cff
commit e3ea377
Showing
6 changed files
with
131 additions
and
51 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,44 @@ | ||
package algorithms.Dijkstra; | ||
|
||
import elements.Graph; | ||
import elements.GraphNode; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* A type that calculates the path and distance from the source node to any other node that the source node is connected to(Including the target node) | ||
* It's methods return an instance PathData. | ||
*/ | ||
public class DijkstraAlgorithm { | ||
public static void FindShortestPath(Graph graph, String sourceNodeIdentification, String targetNodeIdentification) { | ||
|
||
public static PathData FindShortestPath(Graph graph, String sourceNodeIdentifier, String targetNodeIdentifier) throws ArrayStoreException { | ||
return FindShortestPath(graph, graph.getNode(sourceNodeIdentifier), graph.getNode(targetNodeIdentifier)); | ||
} | ||
|
||
public static PathData FindShortestPath(Graph graph, GraphNode sourceNode, GraphNode targetNode) throws ArrayStoreException { | ||
if (graph.getNodes().size() == 0) throw new ArrayStoreException(); | ||
ArrayList<GraphNode> nodes = graph.getNodes(); | ||
ArrayList<GraphNode> navigatedNodes = new ArrayList<>(); | ||
PathData pathData = new PathData(nodes, sourceNode, targetNode); | ||
|
||
nodes.remove(sourceNode); | ||
navigatedNodes.add(sourceNode); | ||
|
||
GraphNode leastDistantNode = pathData.GetClosestNode(nodes); | ||
|
||
nodes.remove(leastDistantNode); | ||
navigatedNodes.add(leastDistantNode); | ||
|
||
for (GraphNode node : leastDistantNode.getAttachedNodes().keySet()) { | ||
double currentDistance = pathData.GetDistanceToNode(leastDistantNode) + | ||
leastDistantNode.getAttachedNodes().get(node).getWeight(); | ||
if (currentDistance < pathData.GetDistanceToNode(node)){ | ||
pathData.distances.put(node, currentDistance); | ||
node.setPreviousNodeInPath(leastDistantNode); | ||
break; | ||
} | ||
} | ||
|
||
return pathData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package algorithms.Dijkstra; | ||
|
||
import elements.GraphNode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* 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; | ||
HashMap<GraphNode, Double> distances; | ||
|
||
public PathData(ArrayList<GraphNode> nodes, GraphNode sourceNode, GraphNode targetNode) { | ||
this.sourceNode = sourceNode; | ||
this.targetNode = targetNode; | ||
distances = new HashMap<>(); | ||
for (GraphNode node : nodes) | ||
distances.put(node, Double.POSITIVE_INFINITY); | ||
sourceNode.setPreviousNodeInPath(null); | ||
distances.put(sourceNode, 0.0); | ||
} | ||
|
||
/** | ||
* @return | ||
*/ | ||
public ArrayList<GraphNode> GetPathNodesToTargetNode() { | ||
return null; | ||
} | ||
|
||
/** | ||
* @return | ||
*/ | ||
public ArrayList<GraphNode> GetPathNodesToNode(GraphNode node) { | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* @return | ||
*/ | ||
public double GetDistanceToNode(GraphNode node) { | ||
return 0; | ||
} | ||
|
||
public GraphNode GetClosestNode(ArrayList<GraphNode> nodes) { | ||
double minDistance = Double.POSITIVE_INFINITY; | ||
|
||
GraphNode closestNode = sourceNode; | ||
for (GraphNode graphNode : nodes) { | ||
if (graphNode != sourceNode) { | ||
double distance = distances.get(graphNode); | ||
if (distance < minDistance) { | ||
minDistance = distance; | ||
closestNode = graphNode; | ||
} | ||
} | ||
} | ||
return closestNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters