/// <summary> /// Constructs the graph from a set of words. /// </summary> /// <remarks> /// The algorithm first buckets the words by length. Since only words whose /// lengths differ by at most one can be Levenshtein-adjacent, we only need /// to consider edges between words in buckets i and i+1. /// </remarks> /// <param name="allWords">the set of words that will become nodes in the graph</param> public WordGraph(List <string> allWords) { Validate.IsNotNull(allWords, "allWords"); Graph = new Dictionary <string, List <string> >(); var buckets = BucketByLength(allWords); foreach (var pair in buckets) { int bucketSize = pair.Key; var bucket = pair.Value; var arrangements = new Arrangement <string>(bucket); AddValidEdges(arrangements.GetPairs()); if (buckets.TryGetValue(bucketSize + 1, out List <string> nextBucket)) { AddValidEdges(Combinatorics.CartesianProduct(bucket, nextBucket)); } } }