Sauberer Routenplaner mit Dijkstra fertig implementiert.
parent
a2040c3afe
commit
245b235d43
|
@ -1,6 +1,7 @@
|
|||
package de.th_mannheim.informatik.routenplaner;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Stack;
|
||||
|
||||
import de.th_mannheim.informatik.routenplaner.domain.Routeplaner;
|
||||
|
||||
|
@ -9,9 +10,14 @@ public class NaviApp {
|
|||
public static void main(String[] args) throws FileNotFoundException {
|
||||
Routeplaner map = new Routeplaner();
|
||||
|
||||
System.out.println(map.getNumberOfCities());
|
||||
System.out.println(map.getNumberOfCities() + " Städte enthalten.");
|
||||
|
||||
map.planRoute("Mannheim", "Lübeck");
|
||||
Stack<String> route = map.planRoute("Mannheim", "Lübeck");
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Gefundene Route:");
|
||||
while (!route.isEmpty())
|
||||
System.out.println(route.pop());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +1,80 @@
|
|||
package de.th_mannheim.informatik.routenplaner.domain;
|
||||
|
||||
import java.util.TreeSet;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class City {
|
||||
public class City implements Comparable<City>{
|
||||
private String name;
|
||||
private int distance;
|
||||
private boolean explored;
|
||||
private City predecessor;
|
||||
|
||||
private TreeSet<Street> neighbors = new TreeSet<>();
|
||||
private LinkedList<Road> neighbors = new LinkedList<>();
|
||||
|
||||
public City(String name) {
|
||||
this.name = name;
|
||||
this.reset();
|
||||
}
|
||||
|
||||
public void addConnection(City stadt2, int distance) {
|
||||
neighbors.add(new Street(this, stadt2, distance));
|
||||
neighbors.add(new Road(this, stadt2, distance));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public TreeSet<Street> getNeighbors() {
|
||||
public LinkedList<Road> getConnections() {
|
||||
return neighbors;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
distance = Integer.MAX_VALUE;
|
||||
explored = false;
|
||||
predecessor = null;
|
||||
}
|
||||
|
||||
public int getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void checkAndSetDistance(City start, City target, int value) {
|
||||
if (this.distance > value) {
|
||||
this.distance = value;
|
||||
target.setPredecessor(start);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isExplored() {
|
||||
return explored;
|
||||
}
|
||||
|
||||
public void setExplored() {
|
||||
explored = true;
|
||||
}
|
||||
|
||||
public void setPredecessor(City predecessor) {
|
||||
this.predecessor = predecessor;
|
||||
}
|
||||
|
||||
public City getPredecessor() {
|
||||
return predecessor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(City other) {
|
||||
int diff = this.distance - other.distance;
|
||||
|
||||
if (this == other)
|
||||
return 0;
|
||||
|
||||
if (diff != 0)
|
||||
return diff;
|
||||
|
||||
// if two Cities happen to have the same distance, sort alphabetically
|
||||
return this.name.compareTo(other.name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name + " " + distance;
|
||||
}
|
||||
}
|
|
@ -1,31 +1,17 @@
|
|||
package de.th_mannheim.informatik.routenplaner.domain;
|
||||
|
||||
public class Street implements Comparable<Street> {
|
||||
public class Road {
|
||||
private final City start;
|
||||
private final City destination;
|
||||
|
||||
private final int distance;
|
||||
|
||||
public Street(City start, City destination, int distance) {
|
||||
public Road(City start, City destination, int distance) {
|
||||
super();
|
||||
this.start = start;
|
||||
this.destination = destination;
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Street v) {
|
||||
int diff = this.distance - v.distance;
|
||||
|
||||
if (diff != 0)
|
||||
return diff;
|
||||
|
||||
if (this.start.equals(v.start) && this.destination.equals(v.destination))
|
||||
return 0;
|
||||
|
||||
// wg. Nutzung von TreeSet
|
||||
return 1;
|
||||
}
|
||||
|
||||
public City getStart() {
|
||||
return start;
|
|
@ -2,10 +2,11 @@ package de.th_mannheim.informatik.routenplaner.domain;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Routeplaner {
|
||||
private HashMap<String, City> cities = new HashMap<>();
|
||||
|
@ -18,13 +19,47 @@ public class Routeplaner {
|
|||
return cities.size();
|
||||
}
|
||||
|
||||
public Deque<String> planRoute(String start, String destination) {
|
||||
Deque<String> waypoints = new ArrayDeque<>();
|
||||
public Stack<String> planRoute(String start, String destination) {
|
||||
TreeSet<City> candidates = new TreeSet<>();
|
||||
|
||||
System.out.println(cities.get("Mannheim").getNeighbors().size());
|
||||
City startCity = cities.get(start);
|
||||
startCity.checkAndSetDistance(startCity, startCity, 0);
|
||||
LinkedList<Road> connections = startCity.getConnections();
|
||||
|
||||
this.processCandidates(startCity, connections, candidates);
|
||||
startCity.setExplored();
|
||||
|
||||
// ---------------------------------------
|
||||
|
||||
// iterate over graph
|
||||
while (!candidates.isEmpty()) {
|
||||
City current = candidates.removeFirst();
|
||||
|
||||
this.processCandidates(current, current.getConnections(), candidates);
|
||||
current.setExplored();
|
||||
}
|
||||
|
||||
Stack<String> waypoints = new Stack<>();
|
||||
City waypoint = cities.get(destination);
|
||||
while (!waypoint.getName().equals(start)) {
|
||||
waypoints.add(waypoint.toString());
|
||||
waypoint = waypoint.getPredecessor();
|
||||
}
|
||||
waypoints.add(waypoint.toString());
|
||||
|
||||
return waypoints;
|
||||
}
|
||||
|
||||
private void processCandidates(City start, LinkedList<Road> connections, TreeSet<City> candidates) {
|
||||
for (Road r : connections) {
|
||||
City other = r.getTarget();
|
||||
if (other.isExplored())
|
||||
continue;
|
||||
|
||||
other.checkAndSetDistance(start, other, start.getDistance() + r.getDistance());
|
||||
candidates.add(other);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadData() throws FileNotFoundException {
|
||||
final Scanner sc = new Scanner(new File("Routenplaner/resources/entfernungen.txt"));
|
||||
|
@ -61,6 +96,7 @@ public class Routeplaner {
|
|||
cities.put(name2, c2);
|
||||
}
|
||||
|
||||
// all roads go in both directions
|
||||
c1.addConnection(c2, distance);
|
||||
c2.addConnection(c1, distance);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue