docs: protected N buildSpanningTree(int dim, double[] mat, Layout layout) {
      assert (layout.edges == null || layout.edges.size() == 0);
      int[] iedges = PrimsMinimumSpanningTree.processDense(mat, new LowerTriangularAdapter(dim));
      int root = findOptimalRoot(iedges);
  
      // Convert edges:
      ArrayList<Edge> edges = new ArrayList<>(iedges.length >> 1);
      for(int i = 1; i < iedges.length; i += 2) {
        edges.add(new Edge(iedges[i - 1], iedges[i]));
      }
      layout.edges = edges;
  
      // Prefill nodes array with nulls.
      ArrayList<N> nodes = new ArrayList<>(dim);
      for(int i = 0; i < dim; i++) {
        nodes.add(null);
      }
      layout.nodes = nodes;
      N rootnode = buildTree(iedges, root, -1, nodes);
  
      return rootnode;
    }