算法的演变 —— 起源和历史

注:机翻,未校。


Evolution of Algorithm – Origin and History

Evolution of algorithms has taken a long time. It is an ancient notion. We utilize algorithms all the time, whether we realize it or not. You may think of them as an inseparable element of each activity we take in our daily lives. Algorithms are not only fascinating to study, but they are also critical to the functioning of our modern, digital world.
算法的演进花了很长时间。这是一个古老的概念。我们一直在使用算法,无论我们是否意识到这一点。您可能会将它们视为我们在日常生活中进行的每项活动中不可分割的元素。算法不仅引人入胜,而且对于我们现代数字世界的运作也至关重要。

Algorithms, as you may be aware, are frequently employed in mathematics and computer science. However, you may be shocked to find that you yourself utilize them on a daily basis. An algorithm is a recipe for any food dish. Planning a tour and fixing routes is also a potential algorithm. Planning family functions is also an algorithmic process.
您可能知道,算法经常用于数学和计算机科学中。但是,您可能会惊讶地发现您自己每天都在使用它们。算法是任何菜肴的食谱。规划旅行和固定路线也是一种潜在的算法。规划家庭功能也是一个算法过程。

Algorithms are utilized in current applications such as mathematical determinations, data processing, and automated reasoning, to mention a few. Simply said, without them, the modern world would function quite differently.
算法用于当前应用,例如数学确定、数据处理和自动推理,仅举几例。简单地说,没有它们,现代世界的运作方式将大不相同。

In this article, we will look at the interesting and extensive history of algorithms in a nutshell. We’ll look at the history of algorithms, including their etymology (origin of the word), and then look at some of the most significant ones. We’ll also take you on a brief tour of some of their contemporary applications.
在本文中,我们将简要介绍有趣而广泛的算法历史。我们将了解算法的历史,包括它们的词源(单词的起源),然后看看一些最重要的算法。我们还将带您简要介绍他们的一些当代应用。

Etymology of the Word “Algorithm” “算法” 一词的词源

We will discuss the evolution of algorithms from etymology to evolution to recent trends in algorithms.
我们将讨论算法的演变,从词源到进化再到算法的最新趋势。

There is an interesting history behind the name “Algorithm“. Muhammad Ibn Musa al-Khwarizmi, a 9th-century scholar, astronomer, geographer, and mathematician, became well-known for his contributions to the study of algebra.
“算法” 这个名字背后有一段有趣的历史。穆罕默德・伊本・穆萨・花剌子模 (Muhammad Ibn Musa al-Khwarizmi) 是 9 世纪的学者、天文学家、地理学家和数学家,因其对代数研究的贡献而闻名。

Al-Khwarizmi authored a book about Hindu-Arabic numbers in Arabic, which was eventually translated into Latin. Later, this book was translated in Latin with the title “Algoritmi de numero Indorum”. After then, the Latin word ‘algoritmi’ was translated into English as ‘algorithm’.
花剌子模用阿拉伯语写了一本关于印度教 - 阿拉伯数字的书,最终被翻译成拉丁文。后来,这本书被翻译成拉丁文,标题为 “Algoritmi de numero Indorum”。此后,拉丁语 “algoritmi” 被翻译成英语为 “算法”。

Some refer to Al-Khwarizmi as “the Father of Algebra,” and he is largely regarded as the scholar who introduced advanced mathematics to the West following the fall of Rome.
有人称花剌子模为 “代数之父”,他被广泛认为是罗马沦陷后将高等数学引入西方的学者。

Surprisingly, Al-Khwarizmi appears to have built on the work of an older Indian polymath known as Brahmagupta.
令人惊讶的是,花剌子模似乎是建立在一位被称为布拉马笈多的印度老博学者的工作之上的。

The term “algorithm” is now used to refer to any technique of computing. This is especially true since the mid-twentieth century when it was frequently employed with respect to computing.
术语 “算法” 现在用于指代任何计算技术。自 20 世纪中叶以来尤其如此,当时它经常被用于计算。

The term “algorithm” was coined by the Persian mathematician al-Khwarizmi in the ninth century.
“算法” 一词是由波斯数学家花剌子模在九世纪创造的。

Muhammad al-Khwarizm - Evolution of Algorithm

Muhammad al-Khwarizmi

Image Source: https://www.bbc.co.uk/ideas/videos/why-algorithms-are-called-algorithms/p07gdlwf

Evolution of Algorithm 算法的演进

According to historical documents and archaeological evidence, the Babylonians may have constructed the first identifiable algorithm about 1600 BC. These algorithms, which were recorded on clay tablets in a form of cuniform, were used for factorization and determining square roots, among other things, and were quite simple by today’s standards. This could be stated as the first step in the evolution of the algorithm.
根据历史文献和考古证据,巴比伦人可能在公元前 1600 年左右构建了第一个可识别的算法。这些算法以 cuniform 的形式记录在粘土板上,用于因式分解和确定平方根等,按照今天的标准来说非常简单。这可以说是算法发展的第一步。

In approximately 300 BC, the great Euclid created his renowned “Euclidean algorithm,” which was followed by Eratosthenes in 200 BC with his so-called “Sieve of Eratosthenes.” Lui Hui described Gaussian elimination in 263 AD, while Brahmagupta created Chakravala in 628 AD.
大约在公元前 300 年,伟大的欧几里得创造了他著名的 “欧几里得算法”,随后埃拉托色尼在公元前 200 年推出了他所谓的 “埃拉托色尼筛子”。吕辉在公元 263 年描述了高斯消除,而婆罗门笈多在公元 628 年创造了脉轮。

The contemporary algorithm saw significant development throughout the mid-to-late industrial revolution. During this period, George Boole famously created binary algebra, which served as the foundation for contemporary computer coding.
当代算法在整个工业革命中后期都取得了重大发展。在此期间,乔治・布尔(George Boole)创造了著名的二进制代数,为当代计算机编码奠定了基础。

With his iconic Turing machine, Alan Turing formalized the idea of the algorithm for the first time in 1936. The world of computation has been totally changed since then.
1936 年,艾伦・图灵 (Alan Turing) 凭借其标志性的图灵机首次正式确定了该算法的想法。从那时起,计算世界已经发生了翻天覆地的变化。

