lib/unweighted-adjacency-list.js
/**
   * Creates an adjacency list from a given grid or adjacency matrix.
   *
   * @param {Grid|BinaryGrid|SymmetricGrid} grid
   * @returns {UnweightedAdjacencyList}
   */
Creates an adjacency list from a given grid or adjacency matrix.
Creates an adjacency list from a given grid or adjacency matrix .
static fromGrid(grid) {
    const vertices = grid.rows;
    const offset = vertices + 1;
    const empty = grid.pad || 0;
    const array = new Array(offset).fill(offset);
    let edges = 0;
    for (let i = 0; i < vertices; i++) {
      array[i + 1] = i === 0 ? offset : array[i];
      for (let j = 0; j < vertices; j++) {
        if (grid.get(i, j) !== empty) {
          array.push(j);
          array[i + 1] += 1;
          edges++;
        }
      }
    }
    const graph = new this({ vertices, edges });
    graph.set(array);
    return graph;
  }
static fromGrid ( grid ) { const vertices = grid . rows ; const offset = vertices + 1 ; const empty = grid . pad || 0 ; const array = new Array ( offset ) . fill ( offset ) ; let edges = 0 ; for ( let i = 0 ; i < vertices ; i ++ ) { array [ i + 1 ] = i === 0 ? offset : array [ i ] ; for ( let j = 0 ; j < vertices ; j ++ ) { if ( grid . get ( i , j ) !== empty ) { array . push ( j ) ; array [ i + 1 ] += 1 ; edges ++ ; } } } const graph = new this ( { vertices , edges } ) ; graph . set ( array ) ; return graph ; }
Creates an adjacency list from a given grid or adjacency matrix.
Creates an adjacency list from a given grid or adjacency matrix .