离散结构:算法(1)

章节总结

  • 算法

  • 示例算法

  • 算法范例

  • 职能的增长

  • Big-O和其他表示法

  • 算法的复杂性

Chapter Summary

  • Algorithms

  • Example Algorithms

  • Algorithmic Paradigms

  • Growth of Functions

  • Big-O and other Notation

  • Complexity of Algorithms

部分摘要

  • 算法的属性

  • 搜索和排序算法

  • 贪婪算法

Section Summary

  • Properties of Algorithms

  • Algorithms for Searching and Sorting 

  • Greedy Algorithms

问题和算法

  • 在许多域中,当给定有效输入时,存在要求具有特定属性的输出的关键一般问题。

  • 第一步是使用适当的结构精确地说明问题,以指定输入和所需的输出。

  • 然后,我们通过指定采用有效输入并产生所需输出的过程的步骤来解决一般问题。 此过程称为算法。

Problems and Algorithms

  • In many domains there are key general problems that ask for output with specific properties when given valid input.

  • The first step is to precisely state the problem, using the appropriate structures to specify the input and the desired output.

  • We then solve the general problem by specifying the steps of a procedure that takes a valid input and produces the desired output. This procedure is called an algorithm.

算法

Abu Ja'far Mohammed Ibin Musa Al-Khowarizmi(780-850)

定义:算法是用于执行计算或解决问题的一组有限精确指令。

示例:描述用于在有限的整数序列中查找最大值的算法。

Algorithms

Abu Ja’far Mohammed Ibin Musa Al-Khowarizmi (780-850)

Definition: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem.

Example: Describe an algorithm for finding the maximum value in a finite sequence of integers.

解决方案:执行以下步骤:

1. 2。

将临时最大值设置为等于序列中的第一个整数。

将序列中的下一个整数与临时最大值进行比较。

如果它大于临时最大值,则将临时最大值设置为等于此整数。

如果有更多整数,请重复上一步。 如果没有,停止。

当算法终止时,临时最大值是序列中的最大整数。

Solution: Perform the following steps:

1. 2.

Set the temporary maximum equal to the first integer in the sequence.

Compare the next integer in the sequence to the temporary maximum.

If it is larger than the temporary maximum, set the temporary maximum equal to this integer.

Repeat the previous step if there are more integers. If not, stop.

When the algorithm terminates, the temporary maximum is the largest integer in the sequence.

指定算法

  • 可以用不同的方式指定算法。 他们的步骤可以用英语或伪代码描述。

  • 伪代码是步骤的英语描述和使用编程语言编写这些步骤之间的中间步骤。

  • 我们使用的伪代码形式在附录3中规定。它使用了一些流行语言中的结构,如C ++和Java。

  • 程序员可以使用伪代码中的算法描述来构建特定语言的程序。

  • 伪代码帮助我们分析使用算法解决问题所需的时间,与用于实现算法的实际编程语言无关。

Specifying Algorithms

  • Algorithms can be specified in different ways. Their steps can be described in English or in pseudocode.

  • Pseudocode is an intermediate step between an English language description of the steps and a coding of these steps using a programming language.

  • The form of pseudocode we use is specified in Appendix 3. It uses some of the structures found in popular languages such as C++ and Java.

  • Programmers can use the description of an algorithm in pseudocode to construct a program in a particular language.

  • Pseudocode helps us analyze the time required to solve a problem using an algorithm, independent of the actual programming language used to implement algorithm.

算法的属性

  • 输入:算法具有指定集合的输入值。

  • 输出:算法根据输入值生成指定集合的输出值。 输出值是解决方案。

  • 正确性:算法应为每组输入值生成正确的输出值。

  • 有限性:算法应在任意输入的有限步数之后产生输出。

  • 有效性:必须能够在有限的时间内正确执行算法的每个步骤。

  • 通用性:算法应适用于所需形式的所有问题。

Properties of Algorithms

  • Input: An algorithm has input values from a specified set.

  • Output: From the input values, the algorithm produces the output values from a specified set. The output values are the solution.

  • Correctness: An algorithm should produce the correct output values for each set of input values.

  • Finiteness: An algorithm should produce the output after a finite number of steps for any input.

  • Effectiveness: It must be possible to perform each step of the algorithm correctly and in a finite amount of time.

  • Generality: The algorithm should work for all problems of the desired form.

