Skip to content

Commit a8afe9b

Browse files
authored
Merge pull request #1 from rabestro/develop
Develop
2 parents 4d14c5d + 88e904c commit a8afe9b

File tree

9 files changed

+187
-1
lines changed

9 files changed

+187
-1
lines changed

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/Main.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import algorithm.Dijkstras;
2+
import algorithm.Graph;
3+
4+
import java.util.Map;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
System.out.println("Hello!");
9+
var algorithm = new Dijkstras<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");
17+
18+
System.out.println(route);
19+
System.out.println(graph.getDistance(route));
20+
21+
route = algorithm.findPath(graph, "A", "B");
22+
System.out.println(route);
23+
System.out.println(graph.getDistance(route));
24+
25+
route = algorithm.findPath(graph, "B", "A");
26+
System.out.println(route);
27+
System.out.println(graph.getDistance(route));
28+
29+
}
30+
31+
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package algorithm;
2+
3+
import java.util.List;
4+
5+
public interface Algorithm<T> {
6+
List<T> findPath(Graph<T> graph, T source, T target);
7+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package algorithm;
2+
3+
import java.util.*;
4+
import java.util.stream.Stream;
5+
6+
public class Dijkstras<T> implements Algorithm<T> {
7+
8+
@Override
9+
public List<T> findPath(Graph<T> graph, T source, T target) {
10+
final var queue = new LinkedList<T>();
11+
final var visited = new HashSet<T>();
12+
final var distances = new HashMap<T, Double>();
13+
final var previous = new HashMap<T, T>();
14+
queue.add(source);
15+
16+
while (!queue.isEmpty()) {
17+
final var prev = queue.pollFirst();
18+
final var edges = graph.nodes().get(prev);
19+
edges.forEach((node, time) -> {
20+
final var distance = distances.getOrDefault(prev, .0) + time.doubleValue();
21+
if (!visited.contains(node)) {
22+
queue.add(node);
23+
visited.add(node);
24+
}
25+
if (distance < distances.getOrDefault(node, Double.MAX_VALUE)) {
26+
previous.put(node, prev);
27+
distances.put(node, distance);
28+
}
29+
});
30+
}
31+
32+
final var path = new LinkedList<T>();
33+
Stream.iterate(target, Objects::nonNull, previous::get).forEach(path::addFirst);
34+
return path;
35+
}
36+
37+
}

src/main/java/algorithm/Graph.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package algorithm;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public record Graph<T>(Map<T, Map<T, Number>> nodes) {
7+
8+
public double getDistance(List<T> path) {
9+
double distance = 0;
10+
for (int i = 1; i < path.size(); ++i) {
11+
final var previous = nodes.get(path.get(i - 1));
12+
distance += previous.get(path.get(i)).doubleValue();
13+
}
14+
return distance;
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package algorithm
2+
3+
import spock.lang.Specification
4+
import spock.lang.Subject
5+
import spock.lang.Unroll
6+
7+
class DijkstrasSpec extends Specification {
8+
9+
static final SAMPLE_ONE = [
10+
A: [B: 7, C: 2],
11+
B: [A: 3, C: 5],
12+
C: [A: 1, B: 3]
13+
] as Graph<String>
14+
15+
@Subject
16+
def algorithm = new Dijkstras<String>()
17+
18+
@Unroll("from #source to #target the time is #time and the path is #expected")
19+
def 'should find a route for sample one'() {
20+
given:
21+
def graph = SAMPLE_ONE
22+
23+
when:
24+
def path = algorithm.findPath(graph, source, target)
25+
26+
then:
27+
path == expected
28+
29+
where:
30+
source | target || time | expected
31+
'A' | 'B' || 7 | ['A', 'B']
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package algorithm
2+
3+
import spock.lang.Specification
4+
import spock.lang.Unroll
5+
6+
class GraphSpec extends Specification {
7+
8+
@Unroll("distance is #distance for path #path")
9+
def "should calculate distance"() {
10+
given:
11+
def graph = new Graph([
12+
A: [B: 7, C: 2],
13+
B: [A: 3, C: 5],
14+
C: [A: 1, B: 3]
15+
])
16+
17+
expect:
18+
graph.getDistance(path) == distance as double
19+
20+
where:
21+
path | distance
22+
['A'] | 0
23+
['B'] | 0
24+
['C'] | 0
25+
['A', 'B'] | 7
26+
['B', 'C'] | 5
27+
['A', 'B', 'C'] | 12
28+
['C', 'A'] | 1
29+
['A', 'B', 'C', 'A'] | 13
30+
}
31+
32+
}

0 commit comments

Comments
 (0)