class Graph
{
Hashtable graph = new Hashtable();
public Graph(List englishDictionary)
{
foreach (string word in englishDictionary)
{
Object nodeObject = graph[word];
GraphNode node = null;
if (nodeObject == null)
{
node = new GraphNode(word);
graph.Add(word, node);
}
else
{
node = (GraphNode)nodeObject;
}
//check the added truncated ones
for (int i = 0; i < word.Length-1; i++)
{
string truncated = word.Substring(0, i) + word.Substring(i + 1);
if (englishDictionary.Contains(truncated))
{
Object nodeObject2 = graph[truncated];
GraphNode node2 = null;
if (nodeObject2 == null)
{
node2 = new GraphNode(truncated);
graph.Add(truncated, node2);
}
else
{
node2 = (GraphNode)nodeObject2;
}
node.connect(node2);
Console.WriteLine("[Truncated]Connected those two: " + word + ", " + truncated);
}
}
//check the addedOnes
string allEnglishCharacters = "abcdefghijklmnopqrstuvwxyz";
List allEnglishCharacterList = allEnglishCharacters.ToList();
foreach (char c in allEnglishCharacterList)
{
for (int i = 0; i <= word.Length; i++)
{
String addedString = word.Substring(0, i) + c + word.Substring(i);
if (englishDictionary.Contains(addedString))
{
Object nodeObject2 = graph[addedString];
GraphNode node2 = null;
if (nodeObject2 == null)
{
node2 = new GraphNode(addedString);
graph.Add(addedString, node2);
}
else
{
node2 = (GraphNode)nodeObject2;
}
node.connect(node2);
Console.WriteLine("[Added] Connected those two: " + word + ", " + addedString);
}
}
}
//Check the changes
foreach (char c in allEnglishCharacterList)
{
for (int i = 0; i < word.Length; i++)
{
String changedString = word.Substring(0, i) + c + word.Substring(i+1);
if (changedString.Equals(word))
{
continue;
}
if (englishDictionary.Contains(changedString))
{
Object nodeObject2 = graph[changedString];
GraphNode node2 = null;
if (nodeObject2 == null)
{
node2 = new GraphNode(changedString);
graph.Add(changedString, node2);
}
else
{
node2 = (GraphNode)nodeObject2;
}
node.connect(node2);
Console.WriteLine("[Changed] Connected those two: " + word + ", " + changedString);
}
}
}
}
}
public void showTheShortestPath(string source, string target)
{
GraphNode startNode = (GraphNode)graph[source];
if (startNode == null)
{
Console.WriteLine("No words found in the dictionary for input: " + source);
return;
}
foreach (GraphNode node in startNode.ConnectedNodes)
{
node.ParentInTraverse = startNode;
}
String result = traverseConnectedNodes(startNode, new List() { startNode }, target);
if (result == null)
{
Console.WriteLine(“Cannot reach to target”);
}
Console.WriteLine(result);
}
private String traverseConnectedNodes(GraphNode startNode, List visitedNodes, string target){
if (startNode.ConnectedNodes.Count == 0)
{
return null;
}
List children = new List();
foreach (GraphNode childrenNode in startNode.ConnectedNodes)
{
if (!visitedNodes.Contains(childrenNode))
{
visitedNodes.Add(childrenNode);
if (childrenNode.Value.Equals(target))
{
Console.WriteLine(“Found the target”);
return startNode.Value+”->”+ childrenNode.Value;
}
Console.WriteLine(“Adding childrenNode: ” + childrenNode.Value);
string result = traverseConnectedNodes(childrenNode, visitedNodes, target);
if (result != null)
{
return startNode.Value + “->” + result;
}
}
}
return null;
}
}
]]>Suppose if we have a level in the graph which has 100s of elements and the first element is the word that we are looking for, then we do not have to push the 100 elements.
[sourcecode language="python"]
…
for word in transforms:
if word == goal:
return currentPath[:]+[word]
if word not in currentPath:
#avoid loops
paths.append(currentPath[:]+[word])
…
…
[/sourcecode]
here is my code :