找到有限序列中的最大元素

  • 伪代码算法:

过程max(a1,a2,....,an:整数)max:= a1

对于i:= 2到n

ifmax <ai thenmax:= ai

return max {max是最大的元素}

  • 此算法是否具有上一张幻灯片中列出的所有属性?

Finding the Maximum Element in a Finite Sequence

  • The algorithm in pseudocode:

procedure max(a1, a2, ...., an: integers) max := a1

for i := 2 to n

ifmax<ai thenmax:=ai

return max{max is the largest element}

  • Does this algorithm have all the properties listed on the previous slide?

一些示例算法问题

  • 本节将研究三类问题。

1.搜索问题:找到particularelementina列表的位置。

2.排序问题:将列表元素按顺序排列。

3.优化问题:确定特定数量在所有可能输入上的最佳值(最大值或最小值)。

Some Example Algorithm Problems

  • Three classes of problems will be studied in this section.

1. Searching Problems: finding the position of a particularelementina list.

2. Sorting problems: putting the elements of a list into increasing order.

3. Optimization Problems: determining the optimal value (maximum or minimum) of a particular quantity over all possible inputs.

搜索问题

定义:一般搜索问题是在不同元素a1,a2,...,an的列表中定位元素x,或者确定它不在列表中。

  • 搜索问题的解决方案是列表中元素的位置等于x(即,如果x = ai,则i是解决方案),如果x不在列表中,则为0。

  • 例如,图书馆可能希望在允许他/她签出另一本书之前检查顾客是否在列有过期书籍的人名单上。

  • 我们将研究两种不同的搜索算法; 线性搜索和二进制搜索。

Searching Problems

  • Definition: The general searching problem is to locate an element x in the list of distinct elements a1,a2,...,an, or determine that it is not in the list.

  • The solution to a searching problem is the location of the term in the list that equals x (that is, i is the solution if x=ai)or 0 if x is not in the list.

  • For example, a library might want to check to see if a patron is on a list of those with overdue books before allowing him/her to checkout another book.

  • We will study two different searching algorithms; linear search and binary search.

线性搜索算法

  • 线性搜索算法通过从头开始检查序列中的元素来定位列表中的项目。

  • 首先将x与a1进行比较。 如果它们相等,则返回位置1。

  • 如果没有,请尝试a2。 如果x = a2,则返回位置2。

  • 继续前进,如果扫描整个列表时未找到匹配项,则返回0。

过程线性搜索(x:整数,a1,a2,...,an:不同的整数)i:= 1

while(i≤n且x≠ai)

i:= i + 1

如果我≤n然后位置:=我否则位置:= 0

返回位置{location是等于x的术语的下标,如果未找到x则为0}

Linear Search Algorithm

  • The linear search algorithm locates an item in a list by examining elements in the sequence one at a time, starting at the beginning.

  • First compare x with a1. If they are equal, return the position 1.

  • If not, try a2. If x = a2, return the position 2.

  • Keep going, and if no match is found when the entire list is scanned, return 0.

procedure linear search(x:integer, a1, a2, ...,an: distinct integers) i := 1

while (i ≤ n and x ≠ ai)

i := i + 1

if i ≤ n then location := i else location := 0

return location{location is the subscript of the term that equals x, or is 0 if x is not found}

二进制搜索

  • 假设输入是按升序排列的项目列表。 算法首先要比较要素

  • 发现与中间元素。

  • 如果中间元素较低,则搜索继续进行列表的上半部分。

  • 如果不低,则搜索继续进行列表的下半部分(通过中间位置)。

  • 重复此过程,直到我们有一个大小为1的列表。

  • 如果我们要找的元素等于元素中的元素

  • 列表,返回的位置。

  • 否则,返回0表示未找到该元素。

  • 在3.3节中,我们表明二进制搜索算法比线性搜索更有效。

