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

Shortest path to transform one word into another

NEW ANSWER Given the recent update, you could try A* with the Hamming distance as a heuristic. It’s an admissible heuristic since it’s not going to overestimate the distance OLD ANSWER You can modify the dynamic-program used to compute the Levenshtein distance to obtain the sequence of operations. EDIT: If there are a constant number … Read more

The best shortest path algorithm

Dijkstra‘s algorithm finds the shortest path between a node and every other node in the graph. You’d run it once for every node. Weights must be non-negative, so if necessary you have to normalise the values in the graph first. Floyd-Warshall calculates the shortest routes between all pairs of nodes in a single run! Cycle … 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