The first-ever algorithm was proposed by an unknown Indian mathematician who used the concept of zero and decimal position of numbers. This algorithm provided a basis for many basic arithmetic operations, such as square roots, and cube roots.
有史以来第一个算法是由一位不知名的印度数学家提出的,他使用了零和数字小数位置的概念。该算法为许多基本算术运算(例如平方根和立方根)提供了基础。

During the 1940s and 1950s, research was oriented toward building efficient computer systems, so that they could be used in scientific, commercial, and engineering problems.
在 1940 年代和 1950 年代,研究面向构建高效的计算机系统,以便它们可以用于科学、商业和工程问题。

Structural programming came into existence after Alan Turing introduced the idea of Effective Procedure in 1936.
结构编程是在艾伦・图灵(Alan Turing)于 1936 年提出有效程序(Effective Procedure)思想之后出现的。

People realize that effective programming is as important as hardware resources.
人们意识到,有效的编程与硬件资源一样重要。

The application of computers in diverse fields has led to the search for more efficient algorithms and the study of tractable and non-tractable problems.
计算机在不同领域的应用导致了对更有效算法的探索以及对可处理和不可处理问题的研究。

Turing’s algorithm logic also laid the basis for variable-based operations. The calculation of the preceding symbols determines the value of the following symbol. Progressive algorithms are born as a result of this. “We may now design a machine to do the task of this computer,” Turing stated in his hypothesis.
图灵的算法逻辑也为基于变量的运算奠定了基础。前述符号的计算决定了后一个符号的值。渐进式算法由此而诞生。“我们现在可以设计一台机器来完成这台计算机的任务,” 图灵在他的假设中说。

Donald Knuth – Father of Algorithms 唐纳德・克努斯(Donald Knuth)—— 算法之父

Donald Ervin Knuth is an American author. is an American computer scientist, and mathematician, and was a professor at Stanford University. He received the ACM Turing Award in 1974, which is regarded as the “Nobel Prize” in computer science. Knuth has been named the “Father of Algorithm Analysis.”
唐纳德・欧文・克努斯(Donald Ervin Knuth)是美国作家。是美国计算机科学家和数学家,曾是斯坦福大学的教授。他于 1974 年获得 ACM 图灵奖,该奖被誉为计算机科学界的 “诺贝尔奖”。Knuth 被誉为 “算法分析之父”。

father of algorithm analysis - Evolution of Algorithm

Donald Knuth – Father of Algorithms

Image Source: https://www.frontiersofknowledgeawards-fbbva.es/galardonados/donald-e-knuth-2/

Knuth authored the seminal “The Art of Computer Programming” as an associate professor at Caltech, a seven-volume collection that rapidly became a go-to book for anybody interested in the how’s and why’s of computer programming.
作为加州理工学院的副教授,Knuth 撰写了开创性的《计算机编程的艺术》,这是一本七卷本的合集,迅速成为任何对计算机编程的方式和原因感兴趣的人的必读书。

Analysis of Algorithm 算法分析

The branch of the study of tractable and non-tractable problems is known as complexity theory. Algorithms are being analyzed on various bounds. The focus of researchers was on minimizing the lower bound running time of algorithms.
可处理和不可处理问题的研究分支被称为复杂性理论。正在对各种边界的算法进行分析。研究人员的重点是最小化算法的运行下限时间。

Later, they developed formal methods of specification, which describe the precise nature of input to verify the correctness of the algorithm.
后来,他们开发了正式的规范方法,描述了输入的精确性质,以验证算法的正确性。

Typically, algorithms are designed to solve specific problems only, so they are bound to operate on specific inputs only. No algorithm works on every input. So, with the development of algorithms, it has been essential to specify the limits of input and expected output of the algorithm.
通常,算法仅为解决特定问题而设计,因此它们只能对特定输入进行操作。没有算法对每个输入都有效。因此,随着算法的发展,指定算法的输入限制和预期输出至关重要。

The study and development of efficient algorithms led to another aspect called the time-space trade-off. This opens up the study of problems that are both tractable in time and tractable in space. Tractable problems are those which can be solved in a reasonably acceptable time.
对高效算法的研究和开发导致了另一个方面,即时间空间权衡。这开启了对既在时间上易于处理又在空间上易于处理的问题的研究。可处理的问题是那些可以在合理接受的时间内解决的问题。

Approximation algorithms came into the picture to provide an approximate solution to non-tractable problems in a reasonably acceptable time.
近似算法的出现是为了在合理可接受的时间内为难以处理的问题提供近似解决方案。

Randomized algorithms have given a new direction for solving many time-consuming problems. Randomness can be thought of as an integral part of many algorithmic strategies, like simulated annealing and genetic algorithms.
随机算法为解决许多耗时的问题提供了新的方向。随机性可以被认为是许多算法策略的一个组成部分,如模拟退火和遗传算法。

Quicksort is sufficient to explain the importance of randomness. Quicksort runs in quadratic time for sorted data, but with randomized pivot selection, it can be reduced to n.log2n.
Quicksort 足以解释随机性的重要性。对于排序的数据,Quicksort 以二次时间运行,但使用随机透视选择时,可以将其简化为 n.log2n。

Algorithms in Daily Routine 日常生活中的算法

Even if computers did not exist, it is probable that algorithms of some type would still play a significant part in your daily life.
即使计算机不存在,某种类型的算法也可能仍然会在您的日常生活中发挥重要作用。

We utilize algorithms in our everyday routine, whether we realize it or not. Any task, from simple to complicated, may be seen as a potential algorithm.
我们在日常生活中使用算法,无论我们是否意识到这一点。任何任务,从简单到复杂,都可以被视为一种潜在的算法。

Having stated that, below are some examples of algorithms in use in everyday life.
话虽如此,以下是日常生活中使用的一些算法示例。

Planning a Tour 计划旅行

Before you start on your journey, you will undoubtedly consider a lot of factors, including location, the best time to visit the location, method of transportation, nearby tourist attractions, accommodation, and so on. This action sequence is referred to as an algorithm.
在您开始您的旅程之前,您无疑会考虑很多因素,包括位置、访问地点的最佳时间、交通方式、附近的旅游景点、住宿等。此操作序列称为算法。

