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