What is the difference between an algorithm and a function? [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Are they the same thing?

No.
A function is a block of code in a computer program.
An algorithm is an abstract concept that describes how to solve a problem.

In mathematics, a function is "a mathematical relation such that each element of a given set (the domain of the function) is associated with an element of another set (the range of the function)" (source - google.com, define:function).
In computer science, a function is a piece of code that optionally takes parameters, optionally gives a result, and optionally has a side effect (depending on the language - some languages forbid side-effects). It must have a specific machine implementation in order to execute.
The computer science term came out of the mathematical term, being the machine implementation of the mathematical concept.
An algorithm is "a precise rule (or set of rules) specifying how to solve some problem" (source - google.com, define:algorithm). An algorithm can be defined outside of computer science, and does not have a definitive machine implementation. You can "implement" it by writing it out by hand :)
The key difference here is, in computer science, an algorithm is abstract, and doesn't have a definitive machine implementation. A function is concrete, and does have a machine implementation.

An algorithm is a set of instructions.
In computer programming, a function is an implementation of an algorithm.

An algorithm is a series of steps (a process) for performing a calculation, whereas a function is the mathematical relationship between parameters and results.
A function in programming is different than the typical, mathematical meaning of function because it's a set of instructions implementing an algorithm for calculating a function.

An algorithm describes the general idea, whereas a function is an actual working implementation of that idea.

It might be almost a philosophical question, but I'de say an algorithm is the answer(or how-to) to a problem at hand where as a function does not necerally answer one problem in itself.
What you want to do normally is split your algorithm into severals function that each has their own goal, which, in the end, will achieve the problem at hand, when used together.
Ex : You want to Sort a list of numbers. The algorightm used would be for example the Merge-sort algorithm. That specific algorithm is actually composed of more than one functions, one that will split your array, another to check for equality, another to merge everything back together, and so on.

A mathematical function is the interface, or specification of the inputs and outputs of an algorithm.
An algorithm is the precise recipe that defines the steps that may implement a function.
Confusingly, computer language designers diffuse this distinction by using the concept function, func, method, etc, to talk about both concepts.
So the distinction is one of specification vs. definition.
There is also a semantic distinction: an algorithm seeks to provide a solution to a problem. It is goal-oriented. A function simply is - there is no essential teleological component.

An algorithm is the implementation of a function.
In some cases, the algorithm is trivial:
Function: Sum of two numbers.
Algorithm:
int sum(int x, int y){
return x+y;
}
In other cases, it is not:
Function: Best chess move.
Algorithm:
Move bestChessMove(State gameState){
//I don't know the algorithm.
}

Algorithm is a (possibly informal but necessarily precise) sequence of instructions. Function is a formal rule that associates some input w/ a specific output. Functions implement and formalize algorithms. E.g. we can formalize "go from a to b" as go(a)=b or go(x,a)=b (w/ x the one who goes), etc. According to Wikipedia,
An algorithm is an effective method that can be expressed within a
finite amount of space and time and in a well-defined formal
language for calculating a function.
So you can say that "go(ing) from a to b" is an effective method for calculating go(a)=b (if you want)

An Algorithm usually refers to the method or process used to end up with the result after mathematical processing. A Function is a subroutine used to avoid writing the same code over and over again. They are different in their uses. For instance, there may be an Algorithm that is used for encrypting data, and a function for posting code to a webpage.
Here is some further reference:
http://en.wikipedia.org/wiki/Algorithm
http://en.wikipedia.org/wiki/Function_(computer_science)

A function is a symbolic representation whereare a method is the mechanical steps needed to get the answer.
Suppose this function:
f(x) = x^ 2
Now if I tell you to count f(5000) you have to do things that this function doesn't say. Like for example how to multiply. So really these are just symbols.
But if I have a python method for example:
x = math.pow(500, 2) # or whatever it is
Then in this case the steps themselves for each operation are totally defined (in the libraries ;) ).

Related

what is the name of this language / algorithm?

this doesn't look like java/python/c/..etc
its from a book about algorithm/data structures
after a few searches i felt like this is called functional programming, but still
this ADD/ZERO/ISZERO/for_loop_syntax is different in it,
so, what is this thing?, what do i call this type of writings?
& where do i run it? [if its a programming language]
is it wrong, if i write ADD(x,y) as SUM(x,y)?
The code is a formal definition of a Polynomial as computer data structure.
Source is the Book Fundamentals of Data Structures by Ellis Horowitz and Sartaj Sahni. The language being used is called SPARKS.
Introduction to the defintion from the book (page 47; Chapter 2: Arrays):
The first step is to consider how to define polynomials as a
computer structure. For a mathematician a polynomial is a sum of terms where each term has the form ax^e; x is the variable, a is the coefficient and e is the exponent. However this is not an appropriate definition for our purposes. When defining a data object one must decide what functions will be
available, what their input is, what their output is and exactly what it is that they do. A complete specification of the data structure polynomial is now given.
Related question.

Continous action-state-space and tiling