Binary Search

  • Assume the input is a list of items in increasing order.  The algorithm begins by comparing the element to be

  • found with the middle element.

  • If the middle element is lower, the search proceeds with the upper half of the list.

  • If it is not lower, the search proceeds with the lower half of the list (through the middle position).

  • Repeat this process until we have a list of size 1.

  • If the element we are looking for is equal to the element in the

  • list, the position is returned.

  • Otherwise, 0 is returned to indicate that the element was not found.

  • In Section 3.3, we show that the binary search algorithm is much more efficient than linear search.

二进制搜索

  • 以下是伪代码中二进制搜索算法的描述。

过程二进制搜索(x:整数,a1,a2,...,an:增加整数)i:= 1 {i是interval的左端点}

j:= n {j是间隔的右端点}

而我<j

m:=⌊(i + j)/2⌋

如果x> am则i:= m + 1

否则j:= m

如果x = ai则位置:= i其他位置:= 0

返回位置{location是术语ai的下标i等于x,如果未找到x则为0}

Binary Search

  • Here is a description of the binary search algorithm in pseudocode.

procedure binary search(x: integer, a1,a2,..., an: increasing integers) i := 1 {i is the left endpoint of interval}

j := n {j is right endpoint of interval}

while i < j

m := ⌊(i + j)/2⌋

if x > am then i := m + 1

else j := m

if x = ai then location := i else location := 0

return location{location is the subscript i of the term ai equal to x, or 0 if x is not found}

二进制搜索

示例:列表中二进制搜索19所采取的步骤:1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

1.列表有16个元素,因此中点为8.第8个位置的值为10.由于19> 10,进一步搜索仅限于位置9到16。

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

2.列表的中点(位置9到16)现在是第12个位置,值为16.自19> 16以来,进一步搜索仅限于第13个位置及以上位置。

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

3.当前列表的中点现在是第14个位置,值为19.自19≯19以来,进一步搜索仅限于第13到第14位的部分。

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

4.当前列表的中点现在是第13个位置,值为18.由于19> 18,搜索仅限于从第14位到第14位的部分。

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

5.现在列表中有一个元素,循环结束。 从19 = 19开始,返回位置14。

Binary Search

Example: The steps taken by a binary search for 19 in the list: 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

1. The list has 16 elements, so the midpoint is 8. The value in the 8th position is 10. Since 19 > 10, further search is restricted to positions 9 through 16.

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

2. The midpoint of the list (positions 9 through 16) is now the 12th position with a value of 16. Since 19 > 16, further search is restricted to the 13th position and above.

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

3. The midpoint of the current list is now the 14th position with a value of 19. Since 19 ≯ 19, further search is restricted to the portion from the 13th through the 14th positions .

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

4. The midpoint of the current list is now the 13th position with a value of 18. Since 19> 18, search is restricted to the portion from the 14th position through the 14th.

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

5. Now the list has a single element and the loop ends. Since 19=19, the location 14 is returned.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

19>10

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

19 > 16

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

19 = 19

return 14 ( the position of 19)

排序

  • 对列表元素进行排序是在许多情况下出现的问题。 例如,

  • 要生成电话簿,必须按字母顺序排列订户名称。

  • 同样,制作可供下载的歌曲目录要求其标题按字母顺序排列。

  • 在电子邮件邮件列表中按顺序放置地址可以确定是否存在重复的地址。

  • 创建有用的字典需要按字母顺序排列。

  • 同样,生成零件清单要求我们根据零件编号的增加对它们进行订购。

Sorting

  • Ordering the elements of a list is a problem that occurs in many contexts. For example,

  • to produce a telephone directory it is necessary to alphabetize the names of subscribers.

  • Similarly, producing a directory of songs available for downloading requires that their titles be put in alphabetic order.

  • Putting addresses in order in an e-mail mailing list can determine whether there are duplicated addresses.

  • Creating a useful dictionary requires that words be put in alphabetical order.

  • Similarly, generating a parts list requires that we order them according to increasing part number.

