1.算法导论

Algorithms and data structures 

算法与数据结构

 An algorithm is a well-defined computational procedure that takes some value, or sets of values, as input, and produces a value or a sets of values as output.
It solves a computational problem (there are many, discuss examples…) 
It is important to analyze the particular implementation (complexity).
Manipulating data: data structures --a way to store and organize data to facilitate access and modifications. Each DS for a different problem.

算法是一个定义明确的计算过程,它接受一些值或集合并生成一个值或一组值作为输出。
它解决了一个计算问题(有很多,讨论例子…)
分析特定的实现(复杂性)非常重要。
操纵数据:数据结构——存储和组织数据以促进访问和修改。每个DS对应不同的问题。 

 

 Data structures vs. data types

 数据结构与数据类型

 These two terms are tightly related.
A data type is a set of possible values that a variable, a constant or a program, can 
take, that share some common properties.
A data structure is a mathematical object grouping elements with their respective 
characteristic operations (efficient implementations).

DS are used for implementing DT.

这两个术语密切相关。
数据类型是一组变量、常量或程序可以使用的可能值
举个例子,它们有一些共同的特性。
数据结构是一种数学对象,它基于元素及其各自的特征操作进行分类(高效实施)。
DS用于实现DT。 

 

The sorting problem 

 Input: A sequence of n numbers <a1,a2,...an>.

输入:n个数字的序列<a1,a2,...an>
Output: A permutation (reordering) <a’1,a’2,...a’n> of the input sequence such that
a’1 <= a’2 <= ... <= a’n

输出:对于输入序列<a’1,a’2,...a’n>的一个排列(重排列),它满足:a’1 <= a’2 <= ... <= a’n
The sequences are typically stored in arrays.
We also refer to the numbers as keys. Along with each key may be additional 
information, known as satellite data.

 序列通常存储在数组中。
我们还将这些数字称为键。与每把键匹配的是额外的信息,称为卫星数据。(让我想起了python字典里面的键值对)

 

Expressing algorithms 

算法表述 

 ●     We express algorithms in whatever way is the clearest and most concise. 
●     English is sometimes the best way.
●     When issues of control need to be made perfectly clear, we often use pseudocode.
●     Pseudocode is similar to C, C++, Python, and Java. If you know any of these languages, 
you should be able to understand pseudocode.
●     Pseudocode is designed for expressing algorithms to humans. Software engineering 
issues of data abstraction, modularity, and error handling are often ignored.
●     Unlike for “real” programming languages, we cannot create a compiler that translates 
pseudocode to machine code.

●     我们以最清晰、最简洁的方式表达算法。
●     英语有时是最好的方式。
●     当控制问题需要完全明确时,我们通常使用伪代码。
●     伪代码类似于C、C++、Python和Java。如果您知道这些语言中的任何一种,您应该能够理解伪代码。
●     伪码是为向人类表达算法而设计的。数据抽象、模块化和错误处理等软件工程问题常常被忽视。
●     与“真实”编程语言不同,我们无法创建将伪代码转换为机器代码的编译器。

 

 First example: insertion sort
●     A  good  algorithm  for  sorting  a  small number of elements.
●     It works the way you might sort a hand of playing cards.

 

●      We  start  with  an  empty  left  hand  and  the cards face down on the table.
● We then remove one card at a time from the table and insert it into the correct position in the left hand.
● To find the correct position for a card, we compare it with each of the cards already in the hand, from right to left.
● At all times, the cards held in the left hand are sorted, and these cards were originally the 
top cards of the pile on the table.

● 我们开始时左手是空的,牌面朝下放在桌子上。
● 然后,我们从桌子上一次取出一张卡片,并将其插入左手的正确位置。
● 为了找到一张牌的正确位置,我们从右到左将其与手中的每张牌进行比较。
● 在任何时候,左手所持的牌都是经过排序的,而这些牌原本是桌子上那堆牌的最上面的牌。

 

Insertion sort pseudocode 

插入排序伪代码 

 

 

 Executing insertion sort algorithm

执行插入排序算法 

 

 

Observations 

 ●     The array A is sorted in place: the numbers are rearranged within the array, with at 
most a constant number outside the array at any time.
●     We usually use 1-origin indexing, as we do here. Careful, in the C language we use 
0-origin indexing!
●     We use “..” to denote a range within an array.

●     数组A按顺序排列:数字在数组中重新排列;在任何时候,数组外最多是一个常量。
●     我们通常使用单原点索引,就像我们在这里所做的那样。小心,在我们使用的C语言中
0原点索引!
●     我们使用“..”表示数组中的范围。 

Typical recipe to show correctness of some algorithm 

显示某些算法正确性的典型方法 

 1. Find the invariant of the main loop/recursion.
2. Use the Initialization/Maintenance/Termination pattern to lay out your proof of correctness.
3. The level of detail depends on context – some sub-loops can be left out of the proof as they are trivial. If some exercise really needs full precision, it will be clearly asked.

Loop invariant of insertion sort 

插入排序的循环不变量

We often use a loop invariant to help us understand why an algorithm gives the correct 
answer. Here’s the loop invariant for INSERTION-SORT:
Loop invariant: At the start of each iteration of the “outer” for loop (the one indexed by j), the 
subarray A[1..j-1] consists of the elements originally in A[1..j-1] but in sorted order.
To use a loop invariant to prove correctness, we must show three things about it:
Initialization: It is true prior to the first iteration of the loop.
Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration. 
Termination: When the loop terminates, the invariant (and the reason that the loop terminated) givesus a useful property that helps show that the algorithm is correct. 

