What are the differences between:
An algorithm to solve optimization problems
and
An algorithm for solving optimization problems
I think only the second one is correct but I can’t explain why the first one is wrong.
What are the differences between:
An algorithm to solve optimization problems
and
An algorithm for solving optimization problems
I think only the second one is correct but I can’t explain why the first one is wrong.
They are both grammatical, but have different structures, and in some contexts could have different meanings.
Algorithm takes a "for" phrase (usually containing an "-ing" clause) as its complement:
An [algorithm for solving optimization problems]]
Its complement is closely bound to it, and typically forms part of the meaning - an algorithm is underspecified if you don't say what it is for.
Algorithm cannot take a "to"-infinitive clause as a complement, so in your other example, the "to"-infinitive clause must be an adjunct, less tightly bound to the headword: it is not part of the meaning of the headword, but additional information about the instance being referred to:
[An algorithm] [to solve optimization problems]
Other examples of adjuncts are:
An algorithm in my book
An algorithm invented by Knuth.
The meaning of a "to"-infinitive clause as an adjunct is the purpose for which something is done.
In some contexts these will in practice have the same meaning:
This is an algorithm to solve optimization problems.
cannot reasonably mean anything different from the other sentence (with the complement).
But consider
I want an algorithm for solving optimization problems.
What do I want? An algorithm, and specifically one that solves ...
I want an algorithm to solve optimization problems.
what do I want? An algorithm. What do I want it for? To solve optimization problems. What kind of algorithm? I don't care what it's for, as long as it solves my problems.
By the way, there is no rule that explains why algorithm takes a for complement and not a to complement: it's just a fact about present day English.