From 30b20dff8da63fd99f1eb37546b5c05b0d6eb643 Mon Sep 17 00:00:00 2001 From: joachimschmidt557 Date: Fri, 22 Mar 2019 23:01:42 +0100 Subject: [PATCH] Fix critical base.Graph Denkfehler --- Projektgruppe_175/src/base/Graph.java | 3 +- .../src/base/GraphAlgorithm.java | 29 ++++++++++++++++--- .../student/GraphAlgoComplexityTest.java | 3 +- doc/Dokumentation.tex | 7 +++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Projektgruppe_175/src/base/Graph.java b/Projektgruppe_175/src/base/Graph.java index fd7a87c..c833bb4 100644 --- a/Projektgruppe_175/src/base/Graph.java +++ b/Projektgruppe_175/src/base/Graph.java @@ -113,7 +113,8 @@ public class Graph { public Edge getEdge(Node nodeA, Node nodeB) { return edges.stream() - .filter(x -> x.getNodeA().equals(nodeA) && x.getNodeB().equals(nodeB)) + .filter(x -> x.getNodeA().equals(nodeA) && x.getNodeB().equals(nodeB) || + x.getNodeA().equals(nodeB) && x.getNodeB().equals(nodeA)) .findFirst() .orElse(null); } diff --git a/Projektgruppe_175/src/base/GraphAlgorithm.java b/Projektgruppe_175/src/base/GraphAlgorithm.java index 1e2f552..ea166b6 100644 --- a/Projektgruppe_175/src/base/GraphAlgorithm.java +++ b/Projektgruppe_175/src/base/GraphAlgorithm.java @@ -11,6 +11,7 @@ import java.util.*; public abstract class GraphAlgorithm { final static boolean DEBUG = true; + final static boolean DEBUG_2 = false; int n = 0; @@ -70,6 +71,9 @@ public abstract class GraphAlgorithm { */ private AlgorithmNode getSmallestNode() { + if (DEBUG_2) + System.out.println("Enter gSN"); + if (availableNodes.isEmpty()) { return null; } @@ -85,16 +89,21 @@ public abstract class GraphAlgorithm { while (iter.hasNext()) { + if (DEBUG) + n += 1; + + if (DEBUG_2) { + System.out.println("gSN with size " + availableNodes.size() + " and n=" + n); + System.out.flush(); + } + tempElem = algorithmNodes.get(iter.next()); if (tempElem.value > 0) { if (tempElem.value < MinElem.value) { MinElem = tempElem; } } - - if (DEBUG) - n += 0; - + } iter.remove(); @@ -124,14 +133,26 @@ public abstract class GraphAlgorithm { */ public void run() { + if (DEBUG_2) + System.out.println("Enter run"); + AlgorithmNode v = getSmallestNode(); while (v != null) { + + if (DEBUG_2) + System.out.println("numEdges="+graph.getEdges(v.node).size()); + for (Edge e : graph.getEdges(v.node)) { if (DEBUG) n += 1; + if (DEBUG_2) { + System.out.println("run with n="+n); + System.out.flush(); + } + if (isPassable(e)) { AlgorithmNode n = algorithmNodes.get(e.getOtherNode(v.node)); diff --git a/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java b/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java index 87144eb..b54a30c 100644 --- a/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java +++ b/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java @@ -15,7 +15,7 @@ public class GraphAlgoComplexityTest { @Test public void test() { - for (int i = 1; i < 100; i++) { + for (int i = 1; i < 1000; i+=10) { Graph g = generateGraph(i); DumbGraphAlgorithm ga = new DumbGraphAlgorithm(g, g.getNodes().get(0)); @@ -27,6 +27,7 @@ public class GraphAlgoComplexityTest { double divN3 = (double)ga.getN() / ((double)i*i*i); System.out.println(i + "," + ga.getN() + "," + divN + "," + divN2 + "," + divN3); + //System.out.println(); } diff --git a/doc/Dokumentation.tex b/doc/Dokumentation.tex index b7a9d65..2b14771 100644 --- a/doc/Dokumentation.tex +++ b/doc/Dokumentation.tex @@ -151,7 +151,7 @@ werden der ArrayDeque \texttt{nextVisitNodes} hinzugefügt. \end{algorithm} -$$O(n)$$ +$$O(n^2)$$ $n$ soll in diesem Fall die Anzahl an Knoten wiedergeben. @@ -166,7 +166,10 @@ eine Komplexität von $O(n)$ hat, wenn $n$ die Anzahl der Knoten darstellt. Bei jeder Iteration des Algorithmus wird zuerst der -Knoten mit dem kleinsten Wert gesucht. +Knoten mit dem kleinsten Wert gesucht. Dann werden alle +Nachbarn dieses Knotens abgeprüft. Das können höchstens +$n - 1$ Knoten sein, da ausgeschlossen wird, dass ein +Knoten eine Kante mit sich selber haben kann. \subsubsection{Teil (b)}