Skip to content

Commit 3617986

Browse files
committed
Update Main
1 parent 4148c42 commit 3617986

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/main/java/Main.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
1+
import algorithm.BreadthFirstSearch;
12
import algorithm.DijkstrasAlgorithm;
23
import algorithm.Graph;
4+
import algorithm.SearchAlgorithm;
35

46
import java.util.Map;
57

68
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<>();
1721

18-
System.out.println(route);
19-
System.out.println(graph.getDistance(route));
22+
public static void main(String[] args) {
23+
System.out.println(complex);
2024

21-
route = algorithm.findPath(graph, "A", "B");
22-
System.out.println(route);
23-
System.out.println(graph.getDistance(route));
25+
printRoute(complex, "D", "C");
2426

25-
route = algorithm.findPath(graph, "B", "A");
26-
System.out.println(route);
27-
System.out.println(graph.getDistance(route));
27+
printRoute(complex, "A", "G");
2828

29+
printRoute(complex, "D", "H");
2930
}
3031

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+
}
3247
}

0 commit comments

Comments
 (0)