Use the solver below to find the shortest word ladder from one word to another, if one exists. Select the "Use Common Words" option to find a word ladder solution using more common dictionary words.
To solve the problem of finding the shortest path from the Start Word to the Target Word, we can model the problem as a graph traversal. Each word can be considered a node, and an edge exists between two nodes if one word can be transformed into the other by changing exactly one letter.
The breadth-first search algorithm (BFS) is ideal for this type of problem since it explores all nodes at the present depth level before moving on to nodes at the next depth level, guaranteeing that we find the shortest path.
The breadth-first search works as follows:
The time complexity is O(N * M), where N is the number of words in the word list and M is the length of each word. This is because, for each word in the list, we are comparing it to the current word (which takes O(M) time).
The space complexity is O(N * M), as we store the entire word list and the paths in the queue.
The Common Words Solver additionaly takes word frequency into account so that the solver produces a path using words most people know and use everyday. Each word is assigned a frequency rating, where the lower the frequency rating, the more common the word. To modify the other search tree to find the path with the lowest sum of frequency ratings, or the shortest path using only common words, we can use a modified version of Dijkstra's algorithm. Instead of finding the shortest path based on the number of steps (as in BFS), we will now find the path with the lowest total frequency rating, where the frequency rating of each word is a weight on the node.
Here are the main differences from the Shortest Path Solver:
The modified search tree using Dijkstra's algorithm works as follows:
The time complexity is O(N * M * log(N)), where N is the number of words in the word list and M is the length of each word. Sorting the priority queue takes O(log(N)), and for each word, we check all the words in the list, leading to the total time complexity.
The space complexity is O(N), where N is the number of words in the word list, because we store each word and its associated path and frequency sum in the priority queue and visited set.