It seems that on this site, people will often correct others for confusing "algorithms" and "problems." What are the difference between these? How do I know when I should be considering algorithms and considering problems? And how do these relate to the concept of a language in formal language theory?

- 72,336
- 29
- 179
- 389

- 29,754
- 5
- 64
- 121
-
An algorithm is a way to solve a problem. – reinierpost Dec 11 '14 at 12:13
2 Answers
For simplicity, I'll begin by only considering "decision" problems, which have a yes/no answer. Function problems work roughly the same way, except instead of yes/no, there is a specific output word associated with each input word.
Language: a language is simply a set of strings. If you have an alphabet, such as $\Sigma$, then $\Sigma^*$ is the set of all words containing only the symbols in $\Sigma$. For example, $\{0,1 \}^*$ is the set of all binary sequences of any length. An alphabet doesn't need to be binary, though. It can be unary, ternary, etc.
A language over an alphabet $\Sigma$ is any subset of $\Sigma^*$.
Problem: A problem is some question about some input we'd like answered. Specifically, a decision problem is a question which asks, "Does our given input fulfill property $X$?
A language is the formal realization of a problem. When we want to reason theoretically about a decision problem, we often examine the corresponding language. For a decision problem $X$, the corresponding language is:
$L = \{w \mid w$ is the encoding of an input $y$ to problem $X$, and the answer to input $y$ for problem $X$ is "Yes" $ \}$
Determining if the answer for an input to a decision problem is "yes" is equivalent to determining whether an encoding of that input over an alphabet is in the corresponding language.
Algorithm: An algorithm is a step-by-step way to solve a problem. Note that there an algorithm can be expressed in many ways and many languages, and that there are many different algorithms solving any given problem.
Turing Machine: A Turing Machine is the formal analogue of an algorithm. A Turing Machine over a given alphabet, for each word, either will or won't halt in an accepting state. Thus for each Turing Machine $M$, there is a corresponding language:
$L(M) = \{w \mid M$ halts in an accepting state on input $w\}$.
(There's a subtle difference between Turing Machines that halt on all inputs and halt on yes inputs, which defines the difference between complexity classes $\mathsf{R}$ and $\mathsf{RE}$.)
The relationship between languages and Turing Machines is as follows
Every Turing Machine accepts exactly one language
There may be more than one Turing Machine that accept a given language
There may be no Turing Machine that accepts a given language.
We can say roughly the same thing about algorithms and problems: every algorithm solves a single problem, but there may be 0, or many, algorithms solving a given problem.
Time Complexity: One of the most common sources of confusion between algorithms and problems is in regards to complexity classes. The correct allocation can be summarized as follows:
- An algorithm has a time complexity
- A problem belongs to a complexity class
An algorithm can have a certain time complexity. We say an algorithm has a worst-case upper-bounded complexity $f(n)$ if the algorithm halts in at most $f(n)$ steps for any input of size $n$.
Problems don't have run-times, since a problem isn't tied to a specific algorithm which actually runs. Instead, we say that a problem belongs to a complexity class, if there exists some algorithm solving that problem with a given time complexity.
$\mathsf{P}, \mathsf{NP}, \mathsf{PSPACE}, \mathsf{EXPTIME}$ etc. are all complexity classes. This means they contain problems, not algorithms. An algorithm can never be in $\mathsf{P}$, but if there's a polynomial-time algorithm solving a given problem $X$, then $X$ can be classified in complexity class $\mathsf{P}$. There could also be a bunch of other algorithms runs in different time complexity will also be able to solve the problem with the same input size under different time complexity, i.e. exponential-time algorithms, but since there already exists a single polynomial-time algorithm accepting $X$, it is in $\mathsf{P}$.

- 9
- 4

- 29,754
- 5
- 64
- 121
-
1
-
I think it would be good to mention that there are other kinds of computational problems (e.g. search problems). – Kaveh Aug 18 '13 at 09:43
-
A language can be infinite but an algorithm MUST halt in a finite number of steps. – Tanmoy Banerjee Mar 19 '14 at 15:27
-
2Says who? That kind of thinking is part of why people had so much trouble formalizing and algorithm before the intention of the Turing Machine. The Church-Turing Thesis says that an algorithm IS a turing machine and vice versa, and not all turing machines halt. – Joey Eremondi Mar 20 '14 at 03:09
-
5Dude, this is the greatest answer I've ever seen. You just summarized all of computer science in 1 page. – CaptainCodeman Jan 02 '16 at 21:44
-
Thanks @CaptainCodeman! Though I did set myself up for it by asking it as a reference question :P – Joey Eremondi Jan 03 '16 at 06:35
-
1NP isn't really about the time complexity of solving a problem; it says (roughly) that a solution can be verified in polynomial time. NP and co-NP are very different, but take exactly the same time to solve. – gnasher729 Mar 30 '17 at 18:26
-
1@gnasher729 there's a theorem that says it can be defined in terms of verifying, but it's actual definition is in terms of time complexity for Non deterministic machines, thus the name NP: nondeterministic polynomial – Joey Eremondi Mar 31 '17 at 07:03
-
@jmite Thank you for your answer. I also want to add that instead of saying "if there exists some algorithm solving that problem with a given time complexity" we say "if there exists at least one algorithm solving that problem with a given time complexity". Because for example it is enough to show one polynomial algorithm for solving a decision NP-complete problem to be in P. – YOUSEFY Sep 04 '17 at 11:31
-
@jmite Also, another note: instead of saying "... all complexity classes. [...] contain problems, not algorithms", we should say " ... all complexity classes. [...] contain languages". So, complexity class has a language [not algorithms and not problems]. The reason behind this is that x the input of the algorithm is subset of the alphabet. So, if $x \in P$, (where x is input and P is the problem) since x is input so it is subset from the alphabet. But P has no meaning here! there is a definition of problem. We have definition of problem in optimization that says: .... – YOUSEFY Sep 04 '17 at 11:52
-
a problem is given some inputs with description and properties how to answer it. Now if we say $x \in L$, (where x is the input and L is the language that has special encoding of the problem) then we say the definition is fine. Suppose that we do not know about x and we want to know whether it is a subset of this language or not. Then we use "algorithm" to see. Suppose Algorithm A is able to test whether x is subset of L or not. Then, we are able to say: If A[x] accepts, then $x \in L$ and if A[x] rejects, then $x \notin L$. – YOUSEFY Sep 04 '17 at 12:01
Even more TLDR:
Problem: Given an input return an output related to the input in some way. Decision problems answer yes/no questions related to the input Optimization problems seek to minimize or maximize some property related to the input. Algorithm: a specific instance of solving a problem. Problems may have many algorithms that solve the problem Language: formalization of the problem (in decision format). That is, the set of all strings spelled from an alphabet for which the problem is true (in the case of a decision problem).

- 3
- 3
-
Welcome to COMPUTER SCIENCE @SE. Append two blanks to lines you want a break after, e.g.
Algorithm:
– greybeard Oct 05 '21 at 11:08