Dot Net Perls

Anagram Open Source in Windows

by Sam Allen - Updated June 13, 2009

Problem. You want an anagram-finding program in C# .NET and Windows Forms. It must works instantly and is free and simple. Simply type in some letters and the anagrams must appear instantly on your screen. Solution. Use the Anagram Free Finder application developed by Dot Net Perls.

Anagrams

1. Anagram finder in Windows Forms

This article is an interesting summary of parts of my third open-source project, called Anagram Free Finder. It is a C# program that is blazingly fast at finding anagrams and displaying them. It is orders of magnitude faster than the obvious solutions to the problem. It is available for download now.

2. How did you make it?

Very slowly. I spent months and years slaving over C and C++ and designing complex data structures for Scrabble and anagrams. Then port the code to C# and turn it into something that can be used outside of my own computer. Make it extremely fast and simple. Write the solution in 400 lines of code in 4 hours.

3. Why does it use the DAWG?

Performance. The directed acyclic word graph is why the program is fast (see the Optimization section below). It is essentially a tree of letters where the first and last parts are all shared in memory. [C# Directed Acyclic Word Graph - dotnetperls.com]

4. How does it use LINQ sorting?

LINQ is a profoundly useful technology, and I use it to sort the results of the search from longest to shortest in length. I have an interesting article on exactly that, and that came in handy here. [C# Sort Strings by Length - dotnetperls.com]

// Find anagrams for the string.
var results = _anagram.FindForString(textBox1.Text);

// LINQ sort words by length, and then by first letter.
var byLength = (from item in results
                orderby item.Length descending
                select item).ToArray();
listBox1.DataSource = byLength;

5. How did you build the UI?

It uses Windows Forms. There are a few important parts of the user interface for this program. I put my experience to use when designing it and the interface was finished within only a few minutes, and it looks good in my opinion and when compared to similar programs. Here are some relevant controls to consider.

6. Optimizations

In version 2 of the application, I sought to make the code tidier and faster. There were many housekeeping tasks, such as pulling constants into const members, and general commenting and tidiness issues.

VersionOptimization typeTime in ms
1None1250
2Changed from char Dictionary to int array826
3Iterate through numbers in loops, not chars764
4Removed ToLower and replaced with char ranges733
5Use char array instead of strings to keep progress436

7. How fast is it?

This program can find all the anagrams for a fairly typical word of 6 letters in half a millisecond. Programs that iterate through word lists to find anagrams would take about half a second to do the same. The end result is that the method I use in this program runs 1000 times faster than the obvious method.

8. Conclusion

There you have it: download an anagram finder that beats every other anagram finding program. Don't pay $8.00 for a similar program. This program uses 1985 technology, which coincidentally is still unbelievably fast these days. Most of all, learn and have fun. [Anagram Free Finder - C# Anagrams Fast With GUI - code.msdn.microsoft.com]

Dot Net Perls
Algorithms | Binary Representation for Integer | IEqualityComparer Dictionary | Pathfinding Algorithm | String Occurrence Count | Swap Methods
C# | Reflection Field Example | Validate Characters in String | Main Args Examples | Enum String Method
© 2009 Sam Allen. All rights reserved.