Implement Dijkstras algorithm
This commit is contained in:
parent
90ad88ee2f
commit
53756b586c
|
@ -2,26 +2,74 @@ package ch.nuth.zhaw.exbox;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
public class RouteServer implements CommandExecutor {
|
public class RouteServer implements CommandExecutor {
|
||||||
/**
|
/**
|
||||||
build the graph given a text file with the topology
|
* build the graph given a text file with the topology
|
||||||
*/
|
*/
|
||||||
public Graph<DijkstraNode, Edge> createGraph(String topo) throws Exception {
|
public Graph<DijkstraNode, Edge> createGraph(String topo) throws Exception {
|
||||||
// TODO implement
|
// TODO implement
|
||||||
|
Graph<DijkstraNode, Edge> graph = new AdjListGraph<>(DijkstraNode.class, Edge.class);
|
||||||
|
|
||||||
|
for (String line : topo.split("\r?\n")) {
|
||||||
|
String[] attributes = line.split(" ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (int i = 0; i <= 1; i++) {
|
||||||
|
graph.addEdge(attributes[i], attributes[1 - i], Double.parseDouble(attributes[2]));
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new Exception(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
apply the dijkstra algorithm
|
* apply the dijkstra algorithm
|
||||||
*/
|
*/
|
||||||
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
|
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
|
||||||
// TODO implement
|
Queue<DijkstraNode> pendingNodes = new PriorityQueue<>();
|
||||||
|
DijkstraNode start = graph.findNode(from);
|
||||||
|
DijkstraNode end = graph.findNode(to);
|
||||||
|
start.setDist(0);
|
||||||
|
pendingNodes.add(start);
|
||||||
|
|
||||||
|
while (!pendingNodes.isEmpty()) {
|
||||||
|
DijkstraNode current = pendingNodes.poll();
|
||||||
|
current.setMark(true);
|
||||||
|
|
||||||
|
if (current == end) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Edge edge : current.getEdges()) {
|
||||||
|
if (edge.getDest() instanceof DijkstraNode node &&
|
||||||
|
!node.getMark()) {
|
||||||
|
double dist = current.getDist() + edge.getWeight();
|
||||||
|
|
||||||
|
if (!pendingNodes.contains(node) ||
|
||||||
|
dist < node.getDist()) {
|
||||||
|
node.setDist(dist);
|
||||||
|
node.setPrev(current);
|
||||||
|
|
||||||
|
if (pendingNodes.contains(node)) {
|
||||||
|
pendingNodes.remove(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingNodes.add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
find the route in the graph after applied dijkstra
|
* find the route in the graph after applied dijkstra
|
||||||
the route should be returned with the start town first
|
* the route should be returned with the start town first
|
||||||
*/
|
*/
|
||||||
public List<DijkstraNode> getRoute(Graph<DijkstraNode, Edge> graph, String to) {
|
public List<DijkstraNode> getRoute(Graph<DijkstraNode, Edge> graph, String to) {
|
||||||
List<DijkstraNode> route = new LinkedList<>();
|
List<DijkstraNode> route = new LinkedList<>();
|
||||||
|
@ -39,7 +87,9 @@ public class RouteServer implements CommandExecutor {
|
||||||
List<DijkstraNode> route = getRoute(graph, "Lugano");
|
List<DijkstraNode> route = getRoute(graph, "Lugano");
|
||||||
// generate result string
|
// generate result string
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (DijkstraNode rt : route) builder.append(rt).append("\n");
|
for (DijkstraNode rt : route) {
|
||||||
|
builder.append(rt).append("\n");
|
||||||
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue