private static long loadUnweightedGraph()

in src/main/java/com/twitter/sbf/graph/Graph.java [283:342]


  private static long loadUnweightedGraph(
      SimpleIterator<String> lines,
      int[][] nbrs,
      long expectedNumEdges) {
    Optional<String> lineOpt;
    int nodeId = 0;
    long edgesSoFar = 0;
    assert nbrs != null;
    while (true) {
      lineOpt = lines.next();
      if (!lineOpt.isPresent()) {
        break;
      } else {
        String line = lineOpt.get();
        if (line.startsWith("%")) {
          continue;
        }
        line = line.trim();
        // Fields are separated by spaces
        String[] tokens = line.split("\\s+");
        if (line.isEmpty()) { // vertices with no neighbor
          nbrs[nodeId++] = new int[0];
        } else { // The rest are adjacency lists
          int numNeighbors = tokens.length;
          edgesSoFar += numNeighbors;
          int[] myNeighbors = new int[numNeighbors];
          for (int i = 0; i < numNeighbors; i++) {
            int nId = Integer.parseInt(tokens[i]) - 1; // 0-based indexing
            if (nId < 0) {
              throw new IllegalStateException(
                  String.format(
                      "Line %d: neighbor %d is less than 1 (all vertex ids are positive)\n",
                      nodeId + 1, nId + 1
                  )
              );
            }
            myNeighbors[i] = nId;
          }
          Arrays.sort(myNeighbors);
          nbrs[nodeId++] = myNeighbors;
        }
        if (nodeId > nbrs.length) {
          throw new RuntimeException("Expected number of nodes " + nbrs.length
              + " is inconsistent with number of adjacency lists");
        }
        if (nodeId % 100000 == 0) {
          System.err.print("\rDone loading " + nodeId + " lines.");
        }
      }
    }
    if (edgesSoFar % 2 != 0) {
      throw new RuntimeException("Got odd number of edges (counting edges from both directions) - "
          + edgesSoFar + ". Graph is not valid undirected graph.");
    }
    if (edgesSoFar != expectedNumEdges * 2) {
      System.err.println("Expected " + 2 * expectedNumEdges
          + " (counting both directions), but got " + edgesSoFar + " many edges.");
    }
    return edgesSoFar / 2;
  }