private void areListsSortedAndSymmetric()

in src/main/java/com/twitter/sbf/graph/BipartiteGraph.java [396:444]


  private void areListsSortedAndSymmetric() throws IllegalArgumentException {
    //Make sure that the adjacency lists for each vertex are in strictly increasing order
    for (int i = 0; i < this.numLeftVertices; i++) {
      int[] curLeftList = this.neighborsForLeft[i];
      checkIsSorted(curLeftList);
    }
    for (int i = 0; i < this.numRightVertices; i++) {
      int[] curRightList = this.neighborsForRight[i];
      checkIsSorted(curRightList);
    }

    //Make sure that the bipartite graph is symmetric,
    // i.e. if i is the list of j, then j is also in the list of i
    //Use HashSets for doing the checks
    ArrayList<HashSet<Integer>> neighborsForLeftHashed =
        new ArrayList<HashSet<Integer>>(this.numLeftVertices);
    ArrayList<HashSet<Integer>> neighborsForRightHashed =
        new ArrayList<HashSet<Integer>>(this.numRightVertices);
    for (int[] curList : this.neighborsForLeft) {
      HashSet<Integer> tempSet = new HashSet<Integer>(0);
      for (int member : curList) {
        tempSet.add(member);
      }
      neighborsForLeftHashed.add(tempSet);
    }
    for (int[] curList : this.neighborsForRight) {
      HashSet<Integer> tempSet = new HashSet<Integer>(0);
      for (int member : curList) {
        tempSet.add(member);
      }
      neighborsForRightHashed.add(tempSet);
    }
    for (int i = 0; i < this.numLeftVertices; i++) {
      int[] curList = this.neighborsForLeft[i];
      for (int member : curList) {
        if (!neighborsForRightHashed.get(member).contains(i)) {
          throw new IllegalArgumentException("Adjacency lists are not symmetric! ");
        }
      }
    }
    for (int i = 0; i < this.numRightVertices; i++) {
      int[] curList = this.neighborsForRight[i];
      for (int member : curList) {
        if (!neighborsForLeftHashed.get(member).contains(i)) {
          throw new IllegalArgumentException("Adjacency lists are not symmetric! ");
        }
      }
    }
  }