Fix GraphAlgorithm (hopefully)

This commit is contained in:
joachimschmidt557 2019-03-25 19:07:35 +01:00
parent d071187c79
commit c331208620
3 changed files with 98 additions and 17 deletions

View file

@ -70,7 +70,7 @@ public abstract class GraphAlgorithm<T> {
* @return Der nächste abzuarbeitende Knoten oder null
*/
private AlgorithmNode<T> getSmallestNode() {
if (DEBUG_2)
System.out.println("Enter gSN");
@ -79,13 +79,15 @@ public abstract class GraphAlgorithm<T> {
}
Iterator<Node<T>> iter = availableNodes.iterator();
AlgorithmNode<T> MinElem;
Iterator<Node<T>> minElemIter = availableNodes.iterator();
AlgorithmNode<T> minElem;
AlgorithmNode<T> tempElem;
if (iter.hasNext()) {
// Set minimum to first elements
MinElem = algorithmNodes.get(iter.next());
// Set minimum to first element
minElem = algorithmNodes.get(iter.next());
minElemIter.next();
while (iter.hasNext()) {
@ -99,15 +101,18 @@ public abstract class GraphAlgorithm<T> {
tempElem = algorithmNodes.get(iter.next());
if (tempElem.value > 0) {
if (tempElem.value < MinElem.value) {
MinElem = tempElem;
if (tempElem.value < minElem.value) {
// New minimum
minElem = tempElem;
minElemIter = iter;
}
}
}
iter.remove();
minElemIter.remove();
return MinElem;
return minElem;
}
@ -192,7 +197,7 @@ public abstract class GraphAlgorithm<T> {
if (prevNode == null)
return null;
while (!prevNode.node.equals(destination)) {
while (!(prevNode.value == 0)) {
AlgorithmNode<T> prevPrevNode = prevNode.previous;

View file

@ -15,7 +15,7 @@ public class GraphAlgoComplexityTest {
@Test
public void test() {
for (int i = 1; i < 1000; i+=10) {
for (int i = 30; i < 50; i+=1) {
Graph<Integer> g = generateGraph(i);
DumbGraphAlgorithm ga = new DumbGraphAlgorithm(g, g.getNodes().get(0));
@ -27,6 +27,19 @@ public class GraphAlgoComplexityTest {
double divN3 = (double)ga.getN() / ((double)i*i*i);
System.out.println(i + "," + ga.getN() + "," + divN + "," + divN2 + "," + divN3);
/////////////
Graph<Integer> g2 = generateGraph2(i);
DumbGraphAlgorithm ga2 = new DumbGraphAlgorithm(g2, g2.getNodes().get(4));
ga2.run();
List<Edge<Integer>> path = ga2.getPath(g2.getNodes().get(12));
for (Edge<Integer> edge : path) {
System.out.println(edge.getNodeA().getValue().toString() + " " + edge.getNodeB().getValue().toString());
}
//System.out.println();
}
@ -53,6 +66,23 @@ public class GraphAlgoComplexityTest {
}
public Graph<Integer> generateGraph2(int n) {
Graph<Integer> g = new Graph<Integer>();
List<Node<Integer>> nodes = new ArrayList<>();
for (int i = 0; i < n; i++)
nodes.add(g.addNode(i));
for (int i = 0; i < n - 1; i++) {
g.addEdge(nodes.get(i), nodes.get(i+1));
}
return g;
}
public class DumbGraphAlgorithm extends GraphAlgorithm<Integer> {
public DumbGraphAlgorithm(Graph<Integer> graph, Node<Integer> sourceNode) {