diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/BreadthFirstSearch.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/BreadthFirstSearch.html new file mode 100644 index 0000000..f8b744e --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/BreadthFirstSearch.html @@ -0,0 +1,194 @@ + + + + +BreadthFirstSearch + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + +

Class BreadthFirstSearch<T>

+
+
java.lang.Object +
lv.id.jc.algorithm.graph.BreadthFirstSearch<T>
+
+
+
+
Type Parameters:
+
T - the type of vertex id
+
+
+
All Implemented Interfaces:
+
SearchAlgorithm<T>
+
+
+
public class BreadthFirstSearch<T> +extends Object +implements SearchAlgorithm<T>
+
Algorithm for finding the shortest paths between nodes in a graph. + + The algorithm doesn't take into account the distance between nodes.
+
+
Since:
+
1.0
+
+
+
+ +
+
+
    + +
  • +
    +

    Constructor Details

    +
      +
    • +
      +

      BreadthFirstSearch

      +
      public BreadthFirstSearch()
      +
      +
    • +
    +
    +
  • + +
  • +
    +

    Method Details

    +
      +
    • +
      +

      findPath

      +
      public List<T> findPath(Graph<T> graph, + T source, + T target)
      +
      Description copied from interface: SearchAlgorithm
      +
      Find the path from the source node to the target
      +
      +
      Specified by:
      +
      findPath in interface SearchAlgorithm<T>
      +
      Parameters:
      +
      graph - The graph in which we search for the path
      +
      source - Search starting point identifier
      +
      target - Search finish point identifier
      +
      Returns:
      +
      Path found or empty list if path cannot be found
      +
      +
      +
    • +
    +
    +
  • +
+
+ +
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/DijkstrasAlgorithm.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/DijkstrasAlgorithm.html new file mode 100644 index 0000000..2625996 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/DijkstrasAlgorithm.html @@ -0,0 +1,194 @@ + + + + +DijkstrasAlgorithm + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + +

Class DijkstrasAlgorithm<T>

+
+
java.lang.Object +
lv.id.jc.algorithm.graph.DijkstrasAlgorithm<T>
+
+
+
+
Type Parameters:
+
T - the type of vertex id
+
+
+
All Implemented Interfaces:
+
SearchAlgorithm<T>
+
+
+
public class DijkstrasAlgorithm<T> +extends Object +implements SearchAlgorithm<T>
+
Algorithm for finding the fastest paths between nodes in a graph. +

+ The algorithm uses information about edge's distance to find the fastest path.

+
+
Since:
+
1.0
+
+
+
+ +
+
+
    + +
  • +
    +

    Constructor Details

    +
      +
    • +
      +

      DijkstrasAlgorithm

      +
      public DijkstrasAlgorithm()
      +
      +
    • +
    +
    +
  • + +
  • +
    +

    Method Details

    +
      +
    • +
      +

      findPath

      +
      public List<T> findPath(Graph<T> graph, + T source, + T target)
      +
      Description copied from interface: SearchAlgorithm
      +
      Find the path from the source node to the target
      +
      +
      Specified by:
      +
      findPath in interface SearchAlgorithm<T>
      +
      Parameters:
      +
      graph - The graph in which we search for the path
      +
      source - Search starting point identifier
      +
      target - Search finish point identifier
      +
      Returns:
      +
      Path found or empty list if path cannot be found
      +
      +
      +
    • +
    +
    +
  • +
+
+ +
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/Graph.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/Graph.html new file mode 100644 index 0000000..f3056ec --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/Graph.html @@ -0,0 +1,205 @@ + + + + +Graph + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + +

Interface Graph<T>

+
+
+
+
Type Parameters:
+
T - the type of vertex in this graph
+
+
+
Functional Interface:
+
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
+
+
+
@FunctionalInterface +public interface Graph<T>
+
An interface for weighted directed graph (network)
+
+
Since:
+
1.0
+
+
+
+
    + +
  • +
    +

    Method Summary

    +
    +
    +
    +
    +
    Modifier and Type
    +
    Method
    +
    Description
    +
    default Map<T,Number>
    +
    edges(T id)
    +
    +
    Edges of the given vertex
    +
    +
    default double
    + +
    +
    Calculate the distance for the given path
    +
    +
    static <T> Graph<T>
    +
    of(Map<T,Map<T,Number>> schema)
    +
    +
    Creates a Graph object by given schema
    +
    + + +
    +
    Schema of the graph
    +
    +
    +
    +
    +
    +
  • +