排序

  • 对列表元素进行排序是按顺序(数字顺序,字母顺序等)对它们进行排序。

  • 排序是一个重要问题,因为:

  • 所有计算资源的重要百分比用于排序不同类型的列表,尤其是涉及需要按特定顺序(例如,按客户,部件号等)呈现的大型信息数据库的应用程序。

  • 已经发明了大量基本不同的算法用于分类。 他们的相对优点和缺点已被广泛研究。

  • 排序算法有助于说明计算机科学的基本概念。

Sorting

  • To sort the elements of a list is to put them in increasing order (numerical order, alphabetic, and so on).

  • Sorting is an important problem because:

  • A nontrivial percentage of all computing resources are devoted to sorting different kinds of lists, especially applications involving large databases of information that need to be presented in a particular order (e.g., by customer, part number etc.).

  • An amazing number of fundamentally different algorithms have been invented for sorting. Their relative advantages and disadvantages have been studied extensively.

  • Sorting algorithms are useful to illustrate the basic notions of computer science.

排序

  • 本书研究了各种排序算法; 二进制,插入,气泡,选择,合并和快速。

  • 在3.3节中,我们将研究使用本节中介绍的排序算法对列表进行排序所需的时间。

Sorting

  • A variety of sorting algorithms are studied in this book; binary, insertion, bubble, selection, merge, and quick.

  • In Section 3.3, we’ll study the amount of time required to sort a list using the sorting algorithms covered in this section.

冒泡排序

  • 冒泡排序是最简单的排序算法之一,但不是最有效的排序算法之一。

  • 通过连续比较相邻元素,将它们列入递增顺序,如果它们的顺序错误,则将它们互换。

  • 为了执行冒泡排序,我们执行基本操作,即,从列表的开头开始,将更大的元素与其后的较小元素进行交换,以获得完整的通过。

  • 我们重复此过程,直到排序完成。

Bubble Sort

  • Bubble sort is one of the simplest sorting algorithms, but not one of the most efficient.

  • It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order.

  • To carry out the bubble sort, we perform the basic operation, that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for a full pass.

  • We iterate this procedure until the sort is complete.

冒泡排序

  • 在第一次通过时,最大的元素已被放入正确的位置。

  • 在第二次通过结束时,第二大元素已被放入正确的位置。

  • 在每个后续过程中,将一个附加元素放在正确的位置。

程序bubblesort(a1,...,an:实数n≥2),i:= 1到n-1

对于j:= 1到n - i

如果aj> aj + 1则交换aj和aj + 1

{a1,...,现在正在递增}

Bubble Sort

  • At the first pass the largest element has been put into the correct position.

  • At the end of the second pass, the 2nd largest element has been put into the correct position.

  • In each subsequent pass, an additional element is put in the correct position.

procedure bubblesort(a1,...,an: real numbers with n ≥ 2) for i := 1 to n− 1

for j := 1 to n − i

if aj >aj+1 then interchange aj and aj+1

{a1,..., an is now in increasing order}

插入排序

  • 插入排序从第2个元素开始。 它将第二个元素与第一个元素进行比较,如果它不大,则将它放在第一个元素之前。

  • 接下来,第3个元素被放入前3个元素中的正确位置。 将第三个元素与第一个元素进行比较,如果它大于第一个元素,则将其与第二个元素进行比较; 它被插入前三个元素中的正确位置。

  • 在每个后续过程中,第n + 1个元素被放入前n + 1个元素中的正确位置。

  • 线性搜索用于找到正确的位置。

Insertion Sort

  • Insertion sort begins with the 2nd element. It compares the 2nd element with the 1st and puts it before the first if it is not larger.

  • Next the 3rd element is put into the correct position among the first 3 elements. The third element is compared with the first element, and if it is larger than the first element, it is compared with the second element; it is inserted into the correct position among the first three elements.

  • In each subsequent pass, the n+1st element is put into its correct position among the first n+1 elements.

  • Linear search is used to find the correct position.

通常,在插入排序的第j步中,列表的第j个元素被插入到先前排序的j-1个元素的列表中的正确位置。

