From f936242b22751ef7c03df721775f1bd4872a2aaf Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 25 Jun 2023 10:59:36 +0200 Subject: [PATCH] Remove old Dijkstra implementation --- .../org/pgpainless/wot/dijkstra/Cost.java | 76 --------- .../org/pgpainless/wot/dijkstra/Dijkstra.java | 12 -- .../org/pgpainless/wot/dijkstra/Edge.java | 30 ---- .../org/pgpainless/wot/dijkstra/Graph.java | 26 --- .../org/pgpainless/wot/dijkstra/Node.java | 44 ----- .../org/pgpainless/wot/dijkstra/Path.java | 63 ------- .../wot/dijkstra/ShortestPathDijkstra.java | 114 ------------- .../pgpainless/wot/dijkstra/SimpleEdge.java | 45 ----- .../pgpainless/wot/dijkstra/TrustEdge.java | 23 --- .../pgpainless/wot/dijkstra/WotDijkstra.java | 17 -- .../BasicShortestPathDijkstraTest.java | 156 ------------------ .../org/pgpainless/wot/dijkstra/NodeTest.java | 35 ---- 12 files changed, 641 deletions(-) delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Cost.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Dijkstra.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Edge.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Graph.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Node.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Path.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/ShortestPathDijkstra.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/SimpleEdge.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/TrustEdge.java delete mode 100644 wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/WotDijkstra.java delete mode 100644 wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/BasicShortestPathDijkstraTest.java delete mode 100644 wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/NodeTest.java diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Cost.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Cost.java deleted file mode 100644 index 7497c433..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Cost.java +++ /dev/null @@ -1,76 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -public class Cost { - - public static class SimpleCost extends Cost { - - private final double weight; - - public SimpleCost(double weight) { - this.weight = weight; - } - - public double getWeight() { - return weight; - } - - @Override - public String toString() { - return Double.toString(getWeight()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (!(obj instanceof SimpleCost)) { - return false; - } - SimpleCost other = (SimpleCost) obj; - return getWeight() == other.getWeight(); - } - - @Override - public int hashCode() { - return (int) getWeight(); - } - } - - public static class TrustCost extends Cost { - - private final int depth; - private final int amount; - private final String regex; - - public TrustCost(int depth, int amount, String regex) { - this.depth = depth; - this.amount = amount; - this.regex = regex; - } - - public int getDepth() { - return depth; - } - - public int getAmount() { - return amount; - } - - public String getRegex() { - return regex; - } - - @Override - public String toString() { - return "d=" + getDepth() + ",a=" + getAmount() + (regex == null ? "" : ",r=" + getRegex()); - } - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Dijkstra.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Dijkstra.java deleted file mode 100644 index e23fc57a..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Dijkstra.java +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import javax.annotation.Nullable; - -public abstract class Dijkstra, C extends Cost> { - @Nullable - public abstract Path, C, E> findPath(Node to); -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Edge.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Edge.java deleted file mode 100644 index ea373a68..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Edge.java +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -public abstract class Edge implements Comparable { - - protected final Node from; - protected final Node to; - protected final C cost; - - public Edge(Node from, Node to, C cost) { - this.from = from; - this.to = to; - this.cost = cost; - } - - public Node getFrom() { - return from; - } - - public Node getTo() { - return to; - } - - public C getCost() { - return cost; - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Graph.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Graph.java deleted file mode 100644 index f4bc8977..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Graph.java +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import java.util.Collection; - -public class Graph, E extends Edge, C extends Cost> { - - private final Collection nodes; - private final Collection edges; - - public Graph(Collection nodes, Collection edges) { - this.nodes = nodes; - this.edges = edges; - } - - public Collection getNodes() { - return nodes; - } - - public Collection getEdges() { - return edges; - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Node.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Node.java deleted file mode 100644 index 9bdd2b43..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Node.java +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -public class Node { - - private final T item; - - public Node(T item) { - this.item = item; - } - - private T getItem() { - return item; - } - - @Override - public String toString() { - return "(" + getItem().toString() + ")"; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (!(obj instanceof Node)) { - return false; - } - - Node other = (Node) obj; - return getItem().equals(other.getItem()); - } - - @Override - public int hashCode() { - return getItem().hashCode(); - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Path.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Path.java deleted file mode 100644 index 38887589..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/Path.java +++ /dev/null @@ -1,63 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import java.util.Arrays; -import java.util.List; - -public class Path, C extends Cost, E extends Edge> { - - private final N from; - private final N to; - - private final List edges; - - public Path(N from, N to, List edges) { - this.from = from; - this.to = to; - this.edges = edges; - } - - public Node getFrom() { - return from; - } - - public Node getTo() { - return to; - } - - public List getEdges() { - return edges; - } - - @Override - public String toString() { - return Arrays.toString(getEdges().toArray()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (!(obj instanceof Path)) { - return false; - } - Path other = (Path) obj; - return getFrom().equals(other.getFrom()) - && getTo().equals(other.getTo()) - && getEdges().equals(other.getEdges()); - } - - @Override - public int hashCode() { - return getFrom().hashCode() - + 13 * getTo().hashCode() - + 31 * getEdges().hashCode(); - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/ShortestPathDijkstra.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/ShortestPathDijkstra.java deleted file mode 100644 index 26016acb..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/ShortestPathDijkstra.java +++ /dev/null @@ -1,114 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class ShortestPathDijkstra extends Dijkstra, Cost.SimpleCost> { - - private final Node root; - private final Graph, SimpleEdge, Cost.SimpleCost> graph; - private final List> queue = new ArrayList<>(); - - private final Map, Double> distances = new HashMap<>(); - private final Map, SimpleEdge> precursors = new HashMap<>(); - - public ShortestPathDijkstra(Graph, SimpleEdge, Cost.SimpleCost> graph, Node root) { - // INITIALIZE - this.graph = graph; - this.root = root; - for (Node node : graph.getNodes()) { - // dist[v] := infinity - distances.put(node, Double.MAX_VALUE); - - // precursor[v] := null - precursors.put(node, null); - } - // dist[root] := 0 - distances.put(root, 0d); - - // Q := set of all nodes in graph - queue.addAll(graph.getNodes()); - - while (!queue.isEmpty()) { - Node closest = closest(); - queue.remove(closest); - - for (SimpleEdge edge : graph.getEdges()) { - if (!closest.equals(edge.getFrom())) { - // Skip non-neighbors - continue; - } - - if (queue.contains(edge.getTo())) { - distUpdate(closest, edge.getTo()); - } - } - } - } - - private Node closest() { - Double minDist = Double.MAX_VALUE; - int index = 0; - Double dist; - for (int i = 0; i < queue.size(); i++) { - if ((dist = distances.get(queue.get(i))) <= minDist) { - index = i; - minDist = dist; - } - } - return queue.get(index); - } - - private void distUpdate(Node from, Node to) { - SimpleEdge edge = getEdgeBetween(from, to); - if (edge == null) { - // No direct path - return; - } - - Double distance = distances.get(from) + edge.getCost().getWeight(); - if (distance < distances.get(to)) { - distances.put(to, distance); - precursors.put(to, edge); - } - } - - private SimpleEdge getEdgeBetween(Node from, Node to) { - for (SimpleEdge edge : graph.getEdges()) { - if (!from.equals(edge.getFrom())) { - continue; - } - - if (to.equals(edge.getTo())) { - return edge; - } - } - return null; - } - - @Override - @Nullable - public Path, Cost.SimpleCost, SimpleEdge> findPath(Node to) { - List> pathEdges = new ArrayList<>(); - Node waypoint = to; - - SimpleEdge edge; - while ((edge = precursors.get(waypoint)) != null) { - waypoint = precursors.get(waypoint).getFrom(); - pathEdges.add(0, edge); - } - - if (pathEdges.isEmpty()) { - return null; - } - - return new Path<>(root, to, pathEdges); - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/SimpleEdge.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/SimpleEdge.java deleted file mode 100644 index 76562c08..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/SimpleEdge.java +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -public class SimpleEdge extends Edge { - - public SimpleEdge(Node from, Node to, Double edgeWeight) { - super(from, to, new Cost.SimpleCost(edgeWeight)); - } - - @Override - public String toString() { - return getFrom().toString() + " " + getCost() + "> " + getTo().toString(); - } - - @Override - public int compareTo(Cost.SimpleCost o) { - return Double.compare(getCost().getWeight(), o.getWeight()); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (!(obj instanceof SimpleEdge)) { - return false; - } - SimpleEdge other = (SimpleEdge) obj; - - return getFrom().equals(other.getFrom()) - && getTo().equals(other.getTo()) - && getCost().equals(other.getCost()); - } - - @Override - public int hashCode() { - return getFrom().hashCode() + 13 * getTo().hashCode() + 17 * getCost().hashCode(); - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/TrustEdge.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/TrustEdge.java deleted file mode 100644 index bc134e41..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/TrustEdge.java +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import javax.annotation.Nonnull; - -public class TrustEdge extends Edge { - - public TrustEdge(Node from, Node to, Cost.TrustCost cost) { - super(from, to, cost); - } - - @Override - public int compareTo(@Nonnull Cost.TrustCost o) { - int depthCompare = Double.compare(cost.getDepth(), o.getDepth()); - if (depthCompare != 0) { - return -depthCompare; - } - return Double.compare(cost.getAmount(), o.getAmount()); - } -} diff --git a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/WotDijkstra.java b/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/WotDijkstra.java deleted file mode 100644 index 359138f4..00000000 --- a/wot-dijkstra/src/main/java/org/pgpainless/wot/dijkstra/WotDijkstra.java +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import javax.annotation.Nullable; - -public class WotDijkstra extends Dijkstra, Cost.TrustCost> { - - @Override - @Nullable - public Path, Cost.TrustCost, TrustEdge> findPath(Node to) { - return null; - } -} - diff --git a/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/BasicShortestPathDijkstraTest.java b/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/BasicShortestPathDijkstraTest.java deleted file mode 100644 index 0aa71363..00000000 --- a/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/BasicShortestPathDijkstraTest.java +++ /dev/null @@ -1,156 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.junit.jupiter.api.Test; - -public class BasicShortestPathDijkstraTest { - - /** - * Generate a test graph from a string definition. - * The definition might look like this: - *
-     *     Alice
-     *     Bob -1> Charlie
-     *     Charlie -4> Dieter -1> Alice
-     *     Dieter -2> Charlie
-     * 
- * @param definition definition - * @return graph - */ - private Graph, SimpleEdge, Cost.SimpleCost> generate(String definition) { - Set> nodes = new HashSet<>(); - Set> edges = new HashSet<>(); - String[] lines = definition.split("\n"); - for (String line : lines) { - line = line.trim(); - if (line.isEmpty()) { - continue; - } - - String[] fromTo = line.split(" -\\d+> "); - if (fromTo.length == 1) { - // Unconnected node - nodes.add(new Node<>(fromTo[0])); - continue; - } - - int searchOffset = 0; - for (int i = 0; i < fromTo.length - 1; i++) { - Node from = new Node<>(fromTo[i]); - nodes.add(from); - searchOffset += fromTo[i].length() + " -".length(); - int costStop = line.indexOf("> ", searchOffset); - String costString = line.substring(searchOffset, costStop); - Double cost = Double.parseDouble(costString); - searchOffset += costString.length() + "> ".length(); - Node to = new Node<>(fromTo[i + 1]); - nodes.add(to); - - edges.add(new SimpleEdge<>(from, to, cost)); - } - } - - return new Graph<>(nodes, edges); - } - - private Path, Cost.SimpleCost, SimpleEdge> path(String definition) { - definition = definition.trim(); - String[] fromTo = definition.split(" -\\d+> "); - if (fromTo.length == 1) { - // Unconnected node - Node node = new Node<>(fromTo[0]); - return new Path<>(node, node, Collections.singletonList(new SimpleEdge<>(node, node, 0d))); - } - - Node start = null; - Node end = null; - List> edges = new ArrayList<>(); - int searchOffset = 0; - for (int i = 0; i < fromTo.length - 1; i++) { - Node from = new Node<>(fromTo[i]); - if (start == null) { - start = from; - } - searchOffset += fromTo[i].length() + " -".length(); - int costStop = definition.indexOf("> ", searchOffset); - String costString = definition.substring(searchOffset, costStop); - Double cost = Double.parseDouble(costString); - searchOffset += costString.length() + "> ".length(); - Node to = new Node<>(fromTo[i + 1]); - edges.add(new SimpleEdge<>(from, to, cost)); - end = to; - } - - Path, Cost.SimpleCost, SimpleEdge> path = new Path<>(start, end, edges); - return path; - } - - @Test - public void exampleGraphTest() { - Graph, SimpleEdge, Cost.SimpleCost> g = generate( - "Alice\n" + - "Bob -1> Charlie\n" + - "Bob -3> Dieter -1> Marlene\n" + - "Dieter -1> Alice\n" + - "Mallory\n"); - - Set> expectedNodes = new HashSet<>(); - expectedNodes.add(new Node<>("Alice")); - expectedNodes.add(new Node<>("Bob")); - expectedNodes.add(new Node<>("Charlie")); - expectedNodes.add(new Node<>("Dieter")); - expectedNodes.add(new Node<>("Marlene")); - expectedNodes.add(new Node<>("Mallory")); - - assertEquals(expectedNodes, g.getNodes()); - - Set> expectedEdges = new HashSet<>(); - expectedEdges.add(new SimpleEdge<>(new Node<>("Bob"), new Node<>("Charlie"), 1d)); - expectedEdges.add(new SimpleEdge<>(new Node<>("Bob"), new Node<>("Dieter"), 3d)); - expectedEdges.add(new SimpleEdge<>(new Node<>("Dieter"), new Node<>("Marlene"), 1d)); - expectedEdges.add(new SimpleEdge<>(new Node<>("Dieter"), new Node<>("Alice"), 1d)); - - assertEquals(g.getEdges(), expectedEdges); - } - - @Test - public void emptyNetworkTest() { - Graph, SimpleEdge, Cost.SimpleCost> graph = generate(""); - - Node root = new Node<>("root"); - Node target = new Node<>("target"); - ShortestPathDijkstra dijkstra = new ShortestPathDijkstra<>(graph, root); - Path, Cost.SimpleCost, SimpleEdge> path = dijkstra.findPath(target); - - assertNull(path); - } - - @Test - public void pathFindingTest() { - Graph, SimpleEdge, Cost.SimpleCost> graph = generate( - "Pablo\n" + - "Root -2> Alice -3> Alexandra\n" + - "Root -1> Karlos -1> Alexandra\n" + - "Karlos -2> Malte -4> Sven"); - - ShortestPathDijkstra dijkstra = new ShortestPathDijkstra<>(graph, new Node<>("Root")); - assertEquals(path("Root -1> Karlos -2> Malte -4> Sven"), dijkstra.findPath(new Node<>("Sven"))); - assertEquals(path("Root -1> Karlos"), dijkstra.findPath(new Node<>("Karlos"))); - assertEquals(path("Root -1> Karlos -1> Alexandra"), dijkstra.findPath(new Node<>("Alexandra"))); - - dijkstra = new ShortestPathDijkstra<>(graph, new Node<>("Karlos")); - assertEquals(path("Karlos -2> Malte -4> Sven"), dijkstra.findPath(new Node<>("Sven"))); - } -} diff --git a/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/NodeTest.java b/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/NodeTest.java deleted file mode 100644 index 417a15e4..00000000 --- a/wot-dijkstra/src/test/java/org/pgpainless/wot/dijkstra/NodeTest.java +++ /dev/null @@ -1,35 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Paul Schaub -// -// SPDX-License-Identifier: Apache-2.0 - -package org.pgpainless.wot.dijkstra; - -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; - -public class NodeTest { - - @Test - public void equalsTest() { - Node n1 = new Node<>("foo"); - Node n1_ = new Node<>("foo"); - Node n2 = new Node<>("bar"); - - assertEquals(n1, n1_); - assertEquals(n1, n1); - assertNotEquals(n1, n2); - - Map, String> map = new HashMap<>(); - map.put(n1, "foo"); - map.put(n2, "bar"); - - assertEquals("foo", map.get(n1)); - assertEquals("bar", map.get(n2)); - assertEquals("foo", map.get(n1_)); - } -}