Algorithm vs. Strategy

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Algorithm vs. Strategy

Post by Ambush Commander »

What's the difference?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

So a strategy contains an algorithm, but not always?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm... that makes sense.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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. :)
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
Post Reply