How does a Breadth-First Search work when looking for Shortest Path?

Technically, Breadth-first search (BFS) by itself does not let you find the shortest path, simply because BFS is not looking for a shortest path: BFS describes a strategy for searching a graph, but it does not say that you must search for anything in particular. Dijkstra’s algorithm adapts BFS to let you find single-source shortest … Read more

Finding all the shortest paths between two nodes in unweighted undirected graph

As a caveat, remember that there can be exponentially many shortest paths between two nodes in a graph. Any algorithm for this will potentially take exponential time. That said, there are a few relatively straightforward algorithms that can find all the paths. Here’s two. BFS + Reverse DFS When running a breadth-first search over a … Read more

Why doesn’t Dijkstra’s algorithm work for negative weight edges?

Recall that in Dijkstra’s algorithm, once a vertex is marked as “closed” (and out of the open set) – the algorithm found the shortest path to it, and will never have to develop this node again – it assumes the path developed to this path is the shortest. But with negative weights – it might … Read more

tech