Skip to content

Commit ffafb2a

Browse files
authored
Merge pull request #5 from rabestro/develop
Updated Main
2 parents beaf692 + 3617986 commit ffafb2a

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
This project was created to test graph search algorithms. There are implementations and tests for two algorithms:
44

5-
- Breadth-first search
6-
- Dijkstra's Algorithm
5+
- [Breadth-first search](src/main/java/algorithm/BreadthFirstSearch.java)
6+
- [Dijkstra's Algorithm](src/main/java/algorithm/DijkstrasAlgorithm.java)
77

88
## Technical specifications
99

src/main/java/Main.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
1-
import algorithm.Dijkstras;
1+
import algorithm.BreadthFirstSearch;
2+
import algorithm.DijkstrasAlgorithm;
23
import algorithm.Graph;
4+
import algorithm.SearchAlgorithm;
35

46
import java.util.Map;
57

68
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");
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<>();
1721

18-
System.out.println(route);
19-
System.out.println(graph.getDistance(route));
22+
public static void main(String[] args) {
23+
System.out.println(complex);
2024

21-
route = algorithm.findPath(graph, "A", "B");
22-
System.out.println(route);
23-
System.out.println(graph.getDistance(route));
25+
printRoute(complex, "D", "C");
2426

25-
route = algorithm.findPath(graph, "B", "A");
26-
System.out.println(route);
27-
System.out.println(graph.getDistance(route));
27+
printRoute(complex, "A", "G");
2828

29+
printRoute(complex, "D", "H");
2930
}
3031

31-
32+
private static void printRoute(final Graph<String> graph, final String source, final String target) {
33+
final var routeOne = shortest.findPath(graph, source, target);
34+
final var routeTwo = fastest.findPath(graph, source, target);
35+
final var message = """
36+
37+
Find the path from %s to %s
38+
- the shortest take %.0f time and the path is %s
39+
- the fastest take %.0f time and the path is %s"""
40+
.formatted(
41+
source, target,
42+
graph.getDistance(routeOne), routeOne,
43+
graph.getDistance(routeTwo), routeTwo);
44+
45+
System.out.println(message);
46+
}
3247
}

src/main/java/algorithm/Dijkstras.java renamed to src/main/java/algorithm/DijkstrasAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Objects;
88
import java.util.stream.Stream;
99

10-
public class Dijkstras<T> implements SearchAlgorithm<T> {
10+
public class DijkstrasAlgorithm<T> implements SearchAlgorithm<T> {
1111

1212
@Override
1313
public List<T> findPath(Graph<T> graph, T source, T target) {

src/test/groovy/algorithm/DijkstrasSpec.groovy renamed to src/test/groovy/algorithm/DijkstrasAlgorithmSpec.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import spock.lang.Specification
44
import spock.lang.Subject
55
import spock.lang.Unroll
66

7-
class DijkstrasSpec extends Specification {
7+
class DijkstrasAlgorithmSpec extends Specification {
88
@Subject
9-
def algorithm = new Dijkstras<String>()
9+
def algorithm = new DijkstrasAlgorithm<String>()
1010

1111
@Unroll("from #source to #target the time is #time and the path is #fastest")
1212
def 'should find a route for a simple graph'() {

0 commit comments

Comments
 (0)