app/js/controllers.js
/*
    Summary: This recursive function will utilize Depth-first search to find all paths from currentStop to destination
    Parameters:
        currentStop: string containing the name of the current stop
        destination: string containing the name of the destination stop
        minIterations: number of minimum stops dictated by the constraints. Provide 0 if no minimum requirements
        maxIterations: number of maximum stops dictated by the constraints. Provide 0 if no maximum requirements
        findMinDistance: boolean representing whether or not to search for the single, minimum distance path
        maxDistance: maximum distance allowed by the problem. Provide 0 if no maximum requirements.
        currentIteration: number of current stops passed. Used for stopping the search should we pass the constraint.
        correctPaths: object containing the number of viable paths fitting the constraints so far and the string representations of the paths
        currentPath: object containing the names of the stops passed so far and the length traveled
    */
This recursive function will utilize Depth-first search to find all paths from currentStop to destination
Parameters:
currentStop: string containing the name of the current stop
destination: string containing the name of the destination stop
minIterations: number of minimum stops dictated by the constraints. Provide 0 if no minimum requirements
maxIterations: number of maximum stops dictated by the constraints. Provide 0 if no maximum requirements
findMinDistance: boolean representing whether or not to search for the single, minimum distance path
maxDistance: maximum distance allowed by the problem. Provide 0 if no maximum requirements.
currentIteration: number of current stops passed. Used for stopping the search should we pass the constraint.
correctPaths: object containing the number of viable paths fitting the constraints so far and the string representations of the paths
currentPath: object containing the names of the stops passed so far and the length traveled
This recursive function will utilize Depth - first search to find all paths from currentStop to destination Parameters : currentStop : string containing the name of the current stop destination : string containing the name of the destination stop minIterations : number of minimum stops dictated by the constraints . Provide 0 if no minimum requirements maxIterations : number of maximum stops dictated by the constraints . Provide 0 if no maximum requirements findMinDistance : boolean representing whether or not to search for the single minimum distance path maxDistance : maximum distance allowed by the problem . Provide 0 if no maximum requirements . currentIteration : number of current stops passed . Used for stopping the search should we pass the constraint . correctPaths : object containing the number of viable paths fitting the constraints so far and the string representations of the paths currentPath : object containing the names of the stops passed so far and the length traveled
function depthFirstSearch(currentStop, destination, minIterations, maxIterations, findMinDistance, maxDistance, currentIterations, correctPaths, currentPath) {
        if ((currentIterations < maxIterations || maxIterations == 0) && (currentPath.pathLength < maxDistance || maxDistance == 0)) {
            for (var x in $scope.nodes) {
                if ($scope.nodes[x].startPoint == currentStop) {
                    currentPath.pathString = currentPath.pathString + $scope.nodes[x].startPoint;
                    currentPath.pathLength = currentPath.pathLength + $scope.nodes[x].length;
                    if ($scope.nodes[x].endPoint == destination
                        && currentIterations + 1 >= minIterations
                        && (currentPath.pathLength < maxDistance || maxDistance == 0)
                        ) {
                        currentPath.pathString = currentPath.pathString + $scope.nodes[x].endPoint;
                        if (findMinDistance == true && correctPaths.paths.length > 0) {
                            if (currentPath.pathLength < correctPaths.paths[0].pathLength) {
                                correctPaths.paths[0] = {
                                    'pathString' : currentPath.pathString,
                                    'pathLength' : currentPath.pathLength
                                }
                            }
                        } else {
                            correctPaths.paths[correctPaths.paths.length] = {
                                'pathString' : currentPath.pathString,
                                'pathLength' : currentPath.pathLength
                            };
                        }
                        currentPath.pathString = currentPath.pathString.substring(0, currentPath.pathString.length - 1);
                    }
                    var currentMinDistance = 0;
                    if (correctPaths.paths.length > 0) {
                        currentMinDistance = correctPaths.paths[0].pathLength;
                    } else {
                        currentMinDistance = currentPath.pathLength;
                    }
                    if (findMinDistance == false || (findMinDistance == true && currentPath.pathLength <= currentMinDistance)) {
                        depthFirstSearch($scope.nodes[x].endPoint,
                            destination,
                            minIterations,
                            maxIterations,
                            findMinDistance,
                            maxDistance,
                            currentIterations + 1,
                            correctPaths,
                            currentPath
                        );
                    }
                    currentPath.pathString = currentPath.pathString.substring(0, currentPath.pathString.length - 1);
                    currentPath.pathLength = currentPath.pathLength - $scope.nodes[x].length;
                }
            }
        }
    }
