function lengthOfShortestRoute(start, end, currentDistance = 0, minRoute = 0) {
  // console.log('start:'+start+',end:'+end+',currentDistance:'+currentDistance+',minRoute:'+minRoute);
  if (testHashMap.hasOwnProperty(start)) {
    testHashMap[start]._visited = true;
    const nextNodes = Object.keys(testHashMap[start]);
    nextNodes.forEach((keyOfNextNode) => {
      const newDistance = currentDistance + testHashMap[start][keyOfNextNode];

      //If it reach the end of the recursion
      if (keyOfNextNode === end) {
        testHashMap[start]._visited = false;
        if (newDistance < minRoute || minRoute === 0) minRoute = newDistance;
        return minRoute;
      } else if (!testHashMap[keyOfNextNode]._visited) {
        minRoute = lengthOfShortestRoute(keyOfNextNode, end, newDistance, minRoute); //recursion
      }
    });
  } else {
    throw new Error("NO SUCH ROUTE");
  }
  return minRoute;
}
/**
 * Find the shortest route by given the start and the end node.
 * @param start
 * @param end
 * @param currentDistance
 * @param minRoute
 * @returns {number}
 */
Find the shortest route by given the start and the end node.
Find the shortest route by given the start and the end node .
function lengthOfShortestRoute(start, end, currentDistance = 0, minRoute = 0) {
  if (testHashMap.hasOwnProperty(start)) {
    testHashMap[start]._visited = true;
    const nextNodes = Object.keys(testHashMap[start]);
    nextNodes.forEach((keyOfNextNode) => {
      const newDistance = currentDistance + testHashMap[start][keyOfNextNode];
      if (keyOfNextNode === end) {
        testHashMap[start]._visited = false;
        if (newDistance < minRoute || minRoute === 0) minRoute = newDistance;
        return minRoute;
      } else if (!testHashMap[keyOfNextNode]._visited) {
        minRoute = lengthOfShortestRoute(keyOfNextNode, end, newDistance, minRoute); 
      }
    });
  } else {
    throw new Error("NO SUCH ROUTE");
  }
  return minRoute;
}
function lengthOfShortestRoute ( start , end , currentDistance = 0 , minRoute = 0 ) { if ( testHashMap . hasOwnProperty ( start ) ) { testHashMap [ start ] . _visited = true ; const nextNodes = Object . keys ( testHashMap [ start ] ) ; nextNodes . forEach ( ( keyOfNextNode ) => { const newDistance = currentDistance + testHashMap [ start ] [ keyOfNextNode ] ; if ( keyOfNextNode === end ) { testHashMap [ start ] . _visited = false ; if ( newDistance < minRoute || minRoute === 0 ) minRoute = newDistance ; return minRoute ; } else if ( ! testHashMap [ keyOfNextNode ] . _visited ) { minRoute = lengthOfShortestRoute ( keyOfNextNode , end , newDistance , minRoute ) ; } } ) ; } else { throw new Error ( "NO SUCH ROUTE" ) ; } return minRoute ; }
Find the shortest route by given the start and the end node.
Find the shortest route by given the start and the end node .
// console.log('start:'+start+',end:'+end+',currentDistance:'+currentDistance+',minRoute:'+minRoute); //If it reach the end of the recursion //recursion