After getting used to the Q-Learning algorithm in discrete action-state-space I would like to expand this now to continous spaces. To do this I read the chapter On-Policy Control with Approximation of Sutton´s introduction. Here, the usage of differentiable functions like a linear function or an ANN are recommended to solve the problem of continous action-state-space. Nevertheless Sutton then discribes the tiling method which maps the continous variables onto a discrete presentation. Is this always necessary?
Trying to understand this methods I tried to implement the example of the Hill Climbing Car in the book without the tiling method and a linear base function q. As my state space is 2 dimensional, and my action is one dimensional I used a three dimensional weight vector w in this equation:
When I now try to choose the action which will maximize the output, the obvious answer will be a=1, if w_2 > 0. Therefore, the weight will slowly converge to positive zero and the agent will not learn anything useful. As Sutton is able to solve the problem using the tiling I am wondering if my problem is caused by the absence of the tiling method or if I am doing anything else wrong.
So: Is the tiling always necessary?
Regarding your main question about tiling, the answer is no, not always it is necessary using tiling.
As you tried, it's a good idea to implement some easy example as the Hill Climbing Car in order to fully understand the concepts. Here, however, you are misundertanding something important. When the book talks about linear methods, it is refering to linear in the parameters, which means that you can extract a set of (non linear) features and combine them linearly. This kind of approximators can represent functions much more complex than a standard linear regression.
The parametrization you have proposed it's not able to represent a non-linear Q function. Taking into account that in the Hill Climbing problem you want to learn Q-functions of this style:
You will need something more powefull than . An easy solution for your problem could be to use a Radial Basis Function (RBF) network. In this case, you use a set of features (or BF, like for example Gaussians functions) to map your state space:
Additionally, if your action space is discrete and small, the easiest solution is to maintain an independent RBF network for each action. For selecting the action, simply compute the Q value for each action and select the one with higher value. In this way you avoid the (complex) optimization problem of selecting the best action in a continuous function.
You can find a more detailed explanation on the Busoniu et al. book Reinforcement Learning and Dynamic Programming Using Function Approximators, pages 49-51. It's available for free here.

ML algorithm Representation, Evaluation & Optimization

I was reading through a paper published by the University of Washington with tips about Machine Learning algorithms.
The way they break down an ML algorithm is into 3 parts. A representation, evaluation and optimization. I would like to understand how these three parts work together, so what is the process like throughout a typical machine learning algorithm.
I understand that my question is very abstract and each algorithm will be different, but if you know of a way to explain it abstractly please do. If not feel free to use a specific algorithm to explain.
Paper: http://homes.cs.washington.edu/~pedrod/papers/cacm12.pdf ~
See Table 1.
Sebastian Raschka has provided a very nice flowchart of the (supervised) machine learning process.
(I am familiar with text classification, so I will use this as an example)
In short :
Representation: Your data needs to be brought into a suitable algorithmic form. For Text classification you would for example extract features from your full text documents (input data) and bring them into a bag-of-words representation.
Application of the respective algorithm: Here your algorithm is executed. After training you have to do some kind of evaluation to measure your success (the criteria for evaluation is depending on the task). (The Evaluation is the final part of this step)
Optimization: The learning algorithms usually have one or more parameters to tune. Having reference pairs of parameter configuration / evaluation (from the previous step) you can now tweak the paramaters and evaluate the effect on your results.
(In the flowchart this is the edge, where we go back to the step "Learning algorithm training")

Algorithmic solution to Minesweeper

I am trying to make the minesweeper solver. As you know there are 2 ways to determine which fields in minefield are safe to open, or to determine which fields are mined and you need to flag it. First way to determine is trivial and we have something like this:
if (number of mines around X – current number of discovered mines around X) = number of unopened fields around X then
All unopened fields around X are mined
if (number of mines around X == current number of discovered mines around X) then
All unopened fields around X are NOT mined
But my question is: What about situation when we can't find any mined or safe field and we need to look at more than 1 field?
http://img541.imageshack.us/img541/4339/10299095.png
For example this situation. We can't determine anything using previous method. So i need a help with algorithm for these cases.
I have to use A* algorithm to make this. That is why i need all possible safe states for next step in algorithm. When i find all possible safe states i will add them to the current shortest path and depending on heuristic function i will sort list of paths and choose next field that needs to be opened.
Awesome problem, before you get too excited though, please read NP Completeness and Minesweeper, as well as the accompanying presentation which develops some good worst case examples and how a human might solve them. Nevertheless, in expectation we most likely won't hit a time barrier, if we use basic pruning and heuristics.
The question of generating the game is asked here: Minesweeper solving algorithm. There is a very cool post on algebraic methods. You can also give backtracking a try (i.e. take a guess and see if that invalidates things), similar to the case where local information is not enough for something like sudoku. See this great discussion about this technique.
As #tigger said this is not a problem that can be solved with a simple set of rules. Minesweeper is a good example where backtracking algorithms such as DPLL is useful. With something as simple as propositional logic, you can implement a very efficient solver for minesweeper. I am not sure if you are familiar with AI reasoning & logic inference - If not, you might want to have a look at the book "Artificial Intelligence - A Modern Approach" by Stuart Russel and Peter Norvig. For quick reference of DPLL and propositional logic, search "wumpus world propositional logic" on Google.

Kolmogorov complexity

I'll be more than thankful if anyone would be able to explain me how Kolmogorov complexity is related to randomness, and random inputs.
Another thing that I can't understand - we know that calculating Kolmogorov complexity of a given input X isn't decidable. Given that, how can it be a measure of randomness?
thanks
Kolmogorov random is a particular definition of the vague intuitive concept of 'random' - which other definition are you referring to when asking for relationship (for reference http://en.wikipedia.org/wiki/Random_number)?
I don't follow your thought pattern in asking why one would have to be able to determine in the general case which strings are Kolmogorov random in order for the concept to be well-defined. Could you elaborate on what is giving you trouble? If nothing else, allow me to point you to the halting problem - certainly the concept of a program halting is well-defined even though there can be no algorithm for determining, in the general case, whether a particular program exhibits the property.
You should think the other way round. If something is not random, it means it follows some law, and this law can give a simpler description of the information. Think of zip: instead of the file you give a procedure to generate the file that is usually more compact than the source file. This is possible because the source file contained some order: if the source file was a random sequence of characters no compression would have been possible.
If you are interested in this topic, I strongly recommend "Computability and Randomness" by Andre' Nies, Oxford Logic Guides n.51.

Resources