Solving an Assignment 解决作业

When a student needs to solve an assignment, he must follow a specific sequence of steps. For example, if he receives an assignment from his professor, he must find the right book or resources to answer the question. Students may use Google or ask their friends for assistance before completing the task. As a result, the entire procedure is an algorithm.
当学生需要解决一项作业时,他必须遵循特定的步骤序列。例如,如果他从教授那里接到作业,他必须找到合适的书籍或资源来回答这个问题。学生可以在完成任务之前使用 Google 或向他们的朋友寻求帮助。因此,整个过程就是一个算法。

Organizing functions 组织功能

It’s your birthday next week, and you’ve decided to treat your pals. What are your plans? You consider the ideal venue, you will confer with someone about the theme of the party, the menu of the party, which cake should be excellent, the order of events, timings, and so on. Everything is meticulously planned by you. You go through your plans several times to ensure that they are as effective as possible. This process of ensuring desired output is nothing but an algorithm
下周是你的生日,你决定犒劳你的朋友。你有什么计划?你考虑理想的场地,你会与某人讨论派对的主题、派对的菜单、哪种蛋糕应该很棒、活动的顺序、时间等等。一切都由您精心策划。你要对你的计划进行多次检查,以确保它们尽可能有效。这个确保所需输出的过程只不过是一种算法

Basic Arithmetic operation 基本算术运算

You get milk bottles from the milkman every day, and you have to pay him the whole sum at the end of the month. So you’ll take the number of milk bottles you purchased over the past month and multiply it by the cost of each individual bottle. This will give you the entire amount you spent on milk over the course of a month.
你每天都从送奶工那里得到奶瓶,你必须在月底付给他全部钱。因此,您需要将过去一个月购买的奶瓶数量乘以每瓶奶瓶的成本。这将为您提供一个月内在牛奶上花费的全部金额。

When you go to a party with friends, and after dinner, when you compute the portion of each member from the overall cost, that method also follows the algorithm.
当你和朋友一起去参加聚会时,晚餐后,当你从总成本中计算每个成员的部分时,这种方法也遵循算法。

Thus, the evolution of algorithms has taken a shape from cuniform to digital computers.
因此,算法的演变已经从普通计算机发展到数字计算机。

Short Questions on Evolution of Algorithms 关于算法演进的简短问题

Q: Who is the pioneer of the study of Algorithms? 谁是算法研究的先驱?

Muhammad Ibn Musa al-Khwarizmi, a 9th-century scholar, astronomer, geographer, and mathematician, became well-known for his contributions to the study of algebra.
穆罕默德・伊本・穆萨・花剌子模 (Muhammad Ibn Musa al-Khwarizmi) 是 9 世纪的学者、天文学家、地理学家和数学家,因其对代数研究的贡献而闻名。

Q: Who is known as the father of linear algebra? 谁被称为线性代数之父?

Muhammad Ibn Musa al-Khwarizmi is known as the father of linear algebra
穆罕默德・伊本・穆萨・花剌子模被称为线性代数之父

Q: Who is known as the father of algorithms? 谁被称为算法之父?

Donald Ervin Knuth is an American author an American computer scientist, a mathematician, and a professor at Stanford University professor is known as the father of algorithms.
唐纳德・欧文・克努斯(Donald Ervin Knuth)是美国作家,美国计算机科学家,数学家,斯坦福大学教授,被称为算法之父。

Q: Name the famous book of Donald Knuth 唐纳德・克努斯(Donald Knuth)的著名著作

“The Art of Computer Programming” is a well-known book written by Donald Knuth.
《计算机编程的艺术》是唐纳德・克努斯(Donald Knuth)写的著名著作。

Q: Enlist a few daily routine activities that can be described as an algorithmic process 列出一些可以被描述为一个算法过程的日常活动

Any routine work takes predefined steps, we can consider them all as an algorithmic processes such as,
任何常规工作都需要预定义的步骤,我们可以将它们全部视为一个算法过程,例如,

  • Making a tea/coffee 泡茶 / 咖啡
  • Prepare food 准备食物
  • Be ready to go to the office 准备好去办公室
  • Clearing the examinations 通过考试
  • Go on tour with friends 和朋友一起去旅游
  • Attend/arrange functions 出席 / 安排活动

What is Algorithm? – Nutshell Explanation 算法简释

What is Algorithm? 什么是算法?

The algorithm is a set of rules defined in specific order to do certain computation and carry out some predefined task. It is a step by step procedure to solve the problem.
该算法是按特定顺序定义的一组规则,用于执行某些计算并执行某些预定义的任务。这是一个逐步解决问题的过程。

Evolution of algorithm has gone through lone way. The term Algorithm was coined by the Persian mathematician Al-Khwarizmi in the ninth century.
算法的演进经历了孤独的道路。算法一词是由波斯数学家花剌子模在九世纪创造的。

Given the algorithm, it is easy to program the solution. It bridges the gap between natural language description of the solution and syntax of programming language. It is a set of rules which are used to solve real-life problems. It provides a loose form of a solution in a pseudo-programming language. We can treat it as a set of finite instructions which solves a particular problem when applied it is applied to that problem with legal inputs.
给定算法,很容易对解决方案进行编程。它弥合了解决方案的自然语言描述和编程语言的语法之间的差距。它是一套用于解决现实生活中问题的规则。它提供了一种伪编程语言中解决方案的松散形式。我们可以将其视为一组有限指令,当应用时可以解决特定问题,然后通过法律输入将其应用于该问题。

The first-ever algorithm was developed by Babylonians for factorization of a number and to find roots of the equation. Euclid had proposed a famous algorithm for finding Greatest Common Divisor (GCD) of two numbers.
有史以来第一个算法是由巴比伦人开发的,用于对数字进行因式分解并找到方程的根。欧几里得提出了一种著名的算法,用于求两个数的最大公约数(GCD)。