我们经常使用循环不变量来帮助我们理解为什么算法给出正确的答复

以下是INSERTION-SORT的循环不变量:
循环不变量:在“外部”for循环(由j索引的循环)的每次迭代开始时,子数组A[1..j-1]由最初在 A[1..j-1]中但按排序顺序排列的元素组成。
为了使用循环不变量来证明正确性,我们必须展示它的三个方面:
初始化:在循环的第一次迭代之前为真。
维护:如果在循环迭代之前为真,则在下一次迭代之前保持为真。
终止:当循环终止时,不变量(以及循环终止的原因)提供了一个有用的属性,有助于证明算法是正确的。 

 

Using loop invariants is like mathematical induction 

使用循环不变量就像数学归纳法

●        To prove that a property holds, you prove a base case and an inductive step.
●        Showing that the invariant holds before the first iteration is like the base case.
●        Showing that the invariant holds from iteration to iteration is like the inductive step. 
●        The termination part differs from the usual use of mathematical induction, in which 
          the  inductive  step  is  used  infinitely.  We  stop  the  “induction”  when  the  loop 
          terminates.
●        We can show the three parts in any order. 

●        为了证明一个性质成立,你需要证明一个基本情况和一个归纳步骤。
●        表明不变量在第一次迭代之前成立,这与基本情况类似。
●        表明不变量从一次迭代到另一次迭代都成立,就像归纳步骤。
●        终止部分不同于通常使用的数学归纳法,其中无限使用归纳步骤。当循环终止时,我们停止            “归纳”。
●        我们可以按任何顺序显示这三个部分。

Correctness of insertion sort

插入排序的正确性

Initialization:  Just  before  the  first  iteration,  j=2. The  subarray A[1..j-1]  is  the  single 
element A[1], which is the element originally in A[1], and it is trivially sorted. 
Maintenance: To be precise, we would need to state and prove a loop invariant for the 
“inner” while loop. Rather than getting bogged down in another loop invariant, we instead 
note that the body of the inner while loop works by moving A[j-1], A[j-2], A[j-3], etc. by one 
position to the right until the proper position for key (which has the value that started out in 
A[j]) is found. At that point, the value of key is placed into this position.
Termination: The outer for loop ends when j > n, which occurs when j=n+1. Therefore, 
j-1=n. Plugging n in for j-1 in the loop invariant, the subarray A[1..n] consists of the 
elements originally in A[1..n] but in sorted order. In other words, the entire array is sorted. 

初始化:就在第一次迭代之前,j=2。子阵列A[1..j-1]是单个元素A[1],它是最初在A[1]中的元素,并且它被普通地排序。
维护:准确地说,我们需要声明并证明“内”while循环。我们没有陷入另一个循环不变量,而是注意到内部while循环的主体通过向右移动A[j-1]、A[j-2]、A[j-3]等一个位置来工作,直到找到键的正确位置(其值从A[j]开始)。此时,key的值被放置在此位置。
终止:当j>n时,外部for循环结束,当j=n+1时发生。因此 j-1=n。将n代入循环不变量中的j-1,子阵列A[1..n]由最初位于A[1..n]中的元素组成,但按排序顺序排列。换句话说,对整个数组进行排序。

tips:

Main pseudocode conventions 

●     Indentation indicates block structure (instead of using {} ).
●     Looping constructs are like in C, C++. We assume that the loop variable in a for 
loop is still defined when the loop exits.
●     // indicates that the remainder of the line is a comment. 
●     Variables are local, unless otherwise specified.
●     We often use objects, which have attributes. For an attribute attr of object x, we 
write x.attr. (Equivalent to x→attr in C).
●     Objects are treated as pointers/references. If x and y denote objects, then the 
assignment y=x makes x and y reference the same object. It does not cause 
attributes of one object to be copied to another.

●     缩进表示块结构(而不是使用{})。
●     循环结构类似于C、C++。我们假设for中的循环变量当循环退出时仍然有定义。
●     // 指示该行的其余部分是注释。
●     除非另有规定,否则变量为本地变量。
●     我们经常使用具有属性的对象。对于对象x的属性attr,我们写入x.attr.(相当于x→C中的属性)。
●     对象被视为指针/引用。如果x和y表示对象,则赋值y=x使x和y引用同一对象。它不会导致一个对象的属性复制到另一个对象。

Exercises

1. Illustrate the operation of INSERTION-SORT on <31, 41, 59, 26, 41, 58>.
2. Rewrite the INSERTION-SORT procedure to sort into nonincreasing instead of 
nondecreasing order.
3. Consider the searching problem:
Input: A sequence of n numbers A=<a1,a2,…,an> and a value v.
Output: An index i such that v = A[i] or the special value NIL if v does not appear in A.
a. Write pseudocode for linear search, which scans through the sequence, looking for v.
b. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop 
invariant fulfills the three necessary properties.

---------------------------------------------------------------------------------------------------------------------------------

由于本人时间有限,大部分译文来自百度翻译。 如有疑惑, 请在评论区留言或私聊,(24h内必定回复😂)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带带萌新JAMMY BEAR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值