Calculate layers of NEAT

Hi,
I’m trying to do my own implementation of a NEAT using processing, to be able to show my neural network, I have to order the different nodes in layers. The problem is that when I add a new node in the network, the layer number gets wrong when I update it. Thats because i’d need to increase the layer of the whole network.
To prevent that, I had the idea to recalculate all the layers using the current configuration of the network, starting with the input nodes, which have a layer number of 0.
How can I calculate the layer of all the other nodes ? I tried to implement a breadth first search, but it doesnt work because I have multiple input nodes. Does somebody have a different version that is able to calculate the distance from all the input nodes ?

To store the network, I use two different objects, Node and Connexion, the network is composed of an array of each object.

Here is the prototype of the two objects :

class Node {
  int ID;
  int type; //0=input, 1=hidden, 2=output
  int layer; //The depth at which is the node
  float x,y;
  float bias, sum, output;
  
  Node(int ID, int type) {
    this.ID = ID;
    this.type = type;
    this.bias = random(-1,1);
  }
class Connexion {
  int input, output, innovation;
  float weight;
  boolean enabled;
  
  Connexion(int input, int output, int innovation, float weight, boolean enabled) {
    this.input = input; //ID of the input node
    this.output = output; //ID of the output node
    this.innovation = innovation; //Epoque of the connexion
    this.weight = weight; //Weight to multiply
    this.enabled = enabled; //Determines if the connexion is active
  }
 
class Network {
 Node[] nodes;
 Connexion[] connexions;
 int nbInputs, nbOutputs, totalNodes;
 int nbConnexions;
 float poidsMax = 5;

 Network(int nbInputs, int nbOutputs) {
   this.nbInputs = nbInputs;
   this.nbOutputs = nbOutputs;
   nbConnexions = 0;
   totalNodes = nbInputs + nbOutputs;
   nodes = new Node[totalNodes];
   connexions = new Connexion[1];

   for (int i=0; i<nbInputs; i++) { //Input nodes
     nodes[i] = new Node(i, 0);
     nodes[i].layer = 0;
   }
   for (int i=nbInputs; i<totalNodes; i++) { //Output nodes
     nodes[i] = new Node(i, 2);
     nodes[i].layer = 1;
   }
 }

The algorithm I currently use is this one :

void calculateLayers() {
    int n = 0, max = 0;
    boolean[] visited =  new boolean[nodes.length];
    int nbVisited = 0, ind = 0;
    boolean problem = false;
    ArrayList<Integer> members;
    ArrayList<Integer> membersChilds = new ArrayList<Integer>();
    for (int i=0; i<nodes.length; i++) {
      if (nodes[i].type==0) {
        nodes[i].layer=0;
        visited[i] = true;
        nbVisited++;
      } else {
        nodes[i].layer = -1;
      }
    }

    while (nbVisited < nodes.length && !problem) {
      //Search all members of level N
      members = getIDofLayer(n);
      membersChilds.clear();

      //Search the childs of level N
      for (Integer node : members) {
        membersChilds.addAll(getChilds(node));
      }

      if (membersChilds.isEmpty()) {
        problem = true;
      }

      //Delete duplicates
      Set<Integer> hs = new HashSet<Integer>();
      hs.addAll(membersChilds);
      membersChilds.clear();
      membersChilds.addAll(hs);


      //Set level N+1 to childrens
      for (Integer child : membersChilds) {
        if (nodes[getPosNode(child)].layer == -1) {
          nbVisited++;
        }
        ind = getPosNode(child);
        visited[ind] = true;
        nodes[getPosNode(child)].layer = n+1;
      }
      n++;
    }

    //Get max of the layer
    for (int i=0; i<nodes.length; i++) {
      if (nodes[i].layer>max) {
        max = nodes[i].layer;
      }
    }

    max++;
    //Set max layer to the output layer
    for (int i=0; i<nodes.length; i++) {
      if (nodes[i].type==2) {
        nodes[i].layer=max;
      }
    }
  }

But sometimes, linked nodes shows up at the same layer, what could cause it ?