2-3 算法分析与设计基础(分治算法)

2022-12-06
2023-01-10

Divide-and-conquer Algorithm 分治算法

理解分治算法的思想;
掌握Counting inversions问题的分治算法;
能用Master Theorem求解递归关系式,典型考题:作业1的第1题;

Divide-and-conquer paradigm 分治算法范式

★Divide-and-conquer 分治算法思想

Divide up problem into several subproblems (of the same kind).
将问题分成几个同类子问题。

Solve (conquer) each subproblem recursively.
递归地解决每个子问题。

Combine solutions to subproblems into overall solution.
将子问题的解决方案合并为整体解决方案。

Merging sort 合并排序

Most common usage, e.g., merging sort.
分治算法最常见的用法,例如,合并排序。

  • Divide problem of size n into two subproblems of size n / 2.
    将大小为n的问题分成大小为n/2的两个子问题。
  • Solve (conquer) two subproblems recursively.
    递归地解决两个子问题。
  • Combine two solutions into overall solution.
    将两种解决方案合并为整体解决方案。

Consequence.
时间复杂度结果

  • Brute force 暴力: Θ(n2).
  • Divide-and-conquer 分治: O(n log n).

求解递归式的时间复杂度1

证明合并排序问题分治算法的时间复杂度是O(n log n)

image-20221204093116254

求解递归式的时间复杂度2

image-20221205194301349

★问题 > Counting inversions 数逆序

Music site tries to match your song preferences with others.
音乐网站想要匹配你和他人的喜好

  • You rank n songs.
    您对n首歌曲进行排名
  • Music site consults database to find people with similar tastes.
    音乐网站查阅数据库,寻找有相似品味的人。

Similarity metric: number of inversions between two rankings.
相似性度量:两个排名之间的逆序次数

  • My rank 我的排名: 1, 2, …, n.
  • Your rank 你的排名: a1, a2, …, an.
  • Songs i and j are inverted if i < j, but ai > aj.
    如果歌曲i和歌曲j在我的排名中i < j,但是在你的排名中ai > aj,那么歌曲i和歌曲j是逆序
image-20221205193518656

Brute force: check all Θ(n2) pairs.
暴力算法:检查所有Θ(n2)对

Applications

  • Voting theory. 投票理论
  • Collaborative filtering. 协作过滤
  • Measuring the “sortedness” of an array. 测量数组的“排序”
  • Sensitivity analysis of Google’s ranking function. 谷歌排名功能的敏感性分析
  • Rank aggregation for meta-searching on the Web. Web上元搜索的排名聚合
  • Nonparametric statistics (e.g., Kendall’s tau distance). 非参数统计(例如Kendall’s tau距离)

Divide-and-conquer 分治算法

  • Divide: separate list into two halves A and B.
    分:将列表平分成A和B两部分
  • Conquer: recursively count inversions in each list.
    治:递归地求解每个列表的逆序
  • Combine: count inversions (a, b) with a ∈ A and b ∈ B.
    合:计算跨越A和B的逆序
  • Return sum of three counts.
    返回三部分之和
image-20221205194010085

How to combine two subproblems? 如何合并两个子问题?

Q. How to count inversions (a, b) with a ∈ A and b ∈ B?
问:如何计算跨越A和B的逆序

A. Easy if A and B are sorted!
答:如果A和B都拍好了序就好办了

Warmup algorithm. 预热算法

  • Sort A and B.
    对A和B排序
  • For each element b ∈ B,
    对于B中的每个元素
    • binary search in A to find how many elements in A are greater than b.
      在A中二分搜索有多少个元素大于B
image-20221205194246299

Count inversions (a, b) with a ∈ A and b ∈ B, assuming A and B are sorted.
假设A和B都拍好了序,计算跨越A和B的逆序:

  • Scan A and B from left to right.
    从左到右扫描A和B
  • Compare ai and bj.
    比较ai和bj
  • If ai < bj, then ai is not inverted with any element left in B.
    如果ai < bj,那么ai不会与B中剩余的任何元素形成逆序
  • If ai > bj, then bj is inverted with every element left in A.
    如果ai > bj,那么bj会与A中剩余的每个元素形成逆序
  • Append smaller element to sorted list C.
    将二者中更小的元素加入排好序的列表C

Divide-and-conquer algorithm implementation 分治算法实现

Sort-and-count algorithm 排序和计数算法

Input. List L.
输入:列表L

Output. Number of inversions in L and L in sorted order.
输出:L的逆序数和排好序的列表L

image-20221205220724723

时间复杂度分析

Proposition. The sort-and-count algorithm counts the number of inversions in a permutation of size n in O(n log n) time.
命题:对于大小为n的排列,排序和计数算法在O(n log n)时间内计算出逆序的个数

Pf. The worst-case running time T(n) satisfies the recurrence:
证明:最坏情况下的运行时间T(n)满足以下条件:

image-20221205221128762

