Dot Net Perls
C#

Anagram Open Source in Windows

by Sam Allen

Problem

Write 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. We refuse to accept a solution that involves waiting at all. We also require a simple and attractive GUI that we can use easily.

Solution: C#

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.

How did you make it?

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. Give it away and put a link to your site on it.

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. For C#, I simply used two arrays, one being a jagged array. Please see some of my work with DAWG trees for more information.

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. (I read my own articles.) Here is some example code.

// Find anagrams for the string.
// Use the var keyword for shorter syntax.
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;

How did you build the user interface?

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.

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. What follows is a table listing some changes and the effect they had on performance.

Version Optimization type Time in ms
1 None 1250
2 Changed from char Dictionary object to int array 826
3 Iterate through numbers in loops, not chars 764
4 Removed ToLower and replaced with char ranges 733
5 Use char array instead of strings to keep progress
Not shown on graph
436

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.

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. Help others learn, play educational games, or just enjoy themselves.

© 2008 Sam Allen. All rights reserved.

Ads by The Lounge