From 757270d9334b1f9643da1ed50ae2c4db2188bad9 Mon Sep 17 00:00:00 2001 From: Juhan Oskar Hennoste Date: Mon, 23 Oct 2023 22:07:01 +0300 Subject: [PATCH] Add separate API-s for drawing directed and undirected edges --- .../java/ee/ut/dendroloj/Dendrologist.java | 16 ++++- .../ee/ut/dendroloj/GenericGraphLayout.java | 4 +- .../java/ee/ut/dendroloj/GraphCanvas.java | 64 ++++++++++++++----- src/test/java/GraafKatsed.java | 2 +- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/main/java/ee/ut/dendroloj/Dendrologist.java b/src/main/java/ee/ut/dendroloj/Dendrologist.java index 7d9a43c..33ae2b3 100644 --- a/src/main/java/ee/ut/dendroloj/Dendrologist.java +++ b/src/main/java/ee/ut/dendroloj/Dendrologist.java @@ -5,6 +5,7 @@ import java.awt.Color; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.function.Function; public final class Dendrologist { @@ -224,11 +225,20 @@ private static void drawAdjacencyMatrixGraph(int vertexCount, WeightProvider wei GraphCanvas graphCanvas = new GraphCanvas<>(); for (int i = 0; i < vertexCount; i++) { graphCanvas.drawVertex(i, labels == null ? String.valueOf(i) : labels[i]); - for (int j = 0; j < vertexCount; j++) { + Number selfWeight = weights.getWeight(i, i); + if (selfWeight != null && selfWeight.doubleValue() != 0.0) { + graphCanvas.drawEdge(i, i, selfWeight.toString()); + } + for (int j = i + 1; j < vertexCount; j++) { Number weight = weights.getWeight(i, j); - if (weight != null && (i != j || weight.doubleValue() != 0.0)) { - graphCanvas.drawEdge(i, j, weight.toString()); + Number backwardsWeight = weights.getWeight(j, i); + if (Objects.equals(weight, backwardsWeight)) { + if (weight != null) graphCanvas.drawEdge(i, j, weight.toString()); + } else { + if (weight != null) graphCanvas.drawDirectedEdge(i, j, weight.toString()); + if (backwardsWeight != null) graphCanvas.drawDirectedEdge(j, i, backwardsWeight.toString()); } + } } Graph graph = GenericGraphLayout.assembleGraph(graphCanvas); diff --git a/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java b/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java index 6b68673..a489396 100644 --- a/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java +++ b/src/main/java/ee/ut/dendroloj/GenericGraphLayout.java @@ -7,8 +7,6 @@ import org.graphstream.ui.layout.Layout; import org.graphstream.ui.layout.springbox.implementations.SpringBox; -import java.util.concurrent.atomic.AtomicLong; - class GenericGraphLayout { public static Layout autoLayout() { @@ -23,7 +21,7 @@ public static Graph assembleGraph(GraphCanvas graphCanvas) { if (vertex.color != null) node.setAttribute("ui.color", vertex.color); } for (var edge : graphCanvas.edges) { - Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), IdHelper.getNodeId(edge.from), IdHelper.getNodeId(edge.to), true); + Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), IdHelper.getNodeId(edge.v1), IdHelper.getNodeId(edge.v2), edge.directed); if (edge.label != null) graphEdge.setAttribute("label", edge.label); if (edge.color != null) graphEdge.setAttribute("ui.color", edge.color); } diff --git a/src/main/java/ee/ut/dendroloj/GraphCanvas.java b/src/main/java/ee/ut/dendroloj/GraphCanvas.java index c693b36..10f668e 100644 --- a/src/main/java/ee/ut/dendroloj/GraphCanvas.java +++ b/src/main/java/ee/ut/dendroloj/GraphCanvas.java @@ -19,12 +19,11 @@ public final class GraphCanvas { public void drawVertex(T vertex) { if (vertex == null) throw new NullPointerException("Vertex must not be null"); - vertices.add(new Vertex<>(vertex, vertex.toString(), null)); + drawVertex(vertex, vertex.toString(), null); } public void drawVertex(T vertex, String label) { - if (vertex == null) throw new NullPointerException("Vertex must not be null"); - vertices.add(new Vertex<>(vertex, label, null)); + drawVertex(vertex, label, null); } public void drawVertex(T vertex, String label, Color color) { @@ -32,19 +31,48 @@ public void drawVertex(T vertex, String label, Color color) { vertices.add(new Vertex<>(vertex, label, color)); } - public void drawEdge(T from, T to) { - if (from == null || to == null) throw new NullPointerException("Target vertices must not be null"); - edges.add(new Edge<>(from, to, null, null)); + /** + * Draws an undirected edge from vertex v1 to vertex v2. + */ + public void drawEdge(T v1, T v2) { + drawEdge(v1, v2, null, null); + } + + /** + * Draws an undirected edge from vertex v1 to vertex v2. + */ + public void drawEdge(T v1, T v2, String label) { + drawEdge(v1, v2, label, null); + } + + /** + * Draws an undirected edge from vertex v1 to vertex v2. + */ + public void drawEdge(T v1, T v2, String label, Color color) { + if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null"); + edges.add(new Edge<>(false, v1, v2, label, color)); + } + + /** + * Draws a directed edge from vertex v1 to vertex v2. + */ + public void drawDirectedEdge(T v1, T v2) { + drawDirectedEdge(v1, v2, null, null); } - public void drawEdge(T from, T to, String label) { - if (from == null || to == null) throw new NullPointerException("Target vertices must not be null"); - edges.add(new Edge<>(from, to, label, null)); + /** + * Draws a directed edge from vertex v1 to vertex v2. + */ + public void drawDirectedEdge(T v1, T v2, String label) { + drawDirectedEdge(v1, v2, label, null); } - public void drawEdge(T from, T to, String label, Color color) { - if (from == null || to == null) throw new NullPointerException("Target vertices must not be null"); - edges.add(new Edge<>(from, to, label, color)); + /** + * Draws a directed edge from vertex v1 to vertex v2. + */ + public void drawDirectedEdge(T v1, T v2, String label, Color color) { + if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null"); + edges.add(new Edge<>(true, v1, v2, label, color)); } static final class Vertex { @@ -60,14 +88,16 @@ private Vertex(T vertex, String label, Color color) { } static final class Edge { - public final T from; - public final T to; + public final boolean directed; + public final T v1; + public final T v2; public final String label; public final Color color; - private Edge(T from, T to, String label, Color color) { - this.from = from; - this.to = to; + private Edge(boolean directed, T v1, T v2, String label, Color color) { + this.directed = directed; + this.v1 = v1; + this.v2 = v2; this.label = label; this.color = color; } diff --git a/src/test/java/GraafKatsed.java b/src/test/java/GraafKatsed.java index 64197f8..a8a6ca8 100644 --- a/src/test/java/GraafKatsed.java +++ b/src/test/java/GraafKatsed.java @@ -20,7 +20,7 @@ public static void main(String[] args) { {4, 0, 2, -1, 6}, {-1, 2, 0, 3, -1}, {12, -1, -1, 0, -1}, - {-1, -1, 5, 4, 0} + {-1, -1, 5, 4, 123} }; Dendrologist.drawGraph(M, nimed); // Dendrologist.drawGraph(M, null);