I have a list of addresses that we can call A-Z. I have used the Google Maps api to geocode these and then find the travel times between each of them to get a 26x26 matrix. I want to come up with a function that takes the address list, the matrix, target node ( which I will call @), and a time limit and returns a list of all possible travel paths that can be achieved from A to @ to any number of different, non-repeating destinations, and back to A before the time limit is reached.
I am thinking that a recursive function might be the best way to do this but I have only learned about them in theory and don't know if it would work.
My thoughts so far are something like this:
var duration = 0;
var path = [];
function getPath(addList, matrix, targetNode, currentNode, timeLimit){
if(duration+matrix[matrix.indexOf(currentNode), matrix.indexOf(A)]>=timeLimit{
path[path.length-1] = A;
// (1) add path to the list of possible paths
}
else{
var tempList = addList;
tempList.splice(tempList.indexOf(currentNode), 1);
for(int i = 0; i < tempList.length; i++){
// (2) increase duration by matrix[matrix.indexOf(currentNode), matrix.indexOf(tempList[i])];
getPath(tempList, matrix, targetNode, tempList[i], timeLimit);
}
}
}
I think that it would work if I could figure out:
(1) How should I store the path (A,@,...,A) as I go into the recursiveness? I cant use a single variable because each function would change it. I was thinking about using something like an xml file as a tree but that seems over the top.
(2) In a similar question, how would I continue increasing duration for each possible path?
On a final note, this would only give me the longest paths from A to @ to other nodes and back to A. I would want to include shorter paths as options as well. For example if my path is {A, @, B, C, D, A} then I would also want to include the following paths {A, @, A}, {A, @, B, A}, and {A, @, B, C, A}. The recursiveness would stop early if I stopped there though so I think that should be handled as a follow up for once the getPath function is totally resolved.
Any pointers, thoughts, help, or comments would be greatly appreciated! Thanks in advance!
Update: I just thought of using a JSON object to handle my issues. It could look something like:
var jsonPaths = [
{
index: 1,
path: [A,@,A],
duration: 187
},
{ .... },
{ .... }
];
and I could push to it and reference it on each function call.