From 445ec73e143fdda523cf298a97f0ec3ad7e41b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sun, 26 Dec 2021 22:12:39 +0200 Subject: [PATCH 1/2] Update Main --- .idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 4 +- 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 .idea/uiDesigner.xml diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 2b3e4c3..140eca1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -35,8 +35,8 @@ private static void printRoute(final Graph graph, final String source, f final var message = """ Find the path from %s to %s - - the shortest take %.0f time and the path is %s - - the fastest take %.0f time and the path is %s""" + - the shortest take %.0f min and the path is %s + - the fastest take %.0f min and the path is %s""" .formatted( source, target, graph.getDistance(routeOne), routeOne, From ea45ce509547a125cbdb810c5a143a88f42812f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sun, 26 Dec 2021 22:24:33 +0200 Subject: [PATCH 2/2] Add SearchAlgorithmSpec --- .../algorithm/SearchAlgorithmSpec.groovy | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/groovy/algorithm/SearchAlgorithmSpec.groovy diff --git a/src/test/groovy/algorithm/SearchAlgorithmSpec.groovy b/src/test/groovy/algorithm/SearchAlgorithmSpec.groovy new file mode 100644 index 0000000..29a6ca6 --- /dev/null +++ b/src/test/groovy/algorithm/SearchAlgorithmSpec.groovy @@ -0,0 +1,56 @@ +package algorithm + +import spock.lang.Specification +import spock.lang.Subject +import spock.lang.Unroll + +class SearchAlgorithmSpec extends Specification { + @Subject + def shortest = new BreadthFirstSearch() + + @Subject + def fastest = new DijkstrasAlgorithm() + + @Unroll("from #source to #target the time is #time and the fastest path is #fastestPath") + def 'should find a route for a complex graph'() { + given: + def graph = new Graph([ + A: [B: 5, H: 2], + B: [A: 5, C: 7], + C: [B: 7, D: 3, G: 4], + D: [C: 20, E: 4], + E: [F: 5], + F: [G: 6], + G: [C: 4], + H: [G: 3] + ]) + + when: + def routeOne = shortest.findPath(graph, source, target) + + and: + def routeTwo = fastest.findPath(graph, source, target) + + then: + routeOne == shortestPath + + and: + routeTwo == fastestPath + + where: + source | target || time | shortestPath | fastestPath + 'A' | 'A' || 0 | ['A'] | ['A'] + 'B' | 'B' || 0 | ['B'] | ['B'] + 'A' | 'B' || 5 | ['A', 'B'] | ['A', 'B'] + 'B' | 'A' || 5 | ['B', 'A'] | ['B', 'A'] + 'A' | 'C' || 9 | ['A', 'B', 'C'] | ['A', 'H', 'G', 'C'] + 'C' | 'A' || 12 | ['C', 'B', 'A'] | ['C', 'B', 'A'] + 'A' | 'G' || 5 | ['A', 'H', 'G'] | ['A', 'H', 'G'] + 'C' | 'D' || 3 | ['C', 'D'] | ['C', 'D'] + 'D' | 'C' || 19 | ['D', 'C'] | ['D', 'E', 'F', 'G', 'C'] + 'B' | 'D' || 10 | ['B', 'C', 'D'] | ['B', 'C', 'D'] + 'D' | 'B' || 26 | ['D', 'C', 'B'] | ['D', 'E', 'F', 'G', 'C', 'B'] + 'D' | 'H' || 33 | ['D', 'C', 'B', 'A', 'H'] | ['D', 'E', 'F', 'G', 'C', 'B', 'A', 'H'] + } + +}