From 75b2a4d19270f7c096d06da60965fd03f2f0b6e3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Mar 2026 17:17:10 +0000 Subject: [PATCH] Fix Dijkstra predecessor bug (#53, #70), complement edge removal (#48), banana tree positioning (#15) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dijkstra: Fix prev.set(edge.source.getId(), edge.target) → prev.set(target.getId(), vMin) to correctly track predecessors in shortest path tree - Complement: Skip self-loops, remove undirected edges in both directions since removeAllEdges only removes source→target direction - BananaTree: Fix skip condition (i+k/2)%n → (i*k/n+k/2)%k to correctly calculate which leaf position points toward root Co-authored-by: rostam <1497363+rostam@users.noreply.github.com> Agent-Logs-Url: https://github.com/rostam/GraphTea/sessions/3b924362-279a-41f3-9237-c722508d2703 --- .../shortestpath/algs/Dijkstra.java | 2 +- .../generators/BananaTreeGenerator.java | 2 +- .../select/MakeSelectionComplementGraph.java | 14 +++++++++---- test/AlgorithmsTest.java | 20 +++++++++++-------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/graphtea/extensions/algorithms/shortestpath/algs/Dijkstra.java b/src/graphtea/extensions/algorithms/shortestpath/algs/Dijkstra.java index 73fa89a2..13a00ee7 100755 --- a/src/graphtea/extensions/algorithms/shortestpath/algs/Dijkstra.java +++ b/src/graphtea/extensions/algorithms/shortestpath/algs/Dijkstra.java @@ -97,7 +97,7 @@ public int compare(Vertex o1, Vertex o2) { target.setMark(true); target.setColor(5); Q.add(target); - prev.set(edge.source.getId(), edge.target); + prev.set(target.getId(), vMin); } } } diff --git a/src/graphtea/extensions/generators/BananaTreeGenerator.java b/src/graphtea/extensions/generators/BananaTreeGenerator.java index 2bb599a1..e6b44d23 100755 --- a/src/graphtea/extensions/generators/BananaTreeGenerator.java +++ b/src/graphtea/extensions/generators/BananaTreeGenerator.java @@ -76,7 +76,7 @@ private static GraphModel generateBananaTree(int n, int k) { GPoint[] sR = PositionGenerators.circle(1000, center.x, center.y, k); for (int j = 0; j < k; j++) { - if(j == (i + k/2)%n) continue; + if (j == (i * k / n + k / 2) % k) continue; curv = new Vertex(); g.insertVertex(curv); setloc(curv, sR[j]); diff --git a/src/graphtea/plugins/main/select/MakeSelectionComplementGraph.java b/src/graphtea/plugins/main/select/MakeSelectionComplementGraph.java index 69139970..ef1af84b 100755 --- a/src/graphtea/plugins/main/select/MakeSelectionComplementGraph.java +++ b/src/graphtea/plugins/main/select/MakeSelectionComplementGraph.java @@ -41,14 +41,20 @@ public void action(GraphData gd) { protected void doEdgeOperation(GraphModel g, HashSet v) { boolean directed = g.isDirected(); - + for (Vertex v1 : v) { for (Vertex v2 : v) { - if (!directed) - if (v1.getId() < v2.getId()) - continue; + if (v1 == v2) { + continue; + } + if (!directed && v1.getId() < v2.getId()) { + continue; + } if (g.isEdge(v1, v2)) { g.removeAllEdges(v1, v2); + if (!directed) { + g.removeAllEdges(v2, v1); + } } else { g.insertEdge(new Edge(v1, v2)); } diff --git a/test/AlgorithmsTest.java b/test/AlgorithmsTest.java index 8c4e206e..d03d83ae 100644 --- a/test/AlgorithmsTest.java +++ b/test/AlgorithmsTest.java @@ -25,7 +25,7 @@ public class AlgorithmsTest { // ------------------------------------------------------------------------- /** Build a simple directed weighted path 0→1(w=1)→2(w=2)→3(w=3) and verify - * that Dijkstra from vertex 0 encodes the correct successor chain. */ + * that Dijkstra from vertex 0 encodes the correct predecessor chain. */ @Test public void testDijkstraLinearPath() throws Exception { GraphModel g = new GraphModel(true); @@ -40,10 +40,11 @@ public void testDijkstraLinearPath() throws Exception { List prev = new Dijkstra().getShortestPath(g, v0); assertNotNull(prev); - // prev[i] is the successor of vertex i on the shortest path from v0 - assertEquals(v1, prev.get(v0.getId())); - assertEquals(v2, prev.get(v1.getId())); - assertEquals(v3, prev.get(v2.getId())); + // prev[i] is the predecessor of vertex i on the shortest path from v0 + assertNull(prev.get(v0.getId()), "Source vertex has no predecessor"); + assertEquals(v0, prev.get(v1.getId()), "Predecessor of v1 is v0"); + assertEquals(v1, prev.get(v2.getId()), "Predecessor of v2 is v1"); + assertEquals(v2, prev.get(v3.getId()), "Predecessor of v3 is v2"); } /** Dijkstra on a complete K4 graph should return a prev-array of size 4 @@ -71,9 +72,12 @@ public void testDijkstraDiamond() throws Exception { List prev = new Dijkstra().getShortestPath(g, v0); assertNotNull(prev); - // The path to v1 should go via v2, not directly - assertEquals(v2, prev.get(v0.getId()), - "Dijkstra should prefer short path 0→2 over long path 0→1"); + // prev[i] is the predecessor of vertex i on the shortest path from v0 + assertNull(prev.get(v0.getId()), "Source vertex has no predecessor"); + assertEquals(v0, prev.get(v2.getId()), + "Predecessor of v2 is v0 (direct path 0→2)"); + assertEquals(v2, prev.get(v1.getId()), + "Predecessor of v1 is v2 (shortest path 0→2→1)"); } // -------------------------------------------------------------------------