Skip to content

Commit e9b1ee5

Browse files
authored
Merge pull request #7 from rabestro/develop
Add App.groovy
2 parents 9830813 + e8da450 commit e9b1ee5

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ repositories {
1111
}
1212

1313
dependencies {
14+
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
15+
1416
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
1517
testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
1618
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)