Fix critical base.Graph Denkfehler

This commit is contained in:
joachimschmidt557 2019-03-22 23:01:42 +01:00
parent efaa96ff68
commit 30b20dff8d
4 changed files with 34 additions and 8 deletions

View file

@ -113,7 +113,8 @@ public class Graph<T> {
public Edge<T> getEdge(Node<T> nodeA, Node<T> 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);
}

View file

@ -11,6 +11,7 @@ import java.util.*;
public abstract class GraphAlgorithm<T> {
final static boolean DEBUG = true;
final static boolean DEBUG_2 = false;
int n = 0;
@ -70,6 +71,9 @@ public abstract class GraphAlgorithm<T> {
*/
private AlgorithmNode<T> getSmallestNode() {
if (DEBUG_2)
System.out.println("Enter gSN");
if (availableNodes.isEmpty()) {
return null;
}
@ -85,16 +89,21 @@ public abstract class GraphAlgorithm<T> {
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<T> {
*/
public void run() {
if (DEBUG_2)
System.out.println("Enter run");
AlgorithmNode<T> v = getSmallestNode();
while (v != null) {
if (DEBUG_2)
System.out.println("numEdges="+graph.getEdges(v.node).size());
for (Edge<T> 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<T> n = algorithmNodes.get(e.getOtherNode(v.node));

View file

@ -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<Integer> 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();
}