+
+
+
    + +
  • +
    +

    Method Details

    +
      +
    • +
      +

      schema

      +
      Map<T,Map<T,Number>> schema()
      +
      Schema of the graph
      +
      +
      Returns:
      +
      the graph scheme
      +
      +
      +
    • +
    • +
      +

      edges

      +
      default Map<T,Number> edges(T id)
      +
      Edges of the given vertex
      +
      +
      Parameters:
      +
      id - vertex
      +
      Returns:
      +
      all links for the given vertex
      +
      +
      +
    • +
    • +
      +

      getDistance

      +
      default double getDistance(List<T> path)
      +
      Calculate the distance for the given path
      +
      +
      Parameters:
      +
      path - the list of vertices representing the path
      +
      Returns:
      +
      distance for the given path as double
      +
      Throws:
      +
      NullPointerException - if path is incorrect and contains more than one vertex
      +
      +
      +
    • +
    • +
      +

      of

      +
      static <T> Graph<T> of(Map<T,Map<T,Number>> schema)
      +
      Creates a Graph object by given schema
      +
      +
      Type Parameters:
      +
      T - the type of vertex in this graph
      +
      Parameters:
      +
      schema - of the graph
      +
      Returns:
      +
      graph with given schema
      +
      +
      +
    • +
    +
    +
  • +
+
+ +
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/SearchAlgorithm.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/SearchAlgorithm.html new file mode 100644 index 0000000..52266d1 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/SearchAlgorithm.html @@ -0,0 +1,159 @@ + + + + +SearchAlgorithm + + + + + + + + + + + + + + + +
+ +
+
+ +
+ + +

Interface SearchAlgorithm<T>

+
+
+
+
Type Parameters:
+
T - the type of vertex id
+
+
+
All Known Implementing Classes:
+
BreadthFirstSearch, DijkstrasAlgorithm
+
+
+
Functional Interface:
+
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
+
+
+
@FunctionalInterface +public interface SearchAlgorithm<T>
+
A functional interface for graph search algorithm
+
+
Since:
+
1.0
+
+
+
+
    + +
  • +
    +

    Method Summary

    +
    +
    +
    +
    +
    Modifier and Type
    +
    Method
    +
    Description
    + +
    findPath(Graph<T> graph, + T source, + T target)
    +
    +
    Find the path from the source node to the target
    +
    +
    +
    +
    +
    +
  • +
+
+
+
    + +
  • +
    +

    Method Details

    +
      +
    • +
      +

      findPath

      +
      List<T> findPath(Graph<T> graph, + T source, + T target)
      +
      Find the path from the source node to the target
      +
      +
      Parameters:
      +
      graph - The graph in which we search for the path
      +
      source - Search starting point identifier
      +
      target - Search finish point identifier
      +
      Returns:
      +
      Path found or empty list if path cannot be found
      +
      +
      +
    • +
    +
    +
  • +
+
+ +
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/BreadthFirstSearch.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/BreadthFirstSearch.html new file mode 100644 index 0000000..3f01bb4 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/BreadthFirstSearch.html @@ -0,0 +1,58 @@ + + + + +Uses of Class lv.id.jc.algorithm.graph.BreadthFirstSearch + + + + + + + + + + + + + + + +
+ +
+
+
+

Uses of Class
lv.id.jc.algorithm.graph.BreadthFirstSearch

+
+No usage of lv.id.jc.algorithm.graph.BreadthFirstSearch
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/DijkstrasAlgorithm.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/DijkstrasAlgorithm.html new file mode 100644 index 0000000..d96e40f --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/DijkstrasAlgorithm.html @@ -0,0 +1,58 @@ + + + + +Uses of Class lv.id.jc.algorithm.graph.DijkstrasAlgorithm + + + + + + + + + + + + + + + +
+ +
+
+
+

Uses of Class
lv.id.jc.algorithm.graph.DijkstrasAlgorithm

+
+No usage of lv.id.jc.algorithm.graph.DijkstrasAlgorithm
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/Graph.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/Graph.html new file mode 100644 index 0000000..cd07958 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/Graph.html @@ -0,0 +1,101 @@ + + + + +Uses of Interface lv.id.jc.algorithm.graph.Graph + + + + + + + + + + + + + + + +
+ +
+
+
+

