package signature;

import java.util.Arrays;

public class CanonicalLabellingVisitor implements DAGVisitor {
    
    private int[] labelling;
    
    private int currentLabel;
    
    public CanonicalLabellingVisitor(int vertexCount) {
        this.labelling = new int[vertexCount];
        Arrays.fill(this.labelling, -1);
        this.currentLabel = 0;
    }

    public void visit(DAG.Node node) {
        // only label if this vertex has not yet been labeled
        if (this.labelling[node.vertexIndex] == -1) {
            this.labelling[node.vertexIndex] = this.currentLabel;
            this.currentLabel++;
        }
        for (DAG.Node child : node.children) {
            child.accept(this);
        }
    }
    
    public int[] getLabelling() {
        return this.labelling;
    }

}

package signature;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class CanonicalLabellingVisitor implements DAGVisitor {
    
    private int[] labelling;
    
    private int currentLabel;
    
    private Comparator<DAG.Node> comparator;
    
    
    public CanonicalLabellingVisitor(
            int vertexCount, Comparator<DAG.Node> comparator) {
        labelling = new int[vertexCount];
        Arrays.fill(labelling, -1);
        currentLabel = 0;
    }

    public void visit(DAG.Node node) {
        // only label if this vertex has not yet been labeled
        if (this.labelling[node.vertexIndex] == -1) {
            this.labelling[node.vertexIndex] = this.currentLabel;
            this.currentLabel++;
        }
        Collections.sort(node.children, comparator);
        for (DAG.Node child : node.children) {
            child.accept(this);
        }
    }
    
    public int[] getLabelling() {
        return this.labelling;
    }

}

Sort the child nodes in the labelling visitor, to make sure that they are sorted