问题 > Maximum Subarray Problem 最大连续子数组问题

Goal. Given an array x of n integer (positive or negative), find a contiguous subarray whose sum is maximum.
目标:给定n个整数(正或负)的数组x,找到一个和最大的连续子数组。

image-20221205221222389

分治解法:……

★Master theorem 主定理

Master theorem 主定理

image-20221205221416591 image-20221205221424607

完整版主定理

设a≥1a≥1和b>1b>1为常数,设f(n)f(n)为一函数,T(n)T(n)由以下递归式给出:

T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n)

对于非负整数定义,其中n/bn/b指⌊n/b⌋⌊n/b⌋或$⌈n/b⌉ 。则。则T(n)$可能有如下的渐近界:

  • 1)若对于某常数ε>0ε>0,有f(n)=O(nlog⁡ba−ε)f(n)=O(n^{\log_ba-ε}),则T(n)=Θ(nlog⁡ba)T(n)=Θ(n^{\log_ba})
  • 2)若f(n)=O(nlog⁡ba−ε)f(n)=O(n^{\log_ba-ε}),则T(n)=Θ(nlog⁡balog⁡n)T(n)=Θ(n^{\log_ba}\log n)
  • 3)若对于某常数ε>0ε>0,有f(n)=Ω(nlog⁡ba+ε)f(n)=Ω(n^{\log_ba+ε}),且存在常数c<1c<1使得对所有足够大的nn,有af(n/b)≤cf(n)af(n/b)≤cf(n),则T(n)=Θ(f(n))T(n)=Θ(f(n))
image-20221205221451095

理解主定理

image-20221205221506988 image-20221205221522775

Gaps in master theorem 主定理的空白

  • Number of subproblems is not a constant.
    子问题个数不是常数
  • Number of subproblems is less than 1.
    子问题个数小于1
  • Work to divide and combine subproblems is not Θ(nc).
    分开合并子问题的操作不是Θ(nc)复杂度的
image-20221205222003657

不能使用主定理的情况

image-20221205221633116

问题 > Integer multiplication 整数乘法

Integer addition and subtraction 整数加减法

image-20221205224126445

加法a+b的输入规模为a和b的位数n,而不是a或b

Integer multiplication 整数乘法

image-20221205224255808

Divide-and-conquer multiplication 分治乘法

image-20221205224405794 image-20221205224416661

Karatsuba trick (Karatsuba技巧)

image-20221205224732459

Karatsuba multiplication (Karatsuba乘法算法)

image-20221205224747331

Karatsuba analysis 分析Karatsuba算法

image-20221205224830875

问题 > Matrix multiplication 矩阵乘法

Dot product 点乘

image-20221205224928825

Matrix multiplication 矩阵乘法

image-20221205225124019

Block matrix multiplication 分块矩阵乘法

image-20221205230512416

Warmup 准备

分成四等分

image-20221205230525708

Strassen’s trick (Strassen的技巧)

image-20221205230549322 image-20221205231033984

Strassen’s algorithm (Strassen的算法)

image-20221205231105658

Analysis of Strassen’s algorithm 分析Strassen的算法

Theorem. Strassen’s algorithm requires O(n2.81) arithmetic operations to multiply two n-by-n matrices.

image-20221205231136452

问题 > Closest pair of points 最近点对

Closest pair of points 最近点对问题

Closest pair problem. Given n points in the plane, find a pair of points with the smallest Euclidean distance between them.

image-20221205231207206 image-20221205231230111

First attempt 初次尝试

Sorting solution. 排序

  • Sort by x-coordinate and consider nearby points.
  • Sort by y-coordinate and consider nearby points.

无法解决这种问题:

image-20221205231320011

Second attempt 二次尝试

Divide. Subdivide region into 4 quadrants.
分成4部分

Obstacle. Impossible to ensure n / 4 points in each piece.
无法保证平分

image-20221205231403883

Divide-and-conquer algorithm 分治算法思想

分成两部分

image-20221205231445873

How to find closest pair with one point in each side? 如何找到分布两边的最近点对

Find closest pair with one point in each side, assuming that distance < δ.

  • Observation: suffices to consider only those points within δ of line L.
  • Sort points in 2 δ-strip by their y-coordinate.
  • Check distances of only those points within 7 positions in sorted list!
image-20221205231600040 image-20221205231616699

Divide-and-conquer algorithm 分治算法

image-20221205231744525

时间复杂度:

image-20221205194301349

Refined version of closest-pair algorithm 最近点对算法的改良版

image-20221205231817277

问题 > 棋盘覆盖

问题描述

image-20221205230151448

分治算法

image-20221205230158459

文章目录

版权

本文遵循署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0) 标准。

部分内容源自互联网、书本和课堂,仅供交流学习,未用于任何商业用途。如果它们侵犯了您的权益,请与我们联系,我们将立即删除它们。

版权所有 © 2020-2026 Kloudy 保留所有权利.