过程插入排序(a1,...,an:对于j:= 2到n

我:= 1

而aj> ai

i:= i + 1 m:= aj

fork:= 0toj -i-1 aj-k:= aj-k-1

ai:= m

{现在a1,...,a正在递增}

n≥2的实数

In general, in the jth step of the insertion sort, the jth element of the list is inserted into the correct position in the list of the previously sorted j − 1 elements.

procedure insertion sort (a1,...,an: for j := 2 to n

i := 1

while aj > ai

i := i + 1 m := aj

fork:=0toj −i−1 aj-k := aj-k-1

ai := m

{Now a1,...,an is in increasing order}

real numbers with n ≥ 2)

示例:使用输入显示插入排序的所有步骤:3 2 4 1 5

一世。 2 3 4 1 5(前两个位置互换)

II。 2 3 4 1 5(第三个元素保持在其位置)

III。 1 2 3 4 5(第四个位于开头)

IV。 1 2 3 4 5(第五个元素保持在其位置)

Example: Show all the steps of insertion sort with the input: 3 2 4 1 5

i. 2 3 4 1 5 (first two positions are interchanged)

ii. 2 3 4 1 5 (third element remains in its position)

iii. 1 2 3 4 5 (fourth is placed at beginning)

iv. 1 2 3 4 5 (fifth element remains in its position)

分而治之的算法策略

  • 将数组分区为小于数据透视的项目以及大于或等于数据透视表的项目

  • 对左侧部分进行排序

  • 对右侧部分进行排序

A divide-and-conquer algorithm  Strategy

  • Partition an array into items that are less than the pivot and those that are greater than or equal to the pivot

  • Sort the left section

  • Sort the right section

使用不变量来开发分区算法分区算法不变

区域S1中的项目都小于枢轴,S2中的项目都大于或等于枢轴

  • Using an invariant to develop a partition algorithm  Invariant for the partition algorithm

  • The items in region S1 are all less than the pivot, and those in S2 are all greater than or equal to the pivot

  • 方法在数组中从左前方搜索大于pivot的第一个元素,然后在数组中向右搜索第一个元素,该元素小于或等于pivot。 交换这两个元素。 重复相同的搜索和交换操作,直到在while循环中搜索所有元素。

  • The method search for the first element from left forward in the array that is greater than the pivot, then search for the first element from right backward in the array that is less than or equal to the pivot. Swap these two elements. Repeat the same search and swap operations until all the elements are searched in a while loop.

  • 分析

  • 快速排序通常在实践中非常快

  • 即使出现最坏情况,对于中等大小的阵列,quicksort的性能也是可以接受的

  • Analysis

  • quicksort is usually extremely fast in practice

  • Even if the worst case occurs, quicksort’s performance is acceptable for moderately large arrays

贪心算法

  • 优化问题可以最大限度地减少或最大化某些参数。

  • 许多优化问题中的一些是:

  • 在两个最小的城市之间寻找路线

  • 总里程。

  • 确定如何使用尽可能少的位编码消息。

  • 使用最少量的光纤查找网络节点之间的光纤链路。

Greedy Algorithms

  • Optimization problems minimize or maximize some parameter over all possible inputs.

  • Some of the many optimization problems are:

  • Finding a route between two cities with the smallest

  • total mileage.

  • Determining how to encode messages using the fewest possible bits.

  • Finding the fiber links between network nodes using the least amount of fiber.

优化问题通常可以使用贪婪算法来解决,这使得每个步骤都成为“最佳”选择。 在每个步骤中做出“最佳选择”并不一定能够为整体问题提供最佳解决方案,但在许多情况下确实如此。

  • 在指定每个步骤的“最佳选择”之后,我们试图证明这种方法总能产生最佳解决方案,或者找到一个反例来证明它没有。

  • 解决问题的贪婪方法是算法范例的一个例子,它是设计算法的一般方法。

示例:设计一个贪婪的算法,使用以下硬币进行n美分的更改(以美元计价):四分之一(25美分),一角硬币(10美分),镍币(5美分)和便士(1美分),使用最少 硬币总数。

想法:在每个步骤中选择具有最大可能值但不超过剩余变化量的硬币。

1.如果n = 67美分,首先选择一个季度,留下67-25 = 42美分。 然后选择另一个季度,留下42 -25 = 17美分

然后选择1角钱,留下17 - 10 = 7美分。

3.选择1镍,留下7 - 5 = 2美分。

选择一分钱,留一分钱。 选择另一分钱,留0美分。

Example: Design a greedy algorithm for making change(in U.S. money) of n cents with the following coins: quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent) , using the least total number of coins.

