I have programmed for several years. And I found it is always difficult to master an algorithm(What 'master' mean here is to understand how the algorithm works and could implement independently in some programming language).

I could implement some easy algorithms(such as bubble sort, binary search, basic graph traversal algorithms such as DFS, BFS, basic dynamic programming problems such as ). But it is very difficult for me to master some advanced algorithms such as KMP, max-flow.

I could understand the basic idea behind the algorithms. But I am usually stucked at implementation and forget the algorithm in a while.

So my questions are:

  1. what is a reasonable procedure to learn/master a new algorithm?
  2. Do I need practice it again and again in platform such as USACO, topcoder to master it? Or is there any better way?
  • 1
    How frequently do you need to even look at a KMP implementation, much less write one yourself? Unless you're doing standard libraries for a living, at most a once a year, I'd say. Most of us never work with it in the wild. Knowing it's there and how it roughly works it fine and useful, but I wouldn't sweat it if you don't know it by heart. – user7043 Sep 18 '11 at 11:59
  • 1
    It seems to me that one can master an algorithm without even knowing how to code "hello world". This is actually very common among many mathematicians I know . – Andrea Sep 18 '11 at 16:09

In The Art of Computer Programming Donald Knuth suggests doing the algorithm by hand with pencil and paper to really internalize it. I have found this to work well.

I'm sure a lot of people here will deride this as pedantic, but if you're actually doing algorithmic analysis (i.e., trying to find hardware-neutral performance and memory characteristics) this is worth a shot. This is prima facie required if you're analyzing anything in crypto as you likely want to prove certain probabilistic aspects of the algorithm.

I am first to admit that this practice requires a lot of time and is probably at odds with most programmers... I do research so I don't really have deadlines. I do think it is good to get familiar with the algorithm in the abstract though, and seeing the particular programming language as a mere implementation of that algorithm. Unfortunately, the theoretical body of work for the relationship between something like a mathematically-abstract algorithm and its 'reification' (sorry!) into syntax is not yet complete.


I think on a more abstract level about algorithms than in a programming language: the essential ideas, mechanisms and bigO-complexity in space and time do not require the detailed level a programming language has.

Hence I think you shouldn't force it upon yourself to being able to implement every algorithm in one pass. Rather know a large set of algorithms or even more abstract algorithmic concepts. Then you can decide which algorithm to choose, and if you do not find an appropriate one in a library, only then spend more time implementing it.

To learn these algorithms and abstract algorithmic concepts, rather read a book or lecture slides about algorithms, see e.g. https://stackoverflow.com/questions/366418/resources-and-tutorials-to-get-started-on-algorithms.


I think delnan's comment is pretty much the answer you need. To add something from my side, i would say every requirement or code you implement is an Algorithm in itself. Suppose you want to add 100 employees to a company database and extract the one's with salary less than 5k/month. The algo for this will be :-

a list of 100 employees, EmployeeTable
parameter to sort, SalaryParam
a sortedList of employees, EmployeeTableWithFilter
define variables :
an integer count, i=0
While i < 100, do :
if List[i].Salary == SalaryParam

I am not that good at Algorithm but i hope you can buy what i am trying to explain. In essence, an Algorithm is a generalized and effective method expressed as a finite list of well-defined instructions for calculating a function. So, every function or program you write is an algorithm in itself

Not the answer you're looking for? Browse other questions tagged or ask your own question.