lib/graph.js
/**
     * Returns a list of vertices along the shortest path between two given vertices.
     *
     * @param {number} start the starting vertex
     * @param {number} end the ending vertex
     * @param {boolean} [isAcyclic=false] whether the graph is acyclic
     * @param {boolean} [isNonNegative=false] whether all edges are non-negative
     * @returns {Array<number>}
     */
Returns a list of vertices along the shortest path between two given vertices.
Returns a list of vertices along the shortest path between two given vertices .
path(start, end, isAcyclic, isNonNegative) {
      const { weighted } = this.constructor;
      const { vertices } = this;
      const predecessors = new Array(vertices).fill(-1);
      const distances = new Array(vertices).fill(Infinity);
      const isFound = !weighted ? this.searchUnweighted(start, end, predecessors)
        : isAcyclic ? this.searchTopological(start, end, distances, predecessors)
          : isNonNegative ? this.searchDijkstra(start, end, distances, predecessors)
            : this.searchBellmanFord(start, end, distances, predecessors);
      if (!isFound) return [];
      const path = [];
      let last = end;
      while (~last) {
        path.unshift(last);
        last = predecessors[last];
      }
      return path;
    }
path ( start , end , isAcyclic , isNonNegative ) { const { weighted } = this . constructor ; const { vertices } = this ; const predecessors = new Array ( vertices ) . fill ( - 1 ) ; const distances = new Array ( vertices ) . fill ( Infinity ) ; const isFound = ! weighted ? this . searchUnweighted ( start , end , predecessors ) : isAcyclic ? this . searchTopological ( start , end , distances , predecessors ) : isNonNegative ? this . searchDijkstra ( start , end , distances , predecessors ) : this . searchBellmanFord ( start , end , distances , predecessors ) ; if ( ! isFound ) return [ ] ; const path = [ ] ; let last = end ; while ( ~ last ) { path . unshift ( last ) ; last = predecessors [ last ] ; } return path ; }
Returns a list of vertices along the shortest path between two given vertices.
Returns a list of vertices along the shortest path between two given vertices .