Divide-and-Conquer

本文深入探讨了分治算法的基本概念及其应用,包括排序算法(如归并排序、快速排序)、矩阵乘法、寻找最近点对等问题,并分析了不同场景下算法的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分治通常是用来降低用暴力解法已经能达到多项式时间复杂度的时间复杂度,结合randomization technique是powerful。
- Divide a problem into a number of independent sub-problems
- Conquer the subproblems by solving them recursively;
- Combine the solutions to the subproblems into the solution to the original problem;

复杂度分析

可分

问题的数据结构为:有相同的结构,但是smaller

  • An array with n elements;
  • A matrix;
  • A set of n elements;
  • A tree;
  • A directed acyclic graph;
  • A general graph;

sort

InsertionSort:Divide n element into a n−1-length array and an element

MergeSort:Divide into two halves


QuickSort:divide according to a randomly-selected pivot

随机选择pivot,会出现最坏情况pivot把number全部分到一个集合里,最好情况是各分一半,大多数情况pivot是nearly-central

为了使pivot基本分成2半,可以让算法强行选择一个比较合适的pivot,适用于distinct number,比上面的方法更慢。

由于大多数的pivot都是good,所以randomly选择pivot的时间复杂度是 O(nlog43n)

Lomuto’s implementation

Hoare’s implementation [1961]

前面的方法对于distinc item是有效的,但是当有许多重复元素时,可考虑将元素分成3parts。

Selection:select the k-th smallest items in an array


pivot的选择会导致不同的时间复杂度,当选择median或者nearly-central时是(n),否则为 O(n2)


CountingInversion: to count inversions in an array of n integers


可以将CountingInversion问题当成排序问题。

MatrixMultiplication

通过减少子问题个数降低时间复杂度

ClosestPair problem

如果在一维直线上,通过排序可以得到最近点对。
二维平面上先按照x排序,等分成2部分。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值