I'm trying to apply BFS to find the length of the shortest path in a graph, but am not quite getting the right result.
I try to find the shortest path by visiting each node in the graph; then mark the ones that are visited, and continue recording the length of the path. What I hope to return is an array that contains the shortest path, but I think I am doing something wrong in the process.
I think this has something to do with how I am indexing my arrays and recording my distances.
My input is currently formatted in the form of an array that contains the neighbors for each vertex i. So, for instance, graph[i] would give you an array of neighbors of vertex i.
Any thoughts on how I can go about fixing my issue would be very helpful. Thanks!
var shortestPathLength = function(graph) {
let distances = []
let pq = []
distances[0] = 0
let mygraph = {}
for (var i = 0; i<graph.length; i++) {
distances[i] = -1
mygraph[i] = graph[i]
}
pq.push(mygraph[0])
while(pq.length > 0) {
let min_node = pq.shift()
for(var i = 0; i<min_node.length; i++) {
candidate = distances[i] + 1
if(distances[min_node[i]]== -1) {
distances[min_node[i]] = distances[i] + 1
pq.push(graph[min_node[i]])
}
else if (candidate < distances[min_node[i]]) {
distances[min_node[i]] = distances[i] + 1
}
}
}
function getSum(total, num) {
return total + num;
}
console.log(distances)
return distances.length
};
distances[i] = -1should bedistances[i] = Infinitysince other nodes have not been discovered yet. - kiner_shahpq(priority-queue?) and fetchmin_nodes from it (instead of simply the first). Also for a BFS you won't need theelse ifbranch - you will only find nodes that you didn't visit or to which you already found the minimum distance. All your edges have length 1, and a BFS will visit nodes in ascending distance order only. - Bergi