Uses of Interface
lv.id.jc.algorithm.graph.Graph

+
+
+ +
+
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/SearchAlgorithm.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/SearchAlgorithm.html new file mode 100644 index 0000000..699ccf5 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/class-use/SearchAlgorithm.html @@ -0,0 +1,83 @@ + + + + +Uses of Interface lv.id.jc.algorithm.graph.SearchAlgorithm + + + + + + + + + + + + + + + +
+ +
+
+
+

Uses of Interface
lv.id.jc.algorithm.graph.SearchAlgorithm

+
+
+ +
+
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-summary.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-summary.html new file mode 100644 index 0000000..7f67904 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-summary.html @@ -0,0 +1,107 @@ + + + + +lv.id.jc.algorithm.graph + + + + + + + + + + + + + + + +
+ +
+
+
+ +

Package lv.id.jc.algorithm.graph

+
+
+
package lv.id.jc.algorithm.graph
+
+
This package contains graph pathfinding algorithms.
+
+
+
    +
  • +
    +
    +
    +
    +
    Class
    +
    Description
    + +
    +
    Algorithm for finding the shortest paths between nodes in a graph.
    +
    + +
    +
    Algorithm for finding the fastest paths between nodes in a graph.
    +
    + +
    +
    An interface for weighted directed graph (network)
    +
    + +
    +
    A functional interface for graph search algorithm
    +
    +
    +
    +
    +
  • +
+
+
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-tree.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-tree.html new file mode 100644 index 0000000..df929a8 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-tree.html @@ -0,0 +1,76 @@ + + + + +lv.id.jc.algorithm.graph Class Hierarchy + + + + + + + + + + + + + + + +
+ +
+
+
+

Hierarchy For Package lv.id.jc.algorithm.graph

+
+
+

Class Hierarchy

+ +
+
+

Interface Hierarchy

+ +
+
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-use.html b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-use.html new file mode 100644 index 0000000..cb55be8 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/lv/id/jc/algorithm/graph/package-use.html @@ -0,0 +1,79 @@ + + + + +Uses of Package lv.id.jc.algorithm.graph + + + + + + + + + + + + + + + +
+ +
+
+
+

Uses of Package
lv.id.jc.algorithm.graph

+
+
+ +
+
+
+
+ + diff --git a/docs/api/lv.id.jc.algorithm/module-summary.html b/docs/api/lv.id.jc.algorithm/module-summary.html new file mode 100644 index 0000000..1aa1d34 --- /dev/null +++ b/docs/api/lv.id.jc.algorithm/module-summary.html @@ -0,0 +1,90 @@ + + + + +lv.id.jc.algorithm + + + + + + + + + + + + + + + +
+ +
+
+
+

Module lv.id.jc.algorithm

+
+
+
module lv.id.jc.algorithm
+
+
    +
  • +
    + +

    Packages

    +
    +
    Exports
    +
    +
    Package
    +
    Description
    + +
    +
    This package contains graph pathfinding algorithms.
    +
    +
    +
    +
    +
  • +
+
+
+
+
+ + diff --git a/docs/coverage/ns-1/sources/source-2.html b/docs/coverage/ns-1/sources/source-2.html new file mode 100644 index 0000000..c0f582b --- /dev/null +++ b/docs/coverage/ns-1/sources/source-2.html @@ -0,0 +1,151 @@ + + + + + + + Coverage Report > DijkstrasAlgorithm + + + + + +
+ + +

Coverage Summary for Class: DijkstrasAlgorithm (lv.id.jc.algorithm.graph)

+ + + + + + + + + + + + + + + +
Class + Class, % + + Method, % + + Line, % +
DijkstrasAlgorithm + + 100% + + + (1/1) + + + + 100% + + + (3/3) + + + + 100% + + + (20/20) + +
+ +
+
+ + +
+
1 package lv.id.jc.algorithm.graph; +2  +3 import java.util.HashMap; +4 import java.util.LinkedList; +5 import java.util.List; +6 import java.util.Objects; +7  +8 import static java.util.stream.Stream.iterate; +9  +10 /** +11  * Algorithm for finding the fastest paths between nodes in a graph. +12  * <p> +13  * The algorithm uses information about edge's distance to find the fastest path. +14  * +15  * @param <T> type of vertex id +16  */ +17 public class DijkstrasAlgorithm<T> implements SearchAlgorithm<T> { +18  +19  @Override +20  public List<T> findPath(Graph<T> graph, T source, T target) { +21  final var queue = new LinkedList<T>(); +22  final var distances = new HashMap<T, Double>(); +23  final var previous = new HashMap<T, T>(); +24  queue.add(source); +25  distances.put(source, .0); +26  +27  while (!queue.isEmpty()) { +28  final var prev = queue.removeFirst(); +29  graph.edges(prev).forEach((node, time) -> { +30  final var distance = distances.get(prev) + time.doubleValue(); +31  if (distance < distances.getOrDefault(node, Double.MAX_VALUE)) { +32  previous.put(node, prev); +33  distances.put(node, distance); +34  queue.addLast(node); +35  } +36  }); +37  } +38  if (previous.containsKey(target) || source.equals(target)) { +39  final var path = new LinkedList<T>(); +40  iterate(target, Objects::nonNull, previous::get).forEach(path::addFirst); +41  return path; +42  } +43  return List.of(); +44  } +45  +46 } +
+
+
+ + + + + + diff --git a/docs/coverage/ns-1/sources/source-3.html b/docs/coverage/ns-1/sources/source-3.html new file mode 100644 index 0000000..56deda3 --- /dev/null +++ b/docs/coverage/ns-1/sources/source-3.html @@ -0,0 +1,153 @@ + + + + + + + Coverage Report > Graph + + + + + +
+ + +

Coverage Summary for Class: Graph (lv.id.jc.algorithm.graph)

+ + + + + + + + + + + + + + + + + + + + + +
Class + Method, % + + Line, % +
Graph + + 100% + + + (3/3) + + + + 100% + + + (7/7) + +
Graph$getDistance
Total + + 100% + + + (3/3) + + + + 100% + + + (7/7) + +
+ +
+
+ + +
+
1 package lv.id.jc.algorithm.graph; +2  +3 import java.util.List; +4 import java.util.Map; +5 import java.util.stream.IntStream; +6  +7 /** +8  * A generic graph representation +9  * +10  * @param <T> type of vertex id +11  */ +12 public record Graph<T>(Map<T, Map<T, Number>> nodes) { +13  +14  /** +15  * Edges of the given vertex +16  * +17  * @param id vertex (node) +18  * @return all connections for the given vertex id +19  */ +20  public Map<T, Number> edges(T id) { +21  return nodes().get(id); +22  } +23  +24  /** +25  * Returns the calculated distance for the given path +26  * +27  * @param path the list of vertex ids representing the path +28  * @return distance for the given path as double +29  * @throws NullPointerException if path is incorrect and contains more than one vertex +30  */ +31  public double getDistance(final List<T> path) { +32  return IntStream +33  .range(1, path.size()) +34  .mapToObj(i -> edges(path.get(i - 1)).get(path.get(i))) +35  .mapToDouble(Number::doubleValue) +36  .sum(); +37  } +38 } +
+
+
+ + + + + + diff --git a/docs/coverage/ns-1/sources/source-4.html b/docs/coverage/ns-1/sources/source-4.html new file mode 100644 index 0000000..fcce85a --- /dev/null +++ b/docs/coverage/ns-1/sources/source-4.html @@ -0,0 +1,93 @@ + + + + + + + Coverage Report > SearchAlgorithm + + + + + +
+ + +

Coverage Summary for Class: SearchAlgorithm (lv.id.jc.algorithm.graph)

+ + + + + + + + + + + + +
Class
SearchAlgorithm$findPath
Total
+ +
+
+ + +
+
1 package lv.id.jc.algorithm.graph; +2  +3 import java.util.List; +4  +5 /** +6  * A functional interface for graph search algorithm +7  * +8  * @param <T> type of vertex id +9  */ +10 @FunctionalInterface +11 public interface SearchAlgorithm<T> { +12  /** +13  * Find the path from the source node to the target +14  * +15  * @param graph The graph in which we search for the path +16  * @param source Search starting point identifier +17  * @param target Search finish point identifier +18  * @return Path found or empty list if path cannot be found +19  */ +20  List<T> findPath(Graph<T> graph, T source, T target); +21 } +
+
+
+ + + + + +