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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

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

## Technical specifications

Expand Down
55 changes: 35 additions & 20 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import algorithm.Dijkstras;
import algorithm.BreadthFirstSearch;
import algorithm.DijkstrasAlgorithm;
import algorithm.Graph;
import algorithm.SearchAlgorithm;

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");
private static final Graph<String> complex = 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),
"D", Map.of("C", 20, "E", 4),
"E", Map.of("F", 5),
"F", Map.of("G", 6),
"G", Map.of("C", 4),
"H", Map.of("G", 3)
));
private static final SearchAlgorithm<String> fastest = new DijkstrasAlgorithm<>();
private static final SearchAlgorithm<String> shortest = new BreadthFirstSearch<>();

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

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

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

printRoute(complex, "D", "H");
}


private static void printRoute(final Graph<String> graph, final String source, final String target) {
final var routeOne = shortest.findPath(graph, source, target);
final var routeTwo = fastest.findPath(graph, source, target);
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"""
.formatted(
source, target,
graph.getDistance(routeOne), routeOne,
graph.getDistance(routeTwo), routeTwo);

System.out.println(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Objects;
import java.util.stream.Stream;

public class Dijkstras<T> implements SearchAlgorithm<T> {
public class DijkstrasAlgorithm<T> implements SearchAlgorithm<T> {

@Override
public List<T> findPath(Graph<T> graph, T source, T target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import spock.lang.Specification
import spock.lang.Subject
import spock.lang.Unroll

class DijkstrasSpec extends Specification {
class DijkstrasAlgorithmSpec extends Specification {
@Subject
def algorithm = new Dijkstras<String>()
def algorithm = new DijkstrasAlgorithm<String>()

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