|
| 1 | +package lv.id.jc |
| 2 | + |
| 3 | +import algorithm.BreadthFirstSearch |
| 4 | +import algorithm.DijkstrasAlgorithm |
| 5 | +import algorithm.Graph |
| 6 | +import algorithm.SearchAlgorithm |
| 7 | + |
| 8 | +class App { |
| 9 | + private static final Graph<String> complex = new Graph<>(Map.of( |
| 10 | + "A", Map.of("B", 5, "H", 2), |
| 11 | + "B", Map.of("A", 5, "C", 7), |
| 12 | + "C", Map.of("B", 7, "D", 3, "G", 4), |
| 13 | + "D", Map.of("C", 20, "E", 4), |
| 14 | + "E", Map.of("F", 5), |
| 15 | + "F", Map.of("G", 6), |
| 16 | + "G", Map.of("C", 4), |
| 17 | + "H", Map.of("G", 3) |
| 18 | + )) |
| 19 | + private static final SearchAlgorithm<String> fastest = new DijkstrasAlgorithm<>() |
| 20 | + private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>() |
| 21 | + |
| 22 | + static void main(String[] args) { |
| 23 | + System.out.println(complex) |
| 24 | + |
| 25 | + printRoute(complex, "D", "C") |
| 26 | + printRoute(complex, "A", "G") |
| 27 | + printRoute(complex, "D", "H") |
| 28 | + } |
| 29 | + |
| 30 | + private static void printRoute(final Graph<String> graph, final String source, final String target) { |
| 31 | + final var routeOne = shortest.findPath(graph, source, target) |
| 32 | + final var routeTwo = fastest.findPath(graph, source, target) |
| 33 | + final var message = """ |
| 34 | + Find the path from %s to %s |
| 35 | + - the shortest take %.0f min and the path is %s |
| 36 | + - the fastest take %.0f min and the path is %s""" |
| 37 | + .formatted( |
| 38 | + source, target, |
| 39 | + graph.getDistance(routeOne), routeOne, |
| 40 | + graph.getDistance(routeTwo), routeTwo) |
| 41 | + |
| 42 | + System.out.println(message) |
| 43 | + } |
| 44 | +} |
0 commit comments