Idea: At each step choose the coin with the largest possible value that does not exceed the amount of change left.

1. If n = 67 cents, first choose a quarter leaving 67−25 = 42 cents. Then choose another quarter leaving 42 −25 = 17 cents

2. Then choose 1 dime, leaving 17 − 10 = 7 cents.

3. Choose 1 nickel, leaving 7 – 5 = 2 cents.

4. Choose a penny, leaving one cent. Choose another penny leaving 0 cents.

解决方案:贪婪的变换算法为n美分。 该算法适用于任何硬币面额c1,c2,...,cr。

程序改变(c1,c2,...,cr:硬币值,其中c1> c2> ...> cr;正整数)

对于i:= 1到r

di:= 0 [di计算面额ci的硬币],而n≥ci

di:= di + 1 [加一个面额硬币ci]

n = n - ci

[di countsthecoinsci]

n:a

  • 对于美国货币的例子,我们可能有四分之一,硬币,镍币和便士,c1 = 25,c2 = 10,c3 = 5,c4 = 1。

Solution: Greedy change-making algorithm for n cents. The algorithm works with any coin denominations c1, c2, ...,cr .

procedure change(c1, c2, ..., cr: values of coins, where c1> c2> ... > cr ; positive integer)

for i := 1 to r

di := 0 [di counts the coins of denomination ci] while n ≥ ci

di := di + 1 [add a coin of denomination ci]

n = n - ci

[di countsthecoinsci]

n: a

  • For the example of U.S. currency, we may have quarters, dimes, nickels and pennies, with c1 = 25, c2 = 10, c3 = 5, and c4 = 1.

表明美国硬币的变更算法是最优的。

引理1:如果n是一个正整数,那么使用最小硬币,使用最少的硬币,使用季度,硬币,镍币和便士的n美分最多有2个角钱,1个镍币,4个硬币,并且不能有2个角钱和镍币。 硬币,镍币和硬币的总变化量不得超过24美分。

证明:矛盾

  • 如果我们有三分之一的时间,我们可以用四分之一和一个镍代替它们。

  • 如果我们有2个镍币,我们可以用1角钱代替它们。

  • 如果我们有五分钱,我们可以用镍代替它们。

  • 如果我们有2角钱和1个镍,我们可以用四分之一替换它们。

允许的组合,最大值为24cents; 2角钱和4便士。

Show that the change making algorithm for U.S. coins is optimal.

Lemma 1: If n is a positive integer, then n cents in change using quarters, dimes, nickels, and pennies, using the fewest coins possible has at most 2 dimes, 1 nickel, 4 pennies, and cannot have 2 dimes and a nickel. The total amount of change in dimes, nickels, and pennies must not exceed 24 cents.

Proof: By contradiction

  • If we had 3 dimes, we could replace them with a quarter and a nickel.

  • If we had 2 nickels, we could replace them with 1 dime.

  • Ifwehad5pennies, we could replace them with a nickel.

  • If we had 2 dimes and 1 nickel, we could replace them with a quarter.

  • The allowable combinations,haveamaximumvalueof24cents;2 dimes and 4 pennies.

最优性取决于可用的面额。 

  • 对于美国硬币,如果我们增加一半,最优性仍然有效

  • 美元硬币(50美分)和美元硬币(100美分)。

  • 但是如果我们只允许四分之一(25美分),硬币(10美分)和便士(1美分),算法不再产生最小数量的硬币。

  • 考虑31美分的例子。 硬币的最佳数量是4,即3硬币和1便士。 算法输出什么?

Optimality depends on the denominations available.

  • For U.S. coins, optimality still holds if we add half

  • dollar coins (50 cents) and dollar coins (100 cents).

  • But if we allow only quarters (25 cents), dimes (10 cents), and pennies (1 cent), the algorithm no longer produces the minimum number of coins.

  • Consider the example of 31 cents. The optimal number of coins is 4, i.e., 3 dimes and 1 penny. What does the algorithm output?