function depthFirstSearch ( currentStop , destination , minIterations , maxIterations , findMinDistance , maxDistance , currentIterations , correctPaths , currentPath ) { if ( ( currentIterations < maxIterations || maxIterations == 0 ) && ( currentPath . pathLength < maxDistance || maxDistance == 0 ) ) { for ( var x in $scope . nodes ) { if ( $scope . nodes [ x ] . startPoint == currentStop ) { currentPath . pathString = currentPath . pathString + $scope . nodes [ x ] . startPoint ; currentPath . pathLength = currentPath . pathLength + $scope . nodes [ x ] . length ; if ( $scope . nodes [ x ] . endPoint == destination && currentIterations + 1 >= minIterations && ( currentPath . pathLength < maxDistance || maxDistance == 0 ) ) { currentPath . pathString = currentPath . pathString + $scope . nodes [ x ] . endPoint ; if ( findMinDistance == true && correctPaths . paths . length > 0 ) { if ( currentPath . pathLength < correctPaths . paths [ 0 ] . pathLength ) { correctPaths . paths [ 0 ] = { 'pathString' : currentPath . pathString , 'pathLength' : currentPath . pathLength } } } else { correctPaths . paths [ correctPaths . paths . length ] = { 'pathString' : currentPath . pathString , 'pathLength' : currentPath . pathLength } ; } currentPath . pathString = currentPath . pathString . substring ( 0 , currentPath . pathString . length - 1 ) ; } var currentMinDistance = 0 ; if ( correctPaths . paths . length > 0 ) { currentMinDistance = correctPaths . paths [ 0 ] . pathLength ; } else { currentMinDistance = currentPath . pathLength ; } if ( findMinDistance == false || ( findMinDistance == true && currentPath . pathLength <= currentMinDistance ) ) { depthFirstSearch ( $scope . nodes [ x ] . endPoint , destination , minIterations , maxIterations , findMinDistance , maxDistance , currentIterations + 1 , correctPaths , currentPath ) ; } currentPath . pathString = currentPath . pathString . substring ( 0 , currentPath . pathString . length - 1 ) ; currentPath . pathLength = currentPath . pathLength - $scope . nodes [ x ] . length ; } } } }
Summary: This recursive function will utilize Depth-first search to find all paths from currentStop to destination
Parameters:
currentStop: string containing the name of the current stop
destination: string containing the name of the destination stop
minIterations: number of minimum stops dictated by the constraints.
Summary : This recursive function will utilize Depth - first search to find all paths from currentStop to destination Parameters : currentStop : string containing the name of the current stop destination : string containing the name of the destination stop minIterations : number of minimum stops dictated by the constraints .
// We must obey the constraints of the problem. This also helps improve performance by not bothering with trips that are too long // We're only interested in edges that start with our start point // Record where we are and how long it took to get here // Does this road go where we're trying to go AND meet our constraints? // Since we're looking ahead by 1 stop, we'll use currentIterations + 1 // Complete the correct path // Length has already been added // Record that we found a correct path // If we're trying to find the min distance and there is already a working path found... // If our current path is better than our previous path... // Replace the winning path // Otherwise, add the current path to the list of correct paths // Take the endPoint back off // We must go deeper only if we're not looking for the min distance // OR we are looking for the min distance and we haven't busted it yet // We need to grab the current winning distance. Since we can't count on correctPaths to have any values // at this point, we'll grab the min from correctPaths if it has one, otherwise we'll fill it with the // current path length // Veni, vidi, vici. We're heading back up.