Update code.

This commit is contained in:
Mikael Capelle
2018-02-20 11:27:12 +01:00
parent a6e8a22081
commit 71accfe13b
18 changed files with 436 additions and 266 deletions

View File

@@ -0,0 +1,30 @@
package org.insa.algo.strongconnectivity;
import java.util.ArrayList;
import org.insa.graph.Node;
public interface StronglyConnectedComponentObserver {
/**
* Notify that the algorithm is entering a new component.
*
* @param curNode Starting node for the component.
*/
public void notifyStartComponent(Node curNode);
/**
* Notify that a new node has been found for the current component.
*
* @param node New node found for the current component.
*/
public void notifyNewNodeInComponent(Node node);
/**
* Notify that the algorithm has computed a new component.
*
* @param nodes List of nodes in the component.
*/
public void notifyEndComponent(ArrayList<Node> nodes);
}

View File

@@ -2,7 +2,7 @@ package org.insa.algo.strongconnectivity ;
import org.insa.algo.AbstractAlgorithm;
public abstract class StronglyConnectedComponentsAlgorithm extends AbstractAlgorithm {
public abstract class StronglyConnectedComponentsAlgorithm extends AbstractAlgorithm<StronglyConnectedComponentObserver> {
/**
*
@@ -12,5 +12,15 @@ public abstract class StronglyConnectedComponentsAlgorithm extends AbstractAlgor
public StronglyConnectedComponentsAlgorithm(StronglyConnectedComponentsInstance instance) {
super(instance);
}
@Override
public StronglyConnectedComponentsSolution run() {
return (StronglyConnectedComponentsSolution)super.run();
}
@Override
public StronglyConnectedComponentsInstance getInstance() {
return (StronglyConnectedComponentsInstance)super.getInstance();
}
}

View File

@@ -1,6 +1,5 @@
package org.insa.algo.strongconnectivity;
import java.time.Duration;
import java.util.ArrayList;
import org.insa.algo.AbstractSolution;
@@ -16,8 +15,8 @@ public class StronglyConnectedComponentsSolution extends AbstractSolution {
}
protected StronglyConnectedComponentsSolution(StronglyConnectedComponentsInstance instance,
Duration solvingTime, Status status, ArrayList<ArrayList<Node>> components) {
super(instance, solvingTime, status);
Status status, ArrayList<ArrayList<Node>> components) {
super(instance, status);
this.components = components;
}

View File

@@ -1,12 +1,9 @@
package org.insa.algo.strongconnectivity;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import org.insa.algo.AbstractSolution;
import org.insa.algo.AbstractSolution.Status;
import org.insa.graph.Arc;
import org.insa.graph.Graph;
@@ -74,7 +71,6 @@ public class TarjanAlgorithm extends StronglyConnectedComponentsAlgorithm {
* @return The strong component containing the given node.
*/
protected void findAndAddStrongComponent(Node v) {
Graph graph = getInstance().getGraph();
// Update node info, index and push the node.
indexes[v.getId()] = index;
@@ -117,14 +113,11 @@ public class TarjanAlgorithm extends StronglyConnectedComponentsAlgorithm {
}
@Override
protected AbstractSolution doRun() {
protected StronglyConnectedComponentsSolution doRun() {
Graph graph = getInstance().getGraph();
components = new ArrayList<ArrayList<Node>>();
// Starting time...
Instant start = Instant.now();
// Initialize everything
final int nbNodes = graph.getNodes().size();
stack = new Stack<Node>();
@@ -144,12 +137,8 @@ public class TarjanAlgorithm extends StronglyConnectedComponentsAlgorithm {
findAndAddStrongComponent(node);
}
}
// Duration...
Duration solvingTime = Duration.between(start, Instant.now());
return new StronglyConnectedComponentsSolution((StronglyConnectedComponentsInstance)getInstance(),
solvingTime, Status.OPTIMAL, components);
return new StronglyConnectedComponentsSolution(getInstance(), Status.OPTIMAL, components);
}
}