Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'groovy'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
}

group 'lv.id.jc'
Expand All @@ -12,11 +13,22 @@ repositories {

dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
testImplementation 'org.codehaus.groovy:groovy-all:3.0.9'
}

test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import algorithm.DijkstrasAlgorithm
import algorithm.Graph
import algorithm.SearchAlgorithm

class App {
private static final Graph<String> complex = new Graph<>(Map.of(
class AppGroovy {
private static final Graph<String> COMPLEX_GRAPH = new Graph<>(Map.of(
"A", Map.of("B", 5, "H", 2),
"B", Map.of("A", 5, "C", 7),
"C", Map.of("B", 7, "D", 3, "G", 4),
Expand All @@ -20,11 +20,11 @@ class App {
private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>()

static void main(String[] args) {
System.out.println(complex)
System.out.println(COMPLEX_GRAPH)

printRoute(complex, "D", "C")
printRoute(complex, "A", "G")
printRoute(complex, "D", "H")
printRoute(COMPLEX_GRAPH, "D", "C")
printRoute(COMPLEX_GRAPH, "A", "G")
printRoute(COMPLEX_GRAPH, "D", "H")
}

private static void printRoute(final Graph<String> graph, final String source, final String target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package lv.id.jc;

import algorithm.BreadthFirstSearch;
import algorithm.DijkstrasAlgorithm;
import algorithm.Graph;
import algorithm.SearchAlgorithm;

import java.util.Map;

public class Main {
private static final Graph<String> complex = new Graph<>(Map.of(
public class AppJava {
private static final Graph<String> COMPLEX_GRAPH = new Graph<>(Map.of(
"A", Map.of("B", 5, "H", 2),
"B", Map.of("A", 5, "C", 7),
"C", Map.of("B", 7, "D", 3, "G", 4),
Expand All @@ -20,13 +22,11 @@ public class Main {
private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>();

public static void main(String[] args) {
System.out.println(complex);

printRoute(complex, "D", "C");

printRoute(complex, "A", "G");
System.out.println(COMPLEX_GRAPH);

printRoute(complex, "D", "H");
printRoute(COMPLEX_GRAPH, "D", "C");
printRoute(COMPLEX_GRAPH, "A", "G");
printRoute(COMPLEX_GRAPH, "D", "H");
}

private static void printRoute(final Graph<String> graph, final String source, final String target) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/lv/id/jc/AppKotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lv.id.jc

import algorithm.BreadthFirstSearch
import algorithm.DijkstrasAlgorithm
import algorithm.Graph
import algorithm.SearchAlgorithm

class AppKotlin {

companion object {
@JvmStatic
private val complexGraph = Graph(
mapOf(
"A" to mapOf("B" to 5, "H" to 2),
"B" to mapOf("A" to 5, "C" to 7),
"C" to mapOf("B" to 7, "D" to 3, "G" to 4),
"D" to mapOf("C" to 20, "E" to 4),
"E" to mapOf("F" to 5),
"F" to mapOf("G" to 6),
"G" to mapOf("C" to 4),
"H" to mapOf("G" to 3)
)
)

@JvmStatic
private val fastest: SearchAlgorithm<String> = DijkstrasAlgorithm()

@JvmStatic
private val shortest: SearchAlgorithm<String> = BreadthFirstSearch()

@JvmStatic
fun main(args: Array<String>) {
println(complexGraph)
printRoute(complexGraph, "D", "C")
printRoute(complexGraph, "A", "G")
printRoute(complexGraph, "D", "H")
}

@JvmStatic
private fun printRoute(graph: Graph<String>, source: String, target: String) {
val routeOne = shortest.findPath(graph, source, target)
val routeTwo = fastest.findPath(graph, source, target)

val message: String = String.format(
"%nFind the path from %s to %s%n" +
" - the shortest take %.0f min and the path is %s%n" +
" - the fastest take %.0f min and the path is %s",
source, target,
graph.getDistance(routeOne), routeOne,
graph.getDistance(routeTwo), routeTwo
)
println(message)
}
}


}