Question
Given a list L of words, one can define an undirected graph, called a WordGraph on the set of words as follows. We have one
Given a list L of words, one can define an undirected graph, called a WordGraph on the set of words as follows. We have one vertex for each word, and an edge between two words word1 and word2 if one can transform one of the words to the other via a character insertion or a character substitution.
For example, suppose L = [pain, gain, pan, span, gait, wait]. In the corresponding WordGraph, (pain, gain) is an edge because we can substitute the character `p' in `pain' with a `g' to obtain `gain'. The pair (pain, pan) is an edge because we can insert `i' in `pan' to obtain `pain'.
In the example, the edge set is {(pain, gain), (pain, pan), (gain, gait), (pan, span), (gait, wait)}.
Write a WordGraph class (in either Java or Python) with the following functionality:
A constructor that takes as input a list of words, and builds the corresponding WordGraph. The type of the input list of words is a List of String.
A function/method numberOfComponents() that returns the number of connected components in the WordGraph. Thus, if w is a WordGraph object, then w.numberOfComponents() should return the number of connected components of w.
A function shortestPath(word1, word2) that takes as input two words word1 and word2 (whose type is String) and returns the list of words in a shortest path between word1 and word2 in the WordGraph. If w is a WordGraph object for our example, then w.shortestPath("pain", "span") should return the list [pain, pan, span].
You may assume that words only have characters from the English alphabet, and we don't distinguish upper and lower case letters. For testing, we plan to use subsets from the Stanford GraphBase's list of five-letter words. Please test your program on this list to check if the functions complete in a reasonable amount of time. We will also test on other hand-crafted lists.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started