Skip to content

Instantly share code, notes, and snippets.

@TahsinTariq
Last active October 1, 2020 08:31
Show Gist options
  • Save TahsinTariq/5c4ba6b74dd1279f6d4bcfea6a3cbefd to your computer and use it in GitHub Desktop.
Save TahsinTariq/5c4ba6b74dd1279f6d4bcfea6a3cbefd to your computer and use it in GitHub Desktop.
function AStar(v1, v2){
function route(v1, v2){
if (parents[v2] != 'NONE'){
route(v1, parents[v2])
}
path.push(v2)
}
parents = {}
searched = []
fcost = {}
queue = {}
parents[v1] = "NONE"
gcost[v1] = 0
fcost[v1] = h(v1)
queue[v1] = fcost[v1]
while (queue.length != 0){
x = sortProperties(queue);
node = x[0][0];
delete queue[node];
searched.push(node);
if (node == v2){
try{
print("found");
route(v1, v2);
}
catch{
print('No path exists');
}
return 0;
}
hash_ = generatechild(node);
for(let[n, c] of Object.entries(hash_)){
if (c + gcost[node] < gcost[n]){
if (searched.includes(n)){
continue;
}
else{
gcost[n] = c + gcost[node];
fcost[n] = (gcost[n] + h(n));
parents[n] = node;
if (!queue.hasOwnProperty(n)){
queue[n] = fcost[n];
}
}
}
}
}
print('NO path Found')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment