Add files for exercise 07

This commit is contained in:
Manuel Thalmann 2022-11-01 19:12:18 +01:00
parent 0033b66a11
commit aad53176f8
7 changed files with 221 additions and 0 deletions

14
Swiss.txt Normal file
View file

@ -0,0 +1,14 @@
Winterthur Zürich 25
Zürich Bern 126
Zürich Genf 277
Zürich Luzern 54
Zürich Chur 121
Zürich Berikon 16
Bern Genf 155
Genf Lugano 363
Lugano Luzern 206
Lugano Chur 152
Chur Luzern 146
Luzern Bern 97
Bern Berikon 102
Luzern Berikon 41

View file

@ -0,0 +1,53 @@
package ch.nuth.zhaw.exbox;
import java.util.LinkedList;
import java.util.List;
public class AdjListGraph<N extends Node, E extends Edge>
implements Graph<N, E> {
private final List<N> nodes = new LinkedList<>();
private final Class<N> nodeClazz;
private final Class<E> edgeClazz;
public AdjListGraph(Class<N> nodeClazz, Class<E> edgeClazz) {
this.nodeClazz = nodeClazz;
this.edgeClazz = edgeClazz;
}
/** füge Knoten hinzu, gebe alten zurück falls Knoten schon existiert */
public N addNode(String name) throws Throwable {
N node = findNode(name);
if (node == null) {
node = nodeClazz.getConstructor().newInstance();
node.setName(name);
nodes.add(node);
}
return node;
}
/** füge gerichtete Kante hinzu */
public void addEdge(String source, String dest, double weight) throws Throwable {
N src = addNode(source);
N dst = addNode(dest);
E edge = edgeClazz.getConstructor().newInstance();
edge.setDest(dst);
edge.setWeight(weight);
src.addEdge(edge);
}
/** finde den Knoten anhand seines Namens */
public N findNode(String name) {
for (N node : nodes) {
if (node.getName().equals(name)) {
return node;
}
}
return null;
}
/** Iterator über alle Knoten */
public Iterable<N> getNodes() {
return nodes;
}
}

View file

@ -0,0 +1,41 @@
package ch.nuth.zhaw.exbox;
class DijkstraNode extends Node implements Comparable<DijkstraNode> {
boolean mark;
DijkstraNode prev;
double dist;
public DijkstraNode() { }
public double getDist (){
return dist;
}
public void setDist(double dist) {
this.dist = dist;
}
public void setMark(boolean m) {
mark = m;
}
public boolean getMark() {
return mark;
}
public void setPrev(DijkstraNode p) {
prev = p;
}
public DijkstraNode getPrev() {
return prev;
}
public String toString() {
return getName() + " "+getDist();
}
public int compareTo(DijkstraNode n) {
return (int)(dist - n.dist);
}
}

View file

@ -0,0 +1,19 @@
package ch.nuth.zhaw.exbox;
public class Edge {
protected Node dest; // Zielknoten der Kante
protected double weight; // Kantengewicht
public Edge() {}
public Edge(Node dest, double weight) {
this.dest = dest;
this.weight = weight;
}
public void setDest(Node node) { this.dest = node; }
public Node getDest() { return dest; }
public void setWeight(double w) { this.weight = w; }
double getWeight() { return weight; }
}

View file

@ -0,0 +1,15 @@
package ch.nuth.zhaw.exbox;
public interface Graph<N, E> {
/** füge Knoten hinzu, tue nichts, falls Knoten schon existiert */
N addNode (String name) throws Throwable;
/** finde den Knoten anhand seines Namens */
N findNode(String name);
/** Iterator über alle Knoten des Graphen */
Iterable<N> getNodes();
/** füge gerichtete und gewichtete Kante hinzu */
void addEdge(String source, String dest, double weight) throws Throwable ;
}

View file

@ -0,0 +1,34 @@
package ch.nuth.zhaw.exbox;
import java.util.LinkedList;
import java.util.List;
public class Node {
protected String name; // Name
protected List<Edge> edges; // Kanten
public Node() {
edges = new LinkedList<>();
}
public Node(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Iterable<Edge> getEdges() {
return edges;
}
public void addEdge(Edge edge) {
edges.add(edge);
}
}

View file

@ -0,0 +1,45 @@
package ch.nuth.zhaw.exbox;
import java.util.LinkedList;
import java.util.List;
public class RouteServer implements CommandExecutor {
/**
build the graph given a text file with the topology
*/
public Graph<DijkstraNode, Edge> createGraph(String topo) throws Exception {
// TODO implement
}
/**
apply the dijkstra algorithm
*/
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
// TODO implement
}
/**
find the route in the graph after applied dijkstra
the route should be returned with the start town first
*/
public List<DijkstraNode> getRoute(Graph<DijkstraNode, Edge> graph, String to) {
List<DijkstraNode> route = new LinkedList<>();
DijkstraNode town = graph.findNode(to);
do {
route.add(0, town);
town = town.getPrev();
} while (town != null);
return route;
}
public String execute(String topo) throws Exception {
Graph<DijkstraNode, Edge> graph = createGraph(topo);
dijkstraRoute(graph, "Winterthur", "Lugano");
List<DijkstraNode> route = getRoute(graph, "Lugano");
// generate result string
StringBuilder builder = new StringBuilder();
for (DijkstraNode rt : route) builder.append(rt).append("\n");
return builder.toString();
}
}