Initially, the solution to the problem is thought as a natural language description, whose syntax is too far from the programming language. Before the actual problem solved in a programming language, natural language description of the solution is first represented as an algorithm. It is then converted to programming syntax. The process of solving a problem is depicted in following figure
最初,该问题的解决方案被认为是自然语言描述,其语法与编程语言相距太远。在用编程语言解决实际问题之前,首先将解决方案的自然语言描述表示为算法。然后,它被转换为编程语法。解决问题的过程如下图所示

problem solving using algorithm

Problem Solving using Algorithm

If the algorithm is correct, then the program should produce correct output on valid input, otherwise, it should produce an appropriate error message. For example, to find the division A/B, correctly written program would return value of A/B for B > 0, and it would show the error message like “Invalid divisor” for B = 0.
如果算法是正确的,那么程序应该在有效的输入上产生正确的输出,否则,它应该产生适当的错误消息。例如,要找到除法 A/B,正确编写的程序将返回 B > 0 的 A/B 值,并且当 B = 0 时,它会显示类似 “无效除数” 的错误消息。

Properties of Algorithm 算法的属性

A good algorithm should have the following properties/characteristics:
一个好的算法应该具有以下属性 / 特征:

Properties of Algorithm

Properties of Algorithm

Input: The algorithm may take zero or more input arguments. Depending on the problem, the input may be a scalar, vector, array, tree, graph or some other data structures
输入:算法可以接受零个或多个输入参数。根据问题的不同,输入可以是标量、向量、数组、树、图形或其他一些数据结构

Output: The algorithm reads input, processes it and produces at least one output. Depending on the problem being solved, the output may of the form scalar, vector, array, tree, graph or some other data structures.
输出:算法读取输入,对其进行处理并产生至少一个输出。根据要解决的问题,输出的形式可能是标量、向量、数组、树、图形或其他一些数据结构。

Definiteness: All instructions should be unambiguous and simple to interpret. There should not be multiple ways to interpret the same instruction. Each instruction should be precise and concise
明确性:所有说明都应该明确无误且易于解释。不应有多种方式来解释同一指令。每条指令都应准确简洁

Finiteness: Every algorithm must terminate after a finite number of steps. If the algorithm contains a loop, the upper bound of the loop must be finite. Recursive calls should have a well-defined base case for the termination
有限性:每个算法都必须在有限数量的步骤后终止。如果算法包含循环,则循环的上限必须是有限的。递归调用应具有明确定义的终止基本情况

Effectiveness: The algorithm should be written with a basic set of instructions. The operations should be basic enough to perform exactly using a basic set, just like one can perform them with pen and paper. Complex operations should be performed using a combination of basic instruction. For example, multiplication should be performed using loop and addition, sorting should be carried out using looping, comparison, swapping etc.
有效性:算法应该用一套基本的指令来编写。这些操作应该足够基本,以便完全使用基本集来执行,就像人们可以用笔和纸执行它们一样。复杂的操作应结合使用基本指令来执行。例如,乘法应使用循环和加法进行,排序应使用循环、比较、交换等进行。

Types of Algorithmic Complexities 算法复杂性的类型

Algorithms are analyzed using two types of complexities
使用两种类型的复杂性来分析算法

Types of algorithmic complexity

Types of algorithmic complexity

Time complexity: It is the amount of time taken by the algorithm to solve the given problem.
时间复杂度:它是算法解决给定问题所花费的时间。

Space complexity: It is the amount of memory required by the algorithm to solve the given problem.
空间复杂度:它是算法解决给定问题所需的内存量。

Problems Solved by Algorithms 算法解决的问题

Algorithms are not developed just to solve problems like sorting, factorial, GCD etc. The scope of the algorithm is much wider than that. Few of the real-life problems are listed here for which the design of an efficient algorithm matters a lot
算法的开发不仅仅是为了解决排序、阶乘、GCD 等问题。该算法的范围远不止于此。这里列出了一些现实生活中的问题,对于这些问题来说,高效算法的设计非常重要

Such problems may not be solved effectively using traditional approaches and even if they are solvable, the answer may not be optimal or it may take a too long time. Various class of algorithms are :
使用传统方法可能无法有效解决此类问题,即使它们可以解决,答案也可能不是最优的,或者可能需要很长时间。各种类型的算法是:

  • String matching algorithms: Can be useful in text search, editors, compilers etc.
    字符串匹配算法:可用于文本搜索、编辑器、编译器等。
  • Number theory and numerical algorithms: Useful in the statistical analysis of data.
    数论和数值算法:在数据的统计分析中很有用。
  • Divide and conquer: Used to solve sorting, convex hull, min-max problem.
    分而治之:用于解决排序、凸壳、最小 - 最大问题。
  • Linear programming: Can be useful in solving many optimization problems.
    线性规划:可用于解决许多优化问题。
  • Dynamic programming: Solves the optimization problems like subsequence, super sequence, assembly line scheduling, cruise scheduling etc.
    动态规划:解决子序列、超级序列、流水线调度、巡航调度等优化问题。
  • Greedy methods: Provides a suboptimal solution to the problems like make a change, minimum spanning tree, Huffman code, job sequencing etc.
    贪婪方法:为诸如更改、最小生成树、霍夫曼代码、作业排序等问题提供次优解决方案。
  • Sorting: Useful to sort numerical or string data.
    排序:用于对数值或字符串数据进行排序。

However, the list is far more exhaustive, but we listed here a few of the most common problems appear in real life.
然而,这份清单要详尽得多,但我们在这里列出了现实生活中出现的一些最常见的问题。

How to Write Algorithm? 如何编写算法?

The algorithm consists of two parts:
该算法由两部分组成:

Head and body. Head part consists of name, description of the problem being solved, input to the problem and expected output. It may also include the explanation of the input argument and output variable. The description provides a clear idea to the user about the functionality of the algorithm.
头部和身体。头部部分由名称、正在解决的问题的描述、问题的输入和预期的输出组成。它还可能包括对输入参数和输出变量的解释。该描述为用户提供了有关算法功能的清晰概念。

Body part includes a logical sequence of statements involving various constructs like conditional statements, expressions, loops, breaks, function calls etc.
正文部分包括涉及各种结构的语句的逻辑序列,如条件语句、表达式、循环、中断、函数调用等。

Structure of Algorithm

Structure of Algorithm

