Skip to content

Commit

Permalink
Made Many Changes
Browse files Browse the repository at this point in the history
Implemented PathData And Dijkstra Algorithm but not tested yet.
  • Loading branch information
OmidRezaei committed May 12, 2019
1 parent 29e2cff commit e3ea377
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 51 deletions.
Binary file modified Diagrams/PathTaker.vsdx
Binary file not shown.
37 changes: 36 additions & 1 deletion Graph/src/algorithms/Dijkstra/DijkstraAlgorithm.java
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;
}
}
21 changes: 18 additions & 3 deletions Graph/src/algorithms/Dijkstra/DijkstraAlgorithmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import algorithms.GraphBuilder;
import elements.Graph;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -15,11 +16,25 @@ void SetUp() {

}

@AfterEach
void TearDown() {

}

@Test
void Test1() {
void GraphWithNoNodes_ThrowsArgumentException() {
graph = GraphBuilder.Build().WithNodes(0).getGraph();

Assertions.assertThrows(IllegalArgumentException.class,
() -> DijkstraAlgorithm.FindShortestPath(graph, "0", "0"));
Assertions.assertThrows(ArrayStoreException.class, () -> DijkstraAlgorithm.FindShortestPath(graph, "0", "0"));
}

@Test
void GraphWith1Node_ReturnsPathWithOneNode() {
graph = GraphBuilder.Build().WithNodes(1).getGraph();

PathData pathData = DijkstraAlgorithm.FindShortestPath(graph, "0", "0");

Assertions.assertEquals(1, pathData.distances.size());
Assertions.assertEquals(0, pathData.GetDistanceToNode(graph.getNode("0")));
}
}
63 changes: 63 additions & 0 deletions Graph/src/algorithms/Dijkstra/PathData.java
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;
}
}
2 changes: 1 addition & 1 deletion Graph/src/algorithms/GraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static GraphBuilder Build() {

public GraphBuilder WithNodes(int nodeCount) {
for (int index = 0; index < nodeCount; index++)
graph.addNode(String.format("%d", nodeCount));
graph.addNode(index + "");

return this;
}
Expand Down
59 changes: 13 additions & 46 deletions Graph/src/elements/Graph.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package elements;

import java.util.ArrayList;
import java.util.List;

/**
* The type Graph.
*/
public class Graph {
private List<GraphNode> nodes = new ArrayList<>();
private List<GraphEdge> edges = new ArrayList<>();
private ArrayList<GraphNode> nodes = new ArrayList<>();
private ArrayList<GraphEdge> edges = new ArrayList<>();

/**
* Gets nodes.
*
* @return the nodes
*/

public List<GraphNode> getNodes() {
public ArrayList<GraphNode> getNodes() {
return nodes;
}

Expand All @@ -26,7 +25,7 @@ public List<GraphNode> getNodes() {
* @param nodes the nodes
*/

public void setNodes(List<GraphNode> nodes) {
public void setNodes(ArrayList<GraphNode> nodes) {
this.nodes = nodes;
}

Expand All @@ -36,7 +35,7 @@ public void setNodes(List<GraphNode> nodes) {
* @return the edges
*/

public List<GraphEdge> getEdges() {
public ArrayList<GraphEdge> getEdges() {
return edges;
}

Expand All @@ -46,7 +45,7 @@ public List<GraphEdge> getEdges() {
* @param edges the edges
*/

public void setEdges(List<GraphEdge> edges) {
public void setEdges(ArrayList<GraphEdge> edges) {
this.edges = edges;
}

Expand All @@ -57,16 +56,9 @@ public void setEdges(List<GraphEdge> edges) {
*/

public GraphNode addNode(String id) {
/*
* Instantiate new graph node and add it
* to the main nodes array list
*/
GraphNode newNode = new GraphNode(id);
nodes.add(newNode);

/*
* Return the new node
*/
return newNode;
}

Expand All @@ -78,22 +70,11 @@ public GraphNode addNode(String id) {
*/

public GraphEdge addDirectionalEdge(double weight, GraphNode source, GraphNode target) {
/*
* Instantiate a new graph edge and
* add it to the main edges array list
*/
GraphEdge newEdge = new GraphEdge(weight, source, target);
edges.add(newEdge);

/*
* Add the target node to the attached nodes array list
* of the source node
*/
attachNodes(source, target, newEdge);

/*
* Return the new graph edge
*/
return newEdge;
}

Expand All @@ -106,26 +87,15 @@ public GraphEdge addDirectionalEdge(double weight, GraphNode source, GraphNode t
*/

public NonDirectionalEdge addNonDirectionalEdge(double weight, GraphNode source, GraphNode target) {
/*
* Instantiate two new graph edges, one from the source node to the target
* and the other one from target node to the source.
* Then add both of the edges to the main edges array list
*/

GraphEdge edgeOne = addDirectionalEdge(weight, source, target);
GraphEdge edgeTwo = addDirectionalEdge(weight, target, source);
edges.add(edgeOne);
edges.add(edgeTwo);

/*
* Attach the nodes together
*/
attachNodes(source, target, edgeOne);
attachNodes(target, source, edgeTwo);

/*
* Instantiate a new non-directional edge
* with the two directional edges already instantiated and return the edge
*/
return new NonDirectionalEdge(edgeOne, edgeTwo);
}

Expand Down Expand Up @@ -174,18 +144,9 @@ public void removeNode(GraphNode node) {
*/

public void removeDirectionalEdge(GraphEdge edge) {
/*
* Remove the edge from the main graph edges array list
*/
edges.remove(edge);

/*
* Remove the target node from the attached nodes array list
* of the source node
*/
detachNodes(edge.getSourceNode(), edge.getTargetNode());


}

/**
Expand All @@ -197,4 +158,10 @@ public void removeDirectionalEdge(GraphEdge edge) {
public void removeNonDirectionalEdge(NonDirectionalEdge edge) {

}

public GraphNode getNode(String nodeIdentifier) {
for (GraphNode node : nodes)
if (node.getIdentifier().equals(nodeIdentifier)) return node;
return null;
}
}

0 comments on commit e3ea377

Please sign in to comment.