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
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import algorithm.Dijkstras;
import algorithm.Graph;

import java.util.Map;

public class Main {
public static void main(String[] args) {
System.out.println("Hello!");
var algorithm = new Dijkstras<String>();

var graph = new Graph<>(Map.of(
"A", Map.of("B", 5),
"B", Map.of("A", 7)
));

var route = algorithm.findPath(graph, "A", "A");

System.out.println(route);
System.out.println(graph.getDistance(route));

route = algorithm.findPath(graph, "A", "B");
System.out.println(route);
System.out.println(graph.getDistance(route));

route = algorithm.findPath(graph, "B", "A");
System.out.println(route);
System.out.println(graph.getDistance(route));

}


}
7 changes: 7 additions & 0 deletions src/main/java/algorithm/Algorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package algorithm;

import java.util.List;

public interface Algorithm<T> {
List<T> findPath(Graph<T> graph, T source, T target);
}
37 changes: 37 additions & 0 deletions src/main/java/algorithm/Dijkstras.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package algorithm;

import java.util.*;
import java.util.stream.Stream;

public class Dijkstras<T> implements Algorithm<T> {

@Override
public List<T> findPath(Graph<T> graph, T source, T target) {
final var queue = new LinkedList<T>();
final var visited = new HashSet<T>();
final var distances = new HashMap<T, Double>();
final var previous = new HashMap<T, T>();
queue.add(source);

while (!queue.isEmpty()) {
final var prev = queue.pollFirst();
final var edges = graph.nodes().get(prev);
edges.forEach((node, time) -> {
final var distance = distances.getOrDefault(prev, .0) + time.doubleValue();
if (!visited.contains(node)) {
queue.add(node);
visited.add(node);
}
if (distance < distances.getOrDefault(node, Double.MAX_VALUE)) {
previous.put(node, prev);
distances.put(node, distance);
}
});
}

final var path = new LinkedList<T>();
Stream.iterate(target, Objects::nonNull, previous::get).forEach(path::addFirst);
return path;
}

}
16 changes: 16 additions & 0 deletions src/main/java/algorithm/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package algorithm;

import java.util.List;
import java.util.Map;

public record Graph<T>(Map<T, Map<T, Number>> nodes) {

public double getDistance(List<T> path) {
double distance = 0;
for (int i = 1; i < path.size(); ++i) {
final var previous = nodes.get(path.get(i - 1));
distance += previous.get(path.get(i)).doubleValue();
}
return distance;
}
}
33 changes: 33 additions & 0 deletions src/test/groovy/algorithm/DijkstrasSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package algorithm

import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Unroll

class DijkstrasSpec extends Specification {

static final SAMPLE_ONE = [
A: [B: 7, C: 2],
B: [A: 3, C: 5],
C: [A: 1, B: 3]
] as Graph<String>

@Subject
def algorithm = new Dijkstras<String>()

@Unroll("from #source to #target the time is #time and the path is #expected")
def 'should find a route for sample one'() {
given:
def graph = SAMPLE_ONE

when:
def path = algorithm.findPath(graph, source, target)

then:
path == expected

where:
source | target || time | expected
'A' | 'B' || 7 | ['A', 'B']
}
}
32 changes: 32 additions & 0 deletions src/test/groovy/algorithm/GraphSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package algorithm

import spock.lang.Specification
import spock.lang.Unroll

class GraphSpec extends Specification {

@Unroll("distance is #distance for path #path")
def "should calculate distance"() {
given:
def graph = new Graph([
A: [B: 7, C: 2],
B: [A: 3, C: 5],
C: [A: 1, B: 3]
])

expect:
graph.getDistance(path) == distance as double

where:
path | distance
['A'] | 0
['B'] | 0
['C'] | 0
['A', 'B'] | 7
['B', 'C'] | 5
['A', 'B', 'C'] | 12
['C', 'A'] | 1
['A', 'B', 'C', 'A'] | 13
}

}