An algorithm is a lucid form of program and it does not have rigid restrictions on the syntax. One can write it using his own terminology and syntax
算法是程序的一种清晰形式,它对语法没有严格的限制。一个人可以使用他自己的术语和语法来编写它

However, some of the common rules followed for writing algorithms, which are stated below :
但是,编写算法遵循的一些常见规则如下所述:

  • The algorithm should start with the keyword Algorithm, followed by its name, followed by a list of arguments to be passed.
    该算法应以关键字 Algorithm 开头,后跟其名称,后跟要传递的参数列表。
Algorithm FIND_SUM (A, B)
  • Comments start with //sign. In the very next line, we should specify the description and input-output.
    注释以 // 符号开头。在下一行中,我们应该指定描述和输入输出。
// Problem Description : Add two integer numbers.
// Input: Two numbers A and B on which sum is to be performed.
// Output: Sum of given two numbers.
  • Next comes the body part, which contains various logical statements in the proper sequence. These statements may contain control statements, loops, expressions etc.
    接下来是正文部分,它以正确的顺序包含各种逻辑陈述。这些语句可能包含控制语句、循环、表达式等。
  • Compound statements are enclosed within the opening and closing curly brace i.e. { … }
    复合语句括在左大括号和右大括号内,即 { … }
  • Use the left arrow for assignment
    使用向左箭头进行分配
