In contrast, aheuristic is an approach to solving problems without well-defined correct or optimal results.[2] For example, although social mediarecommender systems are commonly called "algorithms", they actually rely on heuristics as there is no truly "correct" recommendation.
As aneffective method, an algorithm can be expressed within a finite amount of space and time[3] and in a well-definedformal language[4] for calculating afunction.[5] Starting from an initial state and initial input (perhapsempty),[6] the instructions describe a computation that, whenexecuted, proceeds through a finite[7] number of well-defined successive states, eventually producing "output"[8] and terminating at a final ending state. The transition from one state to the next is not necessarilydeterministic; some algorithms, known asrandomized algorithms, incorporate random input.[9]
Around 825 AD, Persian scientist and polymathMuḥammad ibn Mūsā al-Khwārizmī wrotekitāb al-ḥisāb al-hindī ("Book of Indian computation") andkitab al-jam' wa'l-tafriq al-ḥisāb al-hindī ("Addition and subtraction in Indian arithmetic").[1] In the early 12th century, Latin translations of said al-Khwarizmi texts involving theHindu–Arabic numeral system andarithmetic appeared, for exampleLiber Alghoarismi de practica arismetrice, attributed toJohn of Seville, andLiber Algorismi de numero Indorum, attributed toAdelard of Bath.[10] Hereby,alghoarismi oralgorismi is theLatinization of Al-Khwarizmi's name; the text starts with the phraseDixit Algorismi, or "Thus spoke Al-Khwarizmi".[2] Around 1230, the English wordalgorism is attested and then byChaucer in 1391, English adopted the French term.[3][4][clarification needed] In the 15th century, under the influence of the Greek word ἀριθμός (arithmos, "number";cf. "arithmetic"), the Latin word was altered toalgorithmus.[citation needed]
For a detailed presentation of the various points of view on the definition of "algorithm", seeAlgorithm characterizations.
One informal definition is "a set of rules that precisely defines a sequence of operations",[11][need quotation to verify] which would include allcomputer programs (including programs that do not perform numeric calculations), and any prescribedbureaucratic procedure[12]orcook-bookrecipe.[13] In general, a program is an algorithm only if it stops eventually[14]—even thoughinfinite loops may sometimes prove desirable.Boolos, Jeffrey & 1974, 1999 define an algorithm to be an explicit set of instructions for determining an output, that can be followed by a computing machine or a human who could only carry out specific elementary operations on symbols.[15]
This sectionis missing information about 20th and 21st century development of computer algorithms. Please expand the section to include this information. Further details may exist on thetalk page.(October 2023)
The first cryptographic algorithm for deciphering encrypted code was developed byAl-Kindi, a 9th-century Arab mathematician, inA Manuscript On Deciphering Cryptographic Messages. He gave the first description ofcryptanalysis byfrequency analysis, the earliest codebreaking algorithm.[22]
Bolter credits the invention of the weight-driven clock as "the key invention [ofEurope in the Middle Ages]," specifically theverge escapement mechanism[26] producing the tick and tock of a mechanical clock. "The accurate automatic machine"[27] led immediately to "mechanicalautomata" in the 13th century and "computational machines"—thedifference andanalytical engines ofCharles Babbage andAda Lovelace in the mid-19th century.[28] Lovelace designed the first algorithm intended for processing on a computer, Babbage's analytical engine, which is the first device considered a realTuring-complete computer instead of just acalculator. Although the full implementation of Babbage's second device was not realized for decades after her lifetime, Lovelace has been called "history's first programmer".
Telephone-switching networks ofelectromechanical relays were invented in 1835. These led to the invention of the digital adding device byGeorge Stibitz in 1937. While working in Bell Laboratories, he observed the "burdensome" use of mechanical calculators with gears. "He went home one evening in 1937 intending to test his idea... When the tinkering was over, Stibitz had constructed a binary adding device".[30][31]
Algorithms can be expressed in many kinds of notation, includingnatural languages,pseudocode,flowcharts,drakon-charts,programming languages orcontrol tables (processed byinterpreters). Natural language expressions of algorithms tend to be verbose and ambiguous and are rarely used for complex or technical algorithms. Pseudocode, flowcharts, drakon-charts, and control tables are structured expressions of algorithms that avoid common ambiguities of natural language. Programming languages are primarily for expressing algorithms in a computer-executable form but are also used to define or document algorithms.
There are many possible representations andTuring machine programs can be expressed as a sequence of machine tables (seefinite-state machine,state-transition table, andcontrol table for more), as flowcharts and drakon-charts (seestate diagram for more), as a form of rudimentarymachine code orassembly code called "sets of quadruples", and more. Algorithm representations can also be classified into three accepted levels of Turing machine description: high-level description, implementation description, and formal description.[34] A high-level description describes the qualities of the algorithm itself, ignoring how it is implemented on the Turing machine.[34] An implementation description describes the general manner in which the machine moves its head and stores data to carry out the algorithm, but does not give exact states.[34] In the most detail, a formal description gives the exact state table and list of transitions of the Turing machine.[34]
The graphical aid called aflowchart offers a way to describe and document an algorithm (and a computer program corresponding to it). It has four primary symbols: arrows showing program flow, rectangles (SEQUENCE, GOTO), diamonds (IF-THEN-ELSE), and dots (OR-tie). Sub-structures can "nest" in rectangles, but only if a single exit occurs from the superstructure.
It is often important to know how much time, storage, or other cost an algorithm may require. Methods have been developed for the analysis of algorithms to obtain such quantitative answers (estimates); for example, an algorithm that adds up the elements of a list ofn numbers would have a time requirement of, usingbig O notation. The algorithm only needs to remember two values: the sum of all the elements so far, and its current position in the input list. If the space required to store the input numbers is not counted, it has a space requirement of, otherwise is required.
Different algorithms may complete the same task with a different set of instructions in less or more time, space, or 'effort' than others. For example, abinary search algorithm (with cost) outperforms a sequential search (cost ) when used fortable lookups on sorted lists or arrays.
Theanalysis, and study of algorithms is a discipline ofcomputer science. Algorithms are often studied abstractly, without referencing any specificprogramming language or implementation. Algorithm analysis resembles other mathematical disciplines as it focuses on the algorithm's properties, not implementation.Pseudocode is typical for analysis as it is a simple and general representation. Most algorithms are implemented on particular hardware/software platforms and theiralgorithmic efficiency is tested using real code. The efficiency of a particular algorithm may be insignificant for many "one-off" problems but it may be critical for algorithms designed for fast interactive, commercial, or long-life scientific usage. Scaling from small n to large n frequently exposes inefficient algorithms that are otherwise benign.
Empirical testing is useful for uncovering unexpected interactions that affect performance.Benchmarks may be used to compare before/after potential improvements to an algorithm after program optimization.Empirical tests cannot replace formal analysis, though, and are non-trivial to perform fairly.[35]
To illustrate the potential improvements possible even in well-established algorithms, a recent significant innovation, relating toFFT algorithms (used heavily in the field of image processing), can decrease processing time up to 1,000 times for applications like medical imaging.[36] In general, speed improvements depend on special properties of the problem, which are very common in practical applications.[37] Speedups of this magnitude enable computing devices that make extensive use of image processing (like digital cameras and medical equipment) to consume less power.
Algorithm design is a method or mathematical process for problem-solving and engineering algorithms. The design of algorithms is part of many solution theories, such asdivide-and-conquer ordynamic programming withinoperation research. Techniques for designing and implementing algorithm designs are also called algorithm design patterns,[38] with examples including the template method pattern and the decorator pattern. One of the most important aspects of algorithm design is resource (run-time, memory usage) efficiency; thebig O notation is used to describe e.g., an algorithm's run-time growth as the size of its input increases.[39]
Per theChurch–Turing thesis, any algorithm can be computed by anyTuring complete model. Turing completeness only requires four instruction types—conditional GOTO, unconditional GOTO, assignment, HALT. However, Kemeny and Kurtz observe that, while "undisciplined" use of unconditional GOTOs and conditional IF-THEN GOTOs can result in "spaghetti code", a programmer can write structured programs using only these instructions; on the other hand "it is also possible, and not too hard, to write badly structured programs in a structured language".[40] Tausworthe augments the threeBöhm-Jacopini canonical structures:[41] SEQUENCE, IF-THEN-ELSE, and WHILE-DO, with two more: DO-WHILE and CASE.[42] An additional benefit of a structured program is that it lends itself toproofs of correctness usingmathematical induction.[43]
By themselves, algorithms are not usually patentable. In the United States, a claim consisting solely of simple manipulations of abstract concepts, numbers, or signals does not constitute "processes" (USPTO 2006), so algorithms are not patentable (as inGottschalk v. Benson). However practical applications of algorithms are sometimes patentable. For example, inDiamond v. Diehr, the application of a simplefeedback algorithm to aid in the curing ofsynthetic rubber was deemed patentable. Thepatenting of software is controversial,[44] and there are criticized patents involving algorithms, especiallydata compression algorithms, such asUnisys'sLZW patent. Additionally, some cryptographic algorithms have export restrictions (seeexport of cryptography).
Arecursive algorithm invokes itself repeatedly until meeting a termination condition and is a commonfunctional programming method.Iterative algorithms use repetitions such asloops or data structures likestacks to solve problems. Problems may be suited for one implementation or the other. TheTower of Hanoi is a puzzle commonly solved using recursive implementation. Every recursive version has an equivalent (but possibly more or less complex) iterative version, and vice versa.
Serial, parallel or distributed
Algorithms are usually discussed with the assumption that computers execute one instruction of an algorithm at a time on serial computers. Serial algorithms are designed for these environments, unlikeparallel ordistributed algorithms. Parallel algorithms take advantage of computer architectures where multiple processors can work on a problem at the same time. Distributed algorithms use multiple machines connected via a computer network. Parallel and distributed algorithms divide the problem into subproblems and collect the results back together. Resource consumption in these algorithms is not only processor cycles on each processor but also the communication overhead between the processors. Some sorting algorithms can be parallelized efficiently, but their communication overhead is expensive. Iterative algorithms are generally parallelizable, but some problems have no parallel algorithms and are called inherently serial problems.
While many algorithms reach an exact solution,approximation algorithms seek an approximation that is close to the true solution. Such algorithms have practical value for many hard problems. For example, theKnapsack problem, where there is a set of items, and the goal is to pack the knapsack to get the maximum total value. Each item has some weight and some value. The total weight that can be carried is no more than some fixed number X. So, the solution must consider the weights of items as well as their value.[45]
Brute force is a problem-solving method of systematically trying every possible option until the optimal solution is found. This approach can be very time-consuming, testing every possible combination of variables. It is often used when other methods are unavailable or too complex. Brute force can solve a variety of problems, including finding the shortest path between two points and cracking passwords.
Divide and conquer
Adivide-and-conquer algorithm repeatedly reduces a problem to one or more smaller instances of itself (usuallyrecursively) until the instances are small enough to solve easily.Merge sorting is an example of divide and conquer, where an unordered list can be divided into segments containing one item and sorting of the entire list can be obtained by merging the segments. A simpler variant of divide and conquer is called adecrease-and-conquer algorithm, which solves one smaller instance of itself, and uses the solution to solve the bigger problem. Divide and conquer divides the problem into multiple subproblems and so the conquer stage is more complex than decrease and conquer algorithms.[citation needed] An example of a decrease and conquer algorithm is thebinary search algorithm.
Such algorithms make some choices randomly (or pseudo-randomly). They find approximate solutions when finding exact solutions may be impractical (see heuristic method below). For some problems, the fastest approximations must involve somerandomness.[46] Whether randomized algorithms withpolynomial time complexity can be the fastest algorithm for some problems is an open question known as theP versus NP problem. There are two large classes of such algorithms:
This technique transforms difficult problems into better-known problems solvable with (hopefully)asymptotically optimal algorithms. The goal is to find a reducing algorithm whosecomplexity is not dominated by the resulting reduced algorithms. For example, oneselection algorithm finds the median of an unsorted list by first sorting the list (the expensive portion), and then pulling out the middle element in the sorted list (the cheap portion). This technique is also known astransform and conquer.
Foroptimization problems there is a more specific classification of algorithms; an algorithm for such problems may fall into one or more of the general categories described above as well as into one of the following:
When searching for optimal solutions to a linear function bound by linear equality and inequality constraints, the constraints can be used directly to produce optimal solutions. There are algorithms that can solve any problem in this category, such as the popularsimplex algorithm.[47] Problems that can be solved with linear programming include themaximum flow problem for directed graphs. If a problem also requires that any of the unknowns beintegers, then it is classified ininteger programming. A linear programming algorithm can solve such a problem if it can be proved that all restrictions for integer values are superficial, i.e., the solutions satisfy these restrictions anyway. In the general case, a specialized algorithm or an algorithm that finds approximate solutions is used, depending on the difficulty of the problem.
When a problem shows optimal substructures—meaning the optimal solution can be constructed from optimal solutions to subproblems—andoverlapping subproblems, meaning the same subproblems are used to solve many different problem instances, a quicker approach calleddynamic programming avoids recomputing solutions. For example,Floyd–Warshall algorithm, the shortest path between a start and goal vertex in a weightedgraph can be found using the shortest path to the goal from all adjacent vertices. Dynamic programming andmemoization go together. Unlike divide and conquer, dynamic programming subproblems often overlap. The difference between dynamic programming and simple recursion is the caching or memoization of recursive calls. When subproblems are independent and do not repeat, memoization does not help; hence dynamic programming is not applicable to all complex problems. Using memoization dynamic programming reduces the complexity of many problems from exponential to polynomial.
The greedy method
Greedy algorithms, similarly to a dynamic programming, work by examining substructures, in this case not of the problem but of a given solution. Such algorithms start with some solution and improve it by making small modifications. For some problems, they always find the optimal solution but for others they may stop atlocal optima. The most popular use of greedy algorithms is finding minimal spanning trees of graphs without negative cycles.Huffman Tree,Kruskal,Prim,Sollin are greedy algorithms that can solve this optimization problem.
The heuristic method
Inoptimization problems,heuristic algorithms find solutions close to the optimal solution when finding the optimal solution is impractical. These algorithms get closer and closer to the optimal solution as they progress. In principle, if run for an infinite amount of time, they will find the optimal solution. They can ideally find a solution very close to the optimal solution in a relatively short time. These algorithms includelocal search,tabu search,simulated annealing, andgenetic algorithms. Some, like simulated annealing, are non-deterministic algorithms while others, like tabu search, are deterministic. When a bound on the error of the non-optimal solution is known, the algorithm is further categorized as anapproximation algorithm.
One of the simplest algorithms finds the largest number in a list of numbers of random order. Finding the solution requires looking at every number in the list. From this follows a simple algorithm, which can be described in plain English as:
High-level description:
If a set of numbers is empty, then there is no highest number.
Assume the first number in the set is the largest.
For each remaining number in the set: if this number is greater than the current largest, it becomes the new largest.
When there are no unchecked numbers left in the set, consider the current largest number to be the largest in the set.
(Quasi-)formal description:Written in prose but much closer to the high-level language of a computer program, the following is the more formal coding of the algorithm inpseudocode orpidgin code:
Algorithm LargestNumberInput: A list of numbersL.Output: The largest number in the listL.
^abDavid A. Grossman, Ophir Frieder,Information Retrieval: Algorithms and Heuristics, 2nd edition, 2004,ISBN1402030045
^ab"Any classical mathematical algorithm, for example, can be described in a finite number of English words" (Rogers 1987:2).
^abWell defined concerning the agent that executes the algorithm: "There is a computing agent, usually human, which can react to the instructions and carry out the computations" (Rogers 1987:2).
^"an algorithm is a procedure for computing afunction (concerning some chosen notation for integers) ... this limitation (to numerical functions) results in no loss of generality", (Rogers 1987:1).
^"An algorithm haszero or more inputs, i.e.,quantities which are given to it initially before the algorithm begins" (Knuth 1973:5).
^"A procedure which has all the characteristics of an algorithm except that it possibly lacks finiteness may be called a 'computational method'" (Knuth 1973:5).
^"An algorithm has one or more outputs, i.e., quantities which have a specified relation to the inputs" (Knuth 1973:5).
^Whether or not a process with random interior processes (not including the input) is an algorithm is debatable. Rogers opines that: "a computation is carried out in a discrete stepwise fashion, without the use of continuous methods or analog devices ... carried forward deterministically, without resort to random methods or devices, e.g., dice" (Rogers 1987:2).
^Blair, Ann, Duguid, Paul, Goeing, Anja-Silvia and Grafton, Anthony. Information: A Historical Companion, Princeton: Princeton University Press, 2021. p. 247
^Dietrich, Eric (1999). "Algorithm". In Wilson, Robert Andrew; Keil, Frank C. (eds.).The MIT Encyclopedia of the Cognitive Sciences. MIT Cognet library. Cambridge, Massachusetts: MIT Press (published 2001). p. 11.ISBN9780262731447. RetrievedJuly 22, 2020.An algorithm is a recipe, method, or technique for doing something.
^Stone requires that "it must terminate in a finite number of steps" (Stone 1973:7–8).
^Ast, Courtney."Eratosthenes". Wichita State University: Department of Mathematics and Statistics.Archived from the original on February 27, 2015. RetrievedFebruary 27, 2015.
^For instance, thevolume of aconvex polytope (described using a membership oracle) can be approximated to high accuracy by a randomized polynomial time algorithm, but not by a deterministic one: seeDyer, Martin; Frieze, Alan; Kannan, Ravi (January 1991). "A Random Polynomial-time Algorithm for Approximating the Volume of Convex Bodies".J. ACM.38 (1):1–17.CiteSeerX10.1.1.145.4600.doi:10.1145/102782.102783.S2CID13268711.
^George B. Dantzig and Mukund N. Thapa. 2003.Linear Programming 2: Theory and Extensions. Springer-Verlag.
Bolter, David J. (1984).Turing's Man: Western Culture in the Computer Age (1984 ed.). Chapel Hill, NC: The University of North Carolina Press.ISBN978-0-8078-1564-9.,ISBN0-8078-4108-0
Campagnolo, M.L.,Moore, C., and Costa, J.F. (2000) An analog characterization of the subrecursive functions. InProc. of the 4th Conference on Real Numbers and Computers, Odense University, pp. 91–109
Church, Alonzo (1936). "An Unsolvable Problem of Elementary Number Theory".American Journal of Mathematics.58 (2):345–363.doi:10.2307/2371045.JSTOR2371045. Reprinted inThe Undecidable, p. 89ff. The first expression of "Church's Thesis". See in particular page 100 (The Undecidable) where he defines the notion of "effective calculability" in terms of "an algorithm", and he uses the word "terminates", etc.
Church, Alonzo (1936). "A Note on the Entscheidungsproblem".The Journal of Symbolic Logic.1 (1):40–41.doi:10.2307/2269326.JSTOR2269326.S2CID42323521.Church, Alonzo (1936). "Correction to a Note on the Entscheidungsproblem".The Journal of Symbolic Logic.1 (3):101–102.doi:10.2307/2269030.JSTOR2269030.S2CID5557237. Reprinted inThe Undecidable, p. 110ff. Church shows that the Entscheidungsproblem is unsolvable in about 3 pages of text and 3 pages of footnotes.
Daffa', Ali Abdullah al- (1977).The Muslim contribution to mathematics. London: Croom Helm.ISBN978-0-85664-464-1.
Kleene, Stephen C. (1936)."General Recursive Functions of Natural Numbers".Mathematische Annalen.112 (5):727–742.doi:10.1007/BF01565439.S2CID120517999. Archived fromthe original on September 3, 2014. RetrievedSeptember 30, 2013. Presented to the American Mathematical Society, September 1935. Reprinted inThe Undecidable, p. 237ff. Kleene's definition of "general recursion" (known now as mu-recursion) was used by Church in his 1935 paperAn Unsolvable Problem of Elementary Number Theory that proved the "decision problem" to be "undecidable" (i.e., a negative result).
Kleene, Stephen C. (1943)."Recursive Predicates and Quantifiers".Transactions of the American Mathematical Society.53 (1):41–73.doi:10.2307/1990131.JSTOR1990131. Reprinted inThe Undecidable, p. 255ff. Kleene refined his definition of "general recursion" and proceeded in his chapter "12. Algorithmic theories" to posit "Thesis I" (p. 274); he would later repeat this thesis (in Kleene 1952:300) and name it "Church's Thesis"(Kleene 1952:317) (i.e., theChurch thesis).
A.A. Markov (1954)Theory of algorithms. [Translated by Jacques J. Schorr-Kon and PST staff] Imprint Moscow, Academy of Sciences of the USSR, 1954 [i.e., Jerusalem, Israel Program for Scientific Translations, 1961; available from the Office of Technical Services, U.S. Dept. of Commerce, Washington] Description 444 p. 28 cm. Added t.p. in Russian Translation of Works of the Mathematical Institute, Academy of Sciences of the USSR, v. 42. Original title: Teoriya algerifmov. [QA248.M2943 Dartmouth College library. U.S. Dept. of Commerce, Office of Technical Services, number OTS 60-51085.]
Post, Emil (1936). "Finite Combinatory Processes, Formulation I".The Journal of Symbolic Logic.1 (3):103–105.doi:10.2307/2269031.JSTOR2269031.S2CID40284503. Reprinted inThe Undecidable, pp. 289ff. Post defines a simple algorithmic-like process of a man writing marks or erasing marks and going from box to box and eventually halting, as he follows a list of simple instructions. This is cited by Kleene as one source of his "Thesis I", the so-calledChurch–Turing thesis.
Rogers, Hartley Jr. (1987).Theory of Recursive Functions and Effective Computability. The MIT Press.ISBN978-0-262-68052-3.
Rosser, J.B. (1939). "An Informal Exposition of Proofs of Godel's Theorem and Church's Theorem".Journal of Symbolic Logic.4 (2):53–60.doi:10.2307/2269059.JSTOR2269059.S2CID39499392. Reprinted inThe Undecidable, p. 223ff. Herein is Rosser's famous definition of "effective method": "...a method each step of which is precisely predetermined and which is certain to produce the answer in a finite number of steps... a machine which will then solve any problem of the set with no human intervention beyond inserting the question and (later) reading the answer" (p. 225–226,The Undecidable)
Stone, Harold S. (1972).Introduction to Computer Organization and Data Structures (1972 ed.). McGraw-Hill, New York.ISBN978-0-07-061726-1. Cf. in particular the first chapter titled:Algorithms, Turing Machines, and Programs. His succinct informal definition: "...any sequence of instructions that can be obeyed by a robot, is called analgorithm" (p. 4).
Tausworthe, Robert C (1977).Standardized Development of Computer Software Part 1 Methods. Englewood Cliffs NJ: Prentice–Hall, Inc.ISBN978-0-13-842195-3.
Turing, Alan M. (1939). "Systems of Logic Based on Ordinals".Proceedings of the London Mathematical Society.45:161–228.doi:10.1112/plms/s2-45.1.161.hdl:21.11116/0000-0001-91CE-3. Reprinted inThe Undecidable, pp. 155ff. Turing's paper that defined "the oracle" was his PhD thesis while at Princeton.
Zaslavsky, C. (1970). Mathematics of the Yoruba People and of Their Neighbors in Southern Nigeria. The Two-Year College Mathematics Journal, 1(2), 76–99.https://doi.org/10.2307/3027363
Chabert, Jean-Luc (1999).A History of Algorithms: From the Pebble to the Microchip. Springer Verlag.ISBN978-3-540-63369-3.
Thomas H. Cormen; Charles E. Leiserson; Ronald L. Rivest; Clifford Stein (2009).Introduction To Algorithms (3rd ed.). MIT Press.ISBN978-0-262-03384-8.
Harel, David; Feldman, Yishai (2004).Algorithmics: The Spirit of Computing. Addison-Wesley.ISBN978-0-321-11784-7.
Hertzke, Allen D.; McRorie, Chris (1998). "The Concept of Moral Ecology". In Lawler, Peter Augustine; McConkey, Dale (eds.).Community and Political Thought Today. Westport, CT:Praeger.
Jon Kleinberg, Éva Tardos(2006):Algorithm Design, Pearson/Addison-Wesley, ISBN 978-0-32129535-4