Design and Analysis of Algorithms (Divide & Conquer: Convex Hull, Median Finding)

Paradigm

Given a problem of size n divide it into subproblems of size n,  a ≥ 1, b > 1. Solve each subproblem recursively. Combine solutions of subproblems to get the overall solution.

T(n) = aT(n/b ) + [work for merge]

Convex Hull

Given n points in plane

S = {(xi, yi) | i = 1, 2,...,n}

assume no two have the same x coodinates, no two have sa`me y coordinates, no three in a line.

Convex Hull: smallest polygon contain all points in the plane. 

CH(S) represented by the sequence of points on the boundary in clockwise order as a doubly linked list.

Brute force for Convex Hull

Test each line segment to see if it makes up an edge of the convex hull

  • If the rest of the points are on one side of the segment, the segment is on the convex hull.
  • else the segment is not.
  • O(n2) edges, O(n) tests ⇒ O(n3) complexity

Divide and Conquer Convex Hull

Sort points by x coordinates

  1. divide into left half A and right half B by x coords
  2. compute CH(A) and CH(B)
  3. combine CH's of two halves (merge step)

How to merge?

  • Find the upper tangent (ai, bj).
  • Find the lower tangent (ak, bm).
  • Cut and paste in time Θ(n).

Firstly,  link ai to bj, go down b list till you see bm and link bm to ak, continue along the a list until you return to ai.

Find Tangents

Assume ai maximizes x within CH(A) (a1, a2,...,ap). b1 minimizes x within CH(B) (b1, b2,..., bq)

L is the vertical line separating A and B. Define y(i, j) as y-coordinate of intersection between L and segment (ai, bj ).

Claim: (ai, bj ) is upper tangent if it maximizes y(i, j)

If y(i, j) is not maximum, there will be points on both sides of (ai, bj ) and it cannot be a tangent.

Algorithm: Obvious O(n2) algorithm looks at all ai, bj pairs. T(n)=2T(n/2)+ Θ(n^2) = Θ(n^2).

i = 1
j = 1
while(y(i, j+1) > y(i, j) or y(i − 1, j) > y(i, j))
    if (y(i, j + 1) > y(i, j)) -> move right finger clockwise
        j = j + 1( mod q)
    else
        i = i - 1( mod p) -> move left finger anti-cloclwise
    return (ai, bj ) as upper tangent

T(n)=2T(n/2) + Θ(n) = Θ(n log n) 

Median Finding

Given a set of n numbers, define rank(x) as the number of numbers in the set that are ≤ x. Find an element of rank\left \lfloor \frac{ n+1 }{2} \right \rfloor (lower median) and \left \lceil \frac{n + 1}{2}\right \rceil (upper median).

Select(S, i)
    Pick x in S
    Compute k = rank(x)
    B = {y ∈ S|y<x}
    C = {y ∈ S|y>x}
    if k = i
        return x
    else if k > i
        return Select(B, i)
    else if k < i
        return Select(C, i - k)
        

Picking x Cleverly

Need to pick x so rank(x) is not extreme.

  • Arrange S into columns of size 5 (n/5 cols)
  • Sort each column (bigger elements on top) (linear time)
  • Find “median of medians” as x
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书用Python语言来讲解算法的分析和设计 分别介绍了树、图、计数问题、归纳递归、遍历、分解合并、贪心算法、复杂依赖、Dijkstra算法、匹配切割问题以及困难问题及其稀释等内容 Python Algorithms explains the Python approach to algorithm analysis and design. Written by Magnus Lie Hetland, author of Beginning Python, this book is sharply focused on classical algorithms, but it also gives a solid understanding of fundamental algorithmic problem-solving techniques. * The book deals with some of the most important and challenging areas of programming and computer science, but in a highly pedagogic and readable manner. * The book covers both algorithmic theory and programming practice, demonstrating how theory is reflected in real Python programs. * Well-known algorithms and data structures that are built into the Python language are explained, and the user is shown how to implement and evaluate others himself. What you'll learn * Transform new problems to well-known algorithmic problems with efficient solutions, or show that the problems belong to classes of problems thought not to be efficiently solvable. * Analyze algorithms and Python programs both using mathematical tools and basic experiments and benchmarks. * Prove correctness, optimality, or bounds on approximation error for Python programs and their underlying algorithms. * Understand several classical algorithms and data structures in depth, and be able to implement these efficiently in Python. * Design and implement new algorithms for new problems, using time-tested design principles and techniques. * Speed up implementations, using a plethora of tools for high-performance computing in Python. Who this book is for The book is intended for Python programmers who need to learn about algorithmic problem-solving, or who need a refresher. Students of computer science, or similar programming-related topics, such as bioinformatics, may also find the book to be quite useful. Table of Contents * Introduction * The Basics * Counting 101 * Induction and Recursion ...and Reduction * Traversal: The Skeleton Key of Algorithmics * Divide, Combine, and Conquer * Greed Is Good? Prove It! * Tangled Dependencies and Memoization * From A to B with Edsger and Friends * Matchings, Cuts, and Flows * Hard Problems and (Limited) Sloppiness

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值