C ← A + B
  • Array index usually starts with index 0 or 1.
    数组索引通常以索引 0 或 1 开头。
  • Relational operations are performed using relational operators like <, >, ==, !=, ?= and <=
    关系运算是使用关系运算符(如 <、>、==、!=、?= 和 <=
  • Logical operations are performed using logical operators like and or and not
    逻辑运算是使用逻辑运算符(如 AND 或 NOT )执行的
  • Input and output are performed using read and write a statement.
    输入和输出是使用 read 和 write 语句执行的。
read (A) /read “A”
write (A) /write “A” 
print (A) /print “A”
  • Control statements are written as follows :
    控制语句编写如下:
if (condition) then 
  Statements 
end
if (condition) then 
  Statements 
else 
  Statements 
end
  • Multiple statements are enclosed within { … }
    多个语句包含在 { … } 中
  • While loop is written as follows :
    While 循环的编写方式如下:
while (condition) do
{
  Do some work
}
  • Sometimes curly braces are omitted and a block is closed with the end keyword
    有时省略大括号,并使用结束关键字封闭块
while (condition) do
 Do some work
end
  • For loop is written as follows:
    For 循环的编写方式如下:
for index ← FirstIndex to LastIndex do
{
  Do some work 
}
  • Sometimes curly braces are omitted and a block is closed with the end keyword
    有时省略大括号,并使用结束关键字封闭块
for index ← FirstIndex to LastIndex do
  Do some work 
end

Examples: 例子:

Write an algorithm for finding the factorial of number n.
编写一个用于查找数 n 的阶乘的算法。

Algorithm FACTORIAL (n)
// Description: Find factorial of a given number
// Input: Number n whose factorial is to be computed
// Output : Factorial of n = n x (n – 1) x … x 2 x 1

if (n == 1) then
	return 1
else
	return n * FACTORIAL (n – 1)
end

Write an algorithm to perform matrix multiplication
编写算法以执行矩阵乘法

Algorithm MATRIX_MULTIPLICATION (A, B)
// Description: Perform matrix multiplication of two matrices.
// Input : Two matrices A and B of size n x n.
// Output : Resultant matrix containing multiplication of A and B

for i ← 1 to n do
	for j ← 1 to n do
		C [i][j]0
		for k ← 1 to n do
			C [i][j] ← C [i][j] + A [i][k] * B [k][j]
		end
	end
end

However, there is no strict rule to follow these standards. Its syntax may vary from person to person.
但是,没有严格的规则来遵循这些标准。它的语法可能因人而异。


Short Questions: 简短的问题:

Define the term: Algorithm 定义术语:算法

The algorithm is a set of rules defined in specific order to do certain computation and carry out some predefined task. It is a step by step procedure to solve the problem
该算法是按特定顺序定义的一组规则,用于执行某些计算并执行某些预定义的任务。这是一个逐步解决问题的过程

State the properties of Algorithm 声明 Algorithm 的属性

Following are the properties of algorithm:
以下是算法的属性:

  • Input 输入
  • Output 输出
  • Definiteness 定性
  • Finiteness 有限性
  • Effectiveness 有效性

Define the term: Time Complexity 定义术语:时间复杂度

The amount of time required to solve the given problem is called the time complexity of that algorithm
解决给定问题所需的时间量称为该算法的时间复杂度

Define the term: Space Complexity 定义术语:空间复杂性

The amount of memory required to solve the given problem is called the space complexity of that algorithm
解决给定问题所需的内存量称为该算法的空间复杂度

Enlist few problem solving strategies 采用一些解决问题的策略

Following are some of the popular problem solving strategies:
以下是一些流行的解决问题的策略:

  • String matching algorithms
    字符串匹配算法
  • Number theory and numerical algorithms
    数论和数值算法
  • Divide and conquer 分而治之
  • Linear programming 线性规划
  • Dynamic programming 动态规划
  • Greedy methods 贪婪的方法
  • Sorting 排序

Correctness of Algorithm – Concept and Proof 算法的正确性 —— 概念和证明

Why correctness of Algorithm is essential? 为什么算法的正确性至关重要?

Proving correctness of algorithm is crucial. For many problems, algorithms are very complex. The reliability of an algorithm cannot be claimed unless and until it gives the correct output for each of the valid inputs.
证明算法的正确性至关重要。对于许多问题,算法非常复杂。除非算法为每个有效输入提供正确的输出,否则无法声称算法的可靠性。

Tracing the output of each possible input is impossible. The correctness of an algorithm can be quickly proven by checking certain conditions.
跟踪每个可能的输入的输出是不可能的。通过检查某些条件,可以快速证明算法的正确性。

Programming languages are an effective means of implementing complicated packages and applications. Complex systems, such as banking and e-commerce, are difficult to design and evaluate and are frequently incorrect. Much has been said regarding the creation of an effective tool for developing dependable software.
编程语言是实现复杂包和应用程序的有效手段。复杂的系统,如银行和电子商务,很难设计和评估,而且经常是错误的。关于创建用于开发可靠软件的有效工具,已经说了很多。

We consider software to be “right” if it performs precisely what the programmer and user want it to do. Millions of lines of code are used in modern software, which includes thousands of states and state transitions. Claiming the dependability of such software is not a simple process; it must go through all possible input scenarios to determine whether or not it is giving the proper output.
如果软件准确地执行程序员和用户希望它做的事情,那么我们认为它是 “正确的”。现代软件中使用了数百万行代码,其中包括数千种状态和状态转换。声称此类软件的可靠性不是一个简单的过程;它必须遍历所有可能的输入场景,以确定它是否提供了正确的输出。

Rather than checking all instances of input, correctness can be proved easily by mathematical models.
通过数学模型可以很容易地证明正确性,而不是检查所有输入实例。

Confirming Correctness of Algorithm 确认算法的正确性

The well-defined computational problem is a triplet of
P (I, O, R) such that I is the valid input set, O is the accepted output set, and R defines the relationship between I and O. R can be considered as a mapping function, which translates I to R. Let i ∈ I is a problem instance.
定义明确的计算问题是 P (I,O,R) 使得 I 是有效的输入集,O 是接受的输出集,R 定义了 I 和 O 之间的关系。让我∈我是一个问题实例。

The algorithm to solve problem P is correct if and only if for all the problem instance i ∈ I, it terminates and produces correct output o ∈ O, i.e. (i, o) ∈ R.
当且仅当对于所有问题实例 i ∈ I,它终止并产生正确的输出 o ∈ O,即 (i,o) ∈ R。

It is necessary to prove the correctness of the algorithm. The simplest way of doing so is to test the algorithm for different input combinations and compare the results with manually computed or already available ground truth values
有必要证明算法的正确性。最简单的方法是测试不同输入组合的算法,并将结果与手动计算或已可用的地面实况值进行比较

However, a testing algorithm for all inputs is not possible, or it may take too much time. Let the algorithm consist of a series of n steps.
但是,不可能对所有输入进行测试算法,或者可能需要太多时间。让算法由一系列 n 个步骤组成。

One way to prove the correctness of the algorithm is to check the condition before (precondition) and after (postcondition) the execution of each step.
证明算法正确性的一种方法是在执行每个步骤之前(前置条件)和之后(后置条件)检查条件。

The algorithm is correct only if the precondition is true, and then the postcondition must also be true.
当前条件为真时,该算法才是正确的,然后后置条件也必须为真。

Consider the problem of finding the factorial of a number n. The algorithm halts after doing (n – 1) multiplications. Proving the correctness of this problem informally is very simple. But for large inputs, it becomes tedious to store a huge output.
考虑求数 n 的阶乘问题。 算法在执行 (n – 1) 乘法运算后停止。非正式地证明这个问题的正确性非常简单。但是对于大输入,存储巨大的输出变得乏味。

Algorithms for real-life problems often involve a loop. The correctness of such an algorithm is proved through the loop invariant property. It involves three steps:
解决现实生活中问题的算法通常涉及一个循环。通过循环不变属性证明了这种算法的正确性。它包括三个步骤:

correctness of Algorithm

Steps to prove loop invariant property

Initialization: Conditions true before the first iteration of the loop
初始化:在循环的第一次迭代之前条件为真

Maintenance: If the condition is true before the loop, it must be true before the next iteration.
维护:如果条件在循环之前为真,则在下一次迭代之前必须为真。

Termination: On termination of the loop, the invariant gives us a useful property that helps us prove the correctness of the algorithm.
终止:在循环终止时,不变量为我们提供了一个有用的属性,可以帮助我们证明算法的正确性。

Correctness does not involve analysis of the algorithm
正确性不涉及对算法的分析

Prove the correctness of Insertion sort 证明插入排序的正确性

The algorithm for insertion sort is given below.
下面给出了插入排序的算法。

Algorithm INSERTION_SORT (A, n)

for j ← 2 to n do
 Key ← A [j]
 i ← j – 1 
 while i > 0 && key < A [i] do
  A [i + 1] ← A [i]
  i ← i – 1
 end
 A [i + 1] ← key
end

First, j cards are always sorted in array A [1…j – 1]. The remaining A [j…n] cards are unsorted.
首先,j 张牌总是在数组 A [1…J – 1]。剩下的 A [j…n] 卡片未排序。

Let us try to prove the correctness of INSERTION_SORT using three-loop invariant properties.
让我们尝试使用三循环不变属性来证明 INSERTION_SORT 的正确性。

Initialization: 初始化:

The loop invariant holds before initialization. For j = 2, subarray A [1…j – 1] contains only one element, i.e. A [1]. This is a trivial case of sorting. It proves that the loop invariant is true before the first iteration.
循环不变性在初始化之前保持不变。对于 j = 2,子数组 A [1…j – 1] 只包含一个元素,即 A [1]。这是一个微不足道的排序案例。它证明了循环不变性在第一次迭代之前是正确的。

Maintenance: 保养:

In each iteration, the algorithm finds the correct position of the key to insert the element A [j] by moving A [j – 1], A [j – 2],… elements. After the loop, A [j] element will be inserted into the correct position. After the loop, A [1…j] contains elements the same as in A [1…j] before the loop, but now they will be in sorted order. Thus the invariant is held in attendance too.
在每次迭代中,算法通过移动 A [j – 1]、A [j – 2] 来找到键的正确位置以插入元素 A [j,… 元素。循环后,A [j] 元素将入到正确的位置。循环之后,A [1…j] 包含与 A [1…j] 在循环之前,但现在它们将按排序顺序排列。因此,不变性也出席了会议。

Termination: 终止:

The loop terminates when j = n. Each iteration inserts A [j] on the correct location, so after n iteration, all elements will be in their right position. After the loop, A [1…n] contains elements the same as in A [1…n] before the loop, but now they will be in sorted order.
当 j = n 时,循环终止。每次迭代都会在正确的位置插入 A [j],因此在 n 次迭代后,所有元素都将位于其正确的位置。循环之后,A [1…n] 包含与 A [1…n] 在循环之前,但现在它们将按排序顺序排列。

Short Questions: 简短的问题:

Q: Why proving correctness is important? 问:为什么证明正确性很重要?

Tracing the output of each possible input is impossible. The correctness of an algorithm can be quickly proved by checking certain conditions.
跟踪每个可能的输入的输出是不可能的。通过检查某些条件,可以快速证明算法的正确性。

Q: Which are the steps for loop invariant? 问:循环不变的步骤是什么?

Loop invariant involves:
环路不变性涉及:

  • Initialization: Conditions true before the first iteration of the loop
    初始化:在循环的第一次迭代之前条件为真
  • Maintenance: If the condition is true before the loop, it must be true before the next iteration.
    维护:如果条件在循环之前为真,则在下一次迭代之前必须为真。
  • Termination: On termination of the loop, the invariant gives us a useful property that helps us to prove the correctness of the algorithm.
    终止:在循环终止时,不变量为我们提供了一个有用的属性,可以帮助我们证明算法的正确性。

pproaches for Efficiency Analysis of Algorithm 算法效率分析方法

In this article, we discuss three most common approaches for efficiency analysis of algorithm.
在本文中,我们将讨论三种最常见的算法效率分析方法。

The term “algorithm analysis” refers to the process of determining the resources required by the algorithm. Time, space, power usage, communication bandwidth, computer hardware, and so on are examples of resources.
术语 “算法分析” 是指确定算法所需资源的过程。时间、空间、电源使用、通信带宽、计算机硬件等都是资源的示例。

The running time of an algorithm is proportional to the size of the problem. The amount of resources required grows in the order of linear, quadratic, factorial, and so on.
算法的运行时间与问题的大小成正比。所需的资源量按线性、二次、阶乘等顺序增长。

There may be more than one way to address the same problem. As a result, researchers are concerned with determining the optimal method. The efficiency of an algorithm is assessed by the amount of resources it requires.
可能有多种方法可以解决同一问题。因此,研究人员关心的是确定最佳方法。算法的效率是通过它所需的资源量来评估的。

Algorithm analysis provides an estimate of the resources necessary to tackle the given task. This aids in determining the optimal algorithm from among the set of candidate algorithms.
算法分析提供了处理给定任务所需资源的估计。这有助于从候选算法集中确定最佳算法。

We don’t worry about performance if the problem is to be solved for a small number of instances. We could select the algorithm with the least amount of programming complexity. However, if a problem has to be solved repeatedly for a large number of instances, efficiency is important.
如果要为少数实例解决问题,我们不担心性能问题。我们可以选择编程复杂度最小的算法。但是,如果一个问题必须针对大量实例重复解决,则效率很重要。

So, how should the optimal algorithm be chosen? All potential algorithms’ performance must be compared. The candidate of choice is the one who takes the least amount of time.
那么,应该如何选择最优算法呢?必须比较所有潜在算法的性能。选择的候选人是花费最少时间的人。

We are generally uninterested in space complexity because memory is no longer a major barrier, and it is becoming increasingly cost-effective. As a result, researchers are primarily concerned with increasing time complexity. Any algorithm can be analyzed in three ways:
我们通常对空间复杂性不感兴趣,因为内存不再是主要障碍,而且它变得越来越具有成本效益。因此,研究人员主要关注的是时间复杂性的增加。任何算法都可以通过三种方式进行分析:

Approaches for Efficiency Analysis

Approaches for Efficiency Analysis

Empirical approach 实证法

An empirical approach is applied to the program. Various problem-solving strategies, such as greedy algorithm, dynamic programming, incremental approach, and so on, are turned into the programme and then tested on the computer for various input cases.
该程序采用实证方法。各种解决问题的策略,如贪婪算法、动态规划、增量方法等,都被转化为程序,然后在计算机上针对各种输入情况进行测试。

This method is also known as a posterior approach. It is also possible to test an algorithm for a large number of instances.
这种方法也称为后验方法。也可以为大量实例测试算法。

Theoretical approach 理论方法

The necessary resources are estimated mathematically in this approach. This strategy is also known as the priori approach because it is immediately applied to an algorithm that has yet to be changed in the program.
在这种方法中,必要的资源是以数学方式估计的。这种策略也被称为先验方法,因为它立即应用于程序中尚未更改的算法。

The algorithm is evaluated for many scenarios. The size of the problem is not measured in bits or bytes, but in the number of items in the problem.
该算法针对许多场景进行了评估。问题的大小不是以比特或字节为单位来衡量的,而是以问题中的项数来衡量的。

In sorting issues, the size of an instance is the number of elements to be sorted. The size of the challenge for traversing the linked list is a number of nodes. The size of a graph problem is determined by the number of edges and vertices.
在排序问题时,实例的大小是要排序的元素数。遍历链表的挑战的大小是多个节点。图形问题的大小由边和顶点的数量决定。

A character, integer, float, double, or any other data type may be used as an input element. We aren’t concerned with how much RAM each piece of the input list takes up.
字符、整数、浮点数、双精度型或任何其他数据类型都可以用作输入元素。我们不关心输入列表的每一部分占用多少 RAM。

Advantages: 优势:

  • As this approach is assessed on paper, it is independent of the programmer’s talent, the capacity of the machine, the strength of the programming language, and everything else.
    由于这种方法是在纸面上评估的,因此它与程序员的才能、机器的容量、编程语言的强度以及其他一切无关。
  • It saves a lot of coding time because it has already been tested.
    它节省了大量的编码时间,因为它已经过测试。

Disadvantage : 缺点:

  • Because the algorithm is tested on hand in this approach, it cannot be traced for huge input.
    由于该算法在这种方法中进行了手头测试,因此无法跟踪大量输入。
  • It is impossible to comment on the behavior of an algorithm until it has been tested for enormous input sizes.
    在对算法的巨大输入大小进行测试之前,不可能对算法的行为进行评论。

Hybrid approach 混合方法

It combines the best aspects of the priori and posterior approaches. The theoretical technique is used to determine the function characterizing the algorithm’s efficiency, and the empirical approach is used to derive the requisite numerical parameters by running a program on the machine.
它结合了先验和后验方法的最佳方面。理论技术用于确定表征算法效率的函数,而经验方法则用于通过在机器上运行程序来推导必要的数值参数。

On a computer, required parameters are found using regression algorithms.
在计算机上,使用回归算法找到所需的参数。

The time requirement is predicted by testing it for tiny instances using the priori technique, and it may be verified by running it for big cases on the actual system using the posterior approach.
时间需求是通过使用先验技术对微小实例进行测试来预测的,并且可以通过使用后验方法在实际系统上运行它来验证它对大案例。

However, such an approach may fail at times due to a lack of theoretical basis.
然而,由于缺乏理论基础,这种方法有时可能会失败。

Thus, the approaches for efficiency analysis of algorithm describes the different ways of how we can effectively analyze the algorithm. Each method has its pros and cons.
因此,算法效率分析的方法描述了我们如何有效分析算法的不同方法。每种方法都有其优点和缺点。


How a Smart Algorithm Can beat a Great Hardware? 智能算法如何击败出色的硬件?

Smart algorithms do intelligent work and easily outperform hardware technologies. In this article, we will look at a specific scenario in which a cleverly designed algorithm outperforms hardware. Algorithms have evolved over time, from primitive to very efficient.
智能算法可以完成智能工作,并且可以轻松超越硬件技术。在本文中,我们将研究一个特定场景,在该场景中,巧妙设计的算法优于硬件。算法随着时间的推移而发展,从原始到非常高效。

Different algorithms take different amounts of time to solve the same problem. This gap in algorithmic processing may be more important than the difference in machine hardware and software architectures.
不同的算法需要不同的时间来解决相同的问题。算法处理中的这种差距可能比机器硬件和软件架构的差异更重要。

Effect of Smart Algorithm 智能算法的效果

Consider a faster computer A (executes 1010 instructions/second) and slower computer B (executes 107 instructions /second).
考虑较快的计算机 A(执行 1010 条指令 / 秒)和较慢的计算机 B(执行 107 条指令 / 秒)。

Insertion sort runs in quadratic order of input size i.e. c1.n2, and merge sort runs in c2.nlogn time.
插入排序按输入大小的二次顺序运行,即 c1.n2,合并排序在 c2.nlogn 时间内运行。

Let us schedule insertion sort on computer A with c1 = 2, and merge sort on computer B with c2 = 50.
让我们在计算机 A 上安排插入排序,c1 = 2,在计算机 B 上安排合并排序,c2 = 50。

Consider the problem of size n = 107.
考虑大小 n = 107 的问题。

Running time of insertion sort on computer A would be,
计算机 A 上插入排序的运行时间为:

((2 x 107) 2 instructions) / (1010 instructions /seconds) = 20,000 seconds
((2 x 107)2 条指令)/(1010 条指令 / 秒)= 20,000 秒

Running time of merge sort on computer B would be,
在计算机 B 上合并排序的运行时间为:

(50 x 107 log 107 instructions) / (1010 instructions /seconds) = 1, 163 seconds
(50 x 107 日志 107 条指令)/(1010 条指令 / 秒)= 1,163 秒

Even with poor hardware, the efficient algorithm runs 17 times faster than the poor algorithm on faster hardware. Advantage amplifies when the input instance is even bigger. For 100 million numbers, merge sort takes under four hours, whereas insertion sort takes more than 23 days.
即使在硬件较差的情况下,高效算法的运行速度也比在较快的硬件上运行较差的算法快 17 倍。当输入实例更大时,优势会放大。对于 1 亿个数字,合并排序需要不到 4 小时,而插入排序需要 23 天以上。

Analysis and Discussion 分析与讨论

The above illustration is sufficient to prove the importance of the design of a proper algorithm. For efficiency analysis, algorithm complexity should be evaluated for a large program instance.
上面的插图足以证明设计适当算法的重要性。对于效率分析,应针对大型程序实例评估算法复杂性。

The choice of the algorithm is as important as choosing the hardware, and hence the algorithm should also be considered as technology just like hardware.
算法的选择与硬件的选择同样重要,因此算法也应该像硬件一样被视为技术。

Mathematicians are improving algorithms at a pace researchers are improving the physical hardware.
数学家正在以更快的速度改进算法,研究人员正在改进物理硬件。

Some applications do not require algorithmic efficiency at the application level, but the internal design of an efficient algorithm has a great deal.
有些应用在应用层面并不需要算法效率,但是高效算法的内部设计却有很大的要求。

Consider the application that determines the route to travel from one location to the other. An implementation may rely on fast hardware, wide-area networking, and a possible programming language. Even in such applications also, algorithms would be useful in determining the route, rendering the graphics, interpolating addresses etc.
考虑确定从一个位置到另一个位置的路线的应用程序。实现可能依赖于快速硬件、广域网和可能的编程语言。即使在这样的应用中,算法在确定路线、渲染图形、插值地址等方面也很有用。

We intend to solve larger and larger problems with ever-increasing hardware capacity. The impact of the efficient algorithm is multiplied across both larger problems, just as we discussed above. The well-developed merge sort easily beats the insertion sort running on superior hardware.
我们打算用不断增加的硬件容量来解决越来越大的问题。正如我们上面所讨论的那样,高效算法的影响在两个更大的问题上都成倍增加。完善的合并排序轻松击败了在高级硬件上运行的插入排序。

Solid knowledge of algorithms separates the truly skilled programmer from the novice. We can accomplish our tasks without knowing much about algorithms, but we can do it much more efficiently with knowledge of the algorithm.
扎实的算法知识将真正熟练的程序员与新手区分开来。我们可以在对算法知之甚少的情况下完成我们的任务,但有了算法的知识,我们可以更有效地完成任务。

Short Questions 简短的问题

Q: What is the time complexity of insertion sort? 问:插入排序的时间复杂度是多少?

Insertion sort runs in O (n2) time
插入排序以 O(n2) 时间为

Q: What is the time complexity of merge sort? 问:合并排序的时间复杂度是多少?

Merge sort runs in O (nlogn) time
合并排序以 O(nlogn) 时间运行。


via:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值