package etomica.graph.operations;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

import etomica.graph.model.Graph;
import etomica.graph.model.GraphFactory;
import etomica.graph.property.IsConnected;
import etomica.graph.traversal.CVisitor;

/**
 * Split a disconnected graph into multiple graphs.
 * 
 * @author Andrew Schultz
 */
public class SplitGraph {

  protected IsConnected isCon = new IsConnected();
    
  public Set<Graph> apply(Graph g) {
    Set<Graph> result = new HashSet<Graph>();
    if (isCon.check(g)) {
      result.add(g.copy());
      return result;
    }
    List<List<Byte>> components = CVisitor.getComponents(g);

    for (int iComp = 0; iComp<components.size(); iComp++) {
      List<Byte> nodes = components.get(iComp);
      Graph newG = GraphFactory.createGraph((byte)nodes.size());
      result.add(newG);
      for (byte id = 0; id<nodes.size(); id++) {
        byte nodeID = nodes.get(id);
        newG.getNode(id).setColor(g.getNode(nodeID).getColor());
        newG.getNode(id).setType(g.getNode(nodeID).getType());
        for (byte id2 = (byte)(id+1); id2<nodes.size(); id2++) {
          byte nodeID2 = nodes.get(id2);
          if (g.hasEdge(nodeID, nodeID2)) {
            newG.putEdge(id, id2);
            newG.getEdge(id, id2).setColor(g.getEdge(nodeID, nodeID2).getColor());
          }
        }
      }
      if (iComp == 0) {
        newG.coefficient().multiply(g.coefficient());
      }
    } 
    return result;
  }
}

@@ -33,34 +33,8 @@
 ;%0A%0A%0A
-import java.util.HashSet;%0A
 impo
@@ -150,16 +150,54 @@
 actory;%0A
+import etomica.graph.model.GraphList;%0A
 import e
@@ -515,14 +515,16 @@
 new 
-HashSe
+GraphLis
 t%3CGr
@@ -528,18 +528,58 @@
 %3CGraph%3E(
-);
+null); // we want to return these in order
 %0A    if 

return split graphs in order