Page 1 of 1

Algorithm vs. Strategy

Posted: Mon Jun 19, 2006 7:07 pm
by Ambush Commander
What's the difference?

Posted: Mon Jun 19, 2006 7:27 pm
by Christopher
An algorithm is a set of steps to achieve a goal. A strategy is the plan that would use the algorithm. In software, algorithms are somewhat akin to what the military calls tactics. Like a tactic, an algorithm has a certain repeatable application in a specific set of circumstances.

Posted: Mon Jun 19, 2006 7:51 pm
by Ambush Commander
So a strategy contains an algorithm, but not always?

Posted: Mon Jun 19, 2006 8:09 pm
by Christopher
I guess yes.

Strategy and tactics are similar things, just at different levels. And strategy and algorithm are similar in the sense that they are a set of steps. But we are dealing with the sense of the words here. You would not call an algorithm a plan, because a plan is a set of future steps whereas an algorithm is a set of known steps. Perhaps the difference in sense is a difference in predictablity -- the tactical/algorithm level being more predictable than the strategic. And predictablity is due to tactics/algorithms having a managable number of variables, unlike a strategy.

Posted: Mon Jun 19, 2006 8:17 pm
by Ambush Commander
Hmm... that makes sense.

Posted: Tue Jun 20, 2006 2:54 am
by Maugrim_The_Reaper
My own take. Every situation may require a specific algorithm from a selection. So you might have one main class (the Situation) and multiple applicable algorithms. Instead of bundling a few algorithms into the method, it's more maintainable and open to adaptation (if a new algorithm shows up) to refactor each into it's own separate sub classes, each extending a common parent. This refactor produces the Strategy pattern; you have many algorithms one of which will eventually be selected for the Situation.

Taking the military example - your general (Herr Developer) is entering battle. After assessing the battlefield and current conditions, he selects a tactic to employ. The choice of tactic is akin to a strategy.

One, maybe not so perfect example, is page generation. Say I have a ton of template data, simple variable array. If it's a web page I may want to output it - simple stuff. Next day someone pops up and tells me I need to make the application output XML with XSLT. Someone else determines PDF is a must. One possible way of performing all this is to add a Strategy pattern, where the Page Generator as well as handling template variables, accepts a Strategy in the form of a HTML Template Engine, XML Generator, or PDF Generator. Think that example made sense...

Posted: Tue Jun 20, 2006 7:53 am
by Roja
My take is different.

Algorithm: A detailed sequence of actions to perform to accomplish some task.
Strategy: An elaborate and systematic plan of action.

Algorithms are finite, defined, and are (usually) task-specific. Strategies on the other hand, are general and non-task-specific.

Thats my take, but its coming from more of a math/crypto background than a pure programming point of view.

Posted: Tue Jun 20, 2006 8:04 am
by Maugrim_The_Reaper
I think my take was out of context - I referred to the pattern, and not sure that was what was asked... oh, well. :)

Posted: Tue Jun 20, 2006 5:55 pm
by Robert Plank
I would say the algo is the computation you do i.e. summing all values in an array, the strategy (ick, don't say strategy, say pattern... that confuses some of us who read what you say and think of the Strategy Pattern) is the design, like what variables go in which objects/tables and how those objects relate.

Posted: Tue Jun 20, 2006 11:18 pm
by alex.barylski
An algorithm...is more concrete...

For example Blowfish...is an encryption algorithm...you must carry out specific actions before you qualify as a blowfish encryption...

Strategy...is more of a general overview of how to tackle a problem...

Like in football...when player huddle...the QB gives a strategy and the team executes it the best they can, but not always according to plan...

Strategy is slightly more flexible than algorithm...

When building a web application I have a (albeit forever changing) strategy which I follow...by using various algorithms...

An implementation is most concrete...as it's as specific as you can get...

You can implement an algorithm differently: For instance the implementation of Blowfish in PHP would differ significantly when done in C/C++ but they would both follow an algorithm set out by the author/founder of Blowfish...

So IMHO the chain of events goes something like this:

Strategy -> Algorithm -> Implementation

Highest level to lowest level...

Edit: I also wanted to note...an algorithm is more analytical than a strategy...an algorithm may need mathematical proofs, etc to prove a theory...whereas a strategy...can be ideas...charts, graphs, theory, speculations...and the implementation...well thats the POC (proof of concept)... :)

Cheers :)