示例:我们有一组提议的开始和结束时间。 根据以下假设,构建一个贪婪的算法,在演讲厅尽可能多地安排:

  • 谈话开始时,一直持续到结束。

  • 不能同时进行两次会谈。

  • 谈话可以在另一方结束的同时开始。

  • 一旦我们选择了一些会谈,我们就不能添加一个与已经选择的会谈不相容的演讲,因为它至少与这些先前选定的演讲中的一个重叠。

  • Example: We have a group of proposed talks with start and end times. Construct a greedy algorithm to schedule as many as possible in a lecture hall, under the following assumptions:

  • When a talk starts, it continues till the end.

  • No two talks can occur at the same time.

  • A talk can begin at the same time that another ends.

  • Once we have selected some of the talks, we cannot add a talk which is incompatible with those already selected because it overlaps at least one of these previously selected talks.

我们应该如何在算法的每一步做出“最佳选择”? 那就是我们选择哪个谈话?

  • 最早的会话是否已经与已经选定的会谈相提并论?

  • 那些已经兼容的话题中最短的话题?

  • 与已经选定的会谈相容的那些最早的谈话?

How should we make the “best choice” at each step of the algorithm? That is, which talk do we pick ?

  • Thetalkthatstartsearliestamongthosecompatible with already chosen talks?

  • The talk that is shortest among those already compatible?

  • The talk that end earliest among those compatible with already chosen talks?

  • 挑选最短的谈话不起作用。 开始时间:上午8:00

  谈话1

  结束时间:上午9点15分

开始时间:上午9:00

结束:上午10:00

开始时间:上午9:45对话3

谈话2

  结束:上午11:00

  • 你能在这里找到一个反例吗?

  • 但选择最快结束的那个确实有效。

我们选择Talk 2因为它最短,需要一个小时。 一旦我们选择Talk 2,我们不能选择Talk 1或Talk 3,因为它们都与Talk 2兼容。因此,这种贪婪算法只选择一个通话。 但是,可以选择两个兼容的会话,Talk 1和Talk 3。

  • Picking the shortest talk doesn’t work. Start: 8:00 AM

Talk 1

End:9:15 AM

Start: 9:00 AM

End: 10:00 AM

Start: 9:45 AM Talk 3

Talk 2

End: 11:00 AM

  • Can you find a counterexample here?

  • But picking the one that ends soonest does work.

We select Talk 2 because it is shortest, requiring one hour. Oncewe select Talk 2, we cannot select either Talk 1 or Talk 3 because neither is compatible with Talk 2. Hence, this greedy algorithm selects only one talk. However, it is possible to select two talks, Talk 1 and Talk 3, which are compatible.

解决方案:在每个步骤中,选择与所选内容兼容的会话中具有最早结束时间的会话。

1号房

2号房

谈话1上午8:00 - 9:15

讲话2上午9:00 - 10:00

谈话3上午9:45 - 11:00

Solution: At each step, choose the talks with the earliest ending time among the talks compatible with those selected.

Room 1

Room 2

Talk 1 8:00 – 9:15 a.m.

Talk 2 9:00 – 10:00 a.m.

Talk 3 9:45 - 11:00 a.m.

程序表(s1≤s2≤...≤sn:开始时间,e1≤e2≤...≤en:endtimes)

按完成时间排序并重新排序,使e1≤e2≤...≤enS:=∅

对于j:= 1ton

如果talk j与S兼容则S:=S∪{talk j}

返回S [S是预定的会谈集]

procedure schedule(s1 ≤ s2 ≤ ... ≤ sn : start times, e1 ≤e2 ≤...≤en:endtimes)

sort talks by finish time and reorder so that e1 ≤ e2 ≤ ... ≤ en S:= ∅

for j:=1ton

if talk j is compatible with S then S := S ∪ {talk j}

return S [ S is the set of talks scheduled]

发布了32 篇原创文章 · 获赞 40 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_27467365/article/details/84202131
今日推荐