Python Data Structure 简明教程

Python - Divide and Conquer

在分治法中,手头的问题被划分为更小的子问题,然后独立解决每个问题。当我们继续将子问题划分为更小的子问题时,最终可能会达到无法进一步划分的阶段。解决这些“原子”的最小可能子问题(部分)。所有子问题的解最终都会合并,以获得原始问题的解。

In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and then each problem is solved independently. When we keep on dividing the subproblems into even smaller sub-problems, we may eventually reach a stage where no more division is possible. Those "atomic" smallest possible sub-problem (fractions) are solved. The solution of all sub-problems is finally merged in order to obtain the solution of an original problem.

divide and conquer

从广义上讲,我们可以通过三个步骤理解 @ {s0} 方法。

Broadly, we can understand divide-and-conquer approach in a three-step process.

Divide/Break

此步骤涉及将问题分解为更小的子问题。子问题应表示原始问题的一部分。此步骤通常采用递归方法来划分问题,直到无法进一步划分子问题。在此阶段,子问题本质上变成了原子,但仍然表示实际问题的一部分。

This step involves breaking the problem into smaller sub-problems. Sub-problems should represent a part of the original problem. This step generally takes a recursive approach to divide the problem until no sub-problem is further divisible. At this stage, sub-problems become atomic in nature but still represent some part of the actual problem.

Conquer/Solve

此步骤接收许多较小的子问题以求解。通常,在此级别上,问题被认为自己“已解决”。

This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the problems are considered 'solved' on their own.

Merge/Combine

当较小的子问题得到解决时,此阶段将它们循环组合,直至形成原问题的解决方案。这种算法方法按递归方式工作,而且拆分和合并步骤非常接近,看上去它们好像是一样的。

When the smaller sub-problems are solved, this stage recursively combines them until they formulate a solution of the original problem. This algorithmic approach works recursively and conquer &s; merge steps works so close that they appear as one.

Examples

下面的程序是 divide-and-conquer 编程方法的一个示例,其中使用 Python 实现了二分搜索。

The following program is an example of divide-and-conquer programming approach where the binary search is implemented using python.

Binary Search implementation

在二分搜索中,我们选取一个元素的有序列表,并开始在该列表的中间位置查找元素。如果搜索值与列表中的中间值匹配,则完成搜索。否则,我们将根据被搜索项目的值选取列表的右半部分或左半部分,从而消除列表中一半的元素。

In binary search we take a sorted list of elements and start looking for an element at the middle of the list. If the search value matches with the middle value in the list we complete the search. Otherwise we eleminate half of the list of elements by choosing whether to procees with the right or left half of the list depending on the value of the item searched.

这是可行的,因为列表是有序的,并且它比线性搜索快得多。在这里,我们根据被搜索项目的值选取列表的右半部分或左半部分,从而分离给定的列表并征服它。我们将重复这种做法,直到找到该元素,或推断出它不在列表中。

This is possible as the list is sorted and it is much quicker than linear search.Here we divide the given list and conquer by choosing the proper half of the list. We repeat this approcah till we find the element or conclude about it’s absence in the list.

Example

def bsearch(list, val):
   list_size = len(list) - 1
   idx0 = 0
   idxn = list_size
# Find the middle most value
   while idx0 <= idxn:
      midval = (idx0 + idxn)// 2
      if list[midval] == val:
         return midval
# Compare the value the middle most value
   if val > list[midval]:
      idx0 = midval + 1
   else:
      idxn = midval - 1
   if idx0 > idxn:
      return None
# Initialize the sorted list
list = [2,7,19,34,53,72]

# Print the search result
print(bsearch(list,72))
print(bsearch(list,11))

Output

执行上述代码后,将生成以下结果 −

When the above code is executed, it produces the following result −

5
None