1 / 18

Ternary Search

Ternary Search. Concept, ICPC 4504. Convex Function and Concave Function. For any two points x 1 and x 2 and any t ∈ [0, 1 ] A function is concave if − f is convex. f ( tx 1 + (1 − t ) x 2 ) ≤ tf ( x 1 ) + (1 − t ) f ( x 2 ). f ( x ). f ( x 2 ). tf ( x 1 ) + (1 − t ) f ( x 2 ).

nanda
Download Presentation

Ternary Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ternary Search Concept, ICPC 4504

  2. Convex Function and Concave Function • For any two points x1 and x2 and any t ∈ [0, 1] • A function is concave if −f is convex f(tx1 + (1 − t)x2) ≤ tf(x1) + (1 − t)f(x2) f(x) f(x2) tf(x1) + (1 − t)f(x2) f(x1) f(tx1 + (1 − t)x2) x1 x2 tx1 + (1 − t)x2

  3. Examples f(x) = ax + b f(x) = x2 f(x) = |x|

  4. Finding the Minimum • 給予convex function f,並已知f在(low, up) 之間出現最小值,求值為何 • 像在具有單調性的函數上binary search 的方式,不斷丟棄不可能存在答案的區間來逼進答案 • ternary search 也是divide and conquer binary search ternary search f(mid) f(ml) t f(mr) low mid up low ml mr up f(mid) > t (目標的函數值) 答案不會在mid和up之間 f(ml) >f(mr) 最小值不會在low和ml之間

  5. Algorithm • 假設我們已經知道minimum 所在的區間為(l, r),任取兩點ml, mr,並且l < ml < mr < r,討論三種情況 • 不管mr切的位置在minimum 的哪一側,如果f(ml) > f(mr),答案一定不在 low和ml之間 (1) f(ml) > f(mr) f(ml) f(ml) f(mr) f(mr) ml mr ml mr

  6. Algorithm • 同樣地,如果f(ml) < f(mr),那麼mr到up之間這段就可以丟掉 (2) f(ml) < f(mr) f(mr) f(mr) f(ml) f(ml) ml mr ml mr

  7. Algorithm • 如果函數值相等,minimum 的範圍就可以縮小到ml到mr之間 • 這個case 可以和前面兩個任一合併,即在相等的時候選擇只丟掉左側或右側 (3) f(ml) = f(mr) f(ml) = f(mr) ml mr

  8. Examples of Implementation (I) doubleternary_search() { double low =kLeftBound, up =kRightBound, ml,mr; while(low +kEps< up) { ml = (low + up) /2,mr= (ml + up) /2; // or ml = (2 * low + up) / 3, mr= (low + 2 * up) / 3; if(f(ml) < f(mr)) up =mr; else low = ml; } return low; }

  9. Examples of Implementation (I) • 使用範圍大小來當迴圈的終止條件的缺點是,可能會因為浮點數的計算誤差陷入無窮迴圈 • 可以估計出大概切幾次之後範圍能縮小到能接受的大小,將迴圈改成固定的次數 • 每次縮小為1/2或3/4,以(3/4)k來推算迴圈的次數 • 每次縮小為2/3 ml = (low + up) /2,mr= (ml + up) /2; ml = (2* low + up) /3,mr= (low +2* up) /3;

  10. Examples of Implementation (II) doubleternary_search() { double low =kLeftBound, up =kRightBound, ml,mr; for(inti=0;i<kNumIterations; ++i) { ml = (low + up) /2,mr= (ml + up) /2; if(f(ml) < f(mr)) up =mr; else low = ml; } return low; }

  11. Properties • 兩個convex function 的sum 仍是一個convex function[?] • 兩個 convex function 的 max 仍是一個 convex function[?]

  12. ICPC 4504 • 平面上給定n個點(記為pi, 1 ≤ i ≤ n),求在x軸上找一點p,使最小,d為兩點在平面上的距離 f(p) = max{ d(p, pi) } f(p = p1) p1

  13. ICPC 4504 • 平面上給定n個點(記為pi, 1 ≤ i ≤ n),求在x軸上找一點p,使最小,d為兩點在平面上的距離 f(p) = max{ d(p, pi) } f(p = p2) p2

  14. Observation • 觀察當p在x軸上往正向移動時,其x座標對p到其中某一個給定點(xi, yi) 的函數圖形 • 不把根號開掉,實際上就是一個二次函數 yi xi (xi, yi)

  15. Observation • 有了各個點畫出來的函數fi(x),我們最後取的是F(x) = max{ fi(x) } • 因為每個 fi(x) 都是 convex function,而且 F(x) 是所有函數的 max,故 F(x) 也是一個 convex function F(x) yi yj xi xj (xi, yi) (xj, yj)

  16. Idea • 尋找以p點在原本座標系的x座標對最遠點距離所繪出的函數圖形,發生最小值時的x座標為何 • 選擇合理的起始範圍,並從這組左右界開始以ternary search 來逼進答案

  17. Appendix • 兩個convex function 的sum 仍是一個convex function 令 h(x) = f(x) + g(x),其中 f(x) 和 g(x) 是 convex functions 任取兩點 x1和 x2以及 t ∈ [0, 1] h(tx1 + (1 − t)x2) = f(tx1 + (1 − t)x2) + g(tx1 + (1 − t)x2) ≤ tf(x1) + (1 − t)f(x2) + tg(x1) + (1 − t)g(x2) = t[f(x1) + g(x1)] + (1 − t)[f(x2) + g(x2)] = th(x1) + (1 − t)h(x2)

  18. Appendix • 兩個 convex function 的 max 仍是一個 convex function 令 h(x) = max{ f(x), g(x) },其中 f(x) 和 g(x) 是 convex functions 任取兩點 x1和 x2以及 t ∈ [0, 1] f(tx1+ (1 − t)x2) ≤ tf(x1) + (1 − t)f(x2) g(tx1+ (1 − t)x2) ≤ tg(x1) + (1 − t)g(x2) tf(x1) + (1 − t)f(x2) ≤ th(x1) + (1 − t)h(x2) tg(x1) + (1 − t)g(x2) ≤ th(x1) + (1 − t)h(x2) h(tx1 + (1 − t)x2) = max{f(tx1 + (1 − t)x2), g(tx1 + (1 − t)x2) } ≤ max{tf(x1) + (1 − t)f(x2), tg(x1) + (1 − t)g(x2) } ≤ th(x1) + (1 − t)h(x2)

More Related