2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

5 minute read

2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

Definition. Convex hull

The <convex hull> of a set $P$ of points in the plane is the smallest convex set containing $P$.

Equivalently, it is the largest convex polygon whose vertices are points in $P$.

  • inputs: a set $P = \{ p_1, p_2, \dots, p_n \}$ of points, where $p_i = (x_i, y_i)$

  • output: $(p_2, p_9, \dots, p_5, \dots, p_13)$, a representation of the convex hull

๐Ÿ’ฅ NOTE: We assume that the points in $P$ are in general position, meaning that no three points lie on a common line.


<Convex hull Algorithm>์„ ์‚ดํŽด๋ณด๊ธฐ ์ „์— ๋จผ์ € ์•„๋ž˜์˜ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž.

For three points $p$, $q$, $r$, how do we test whether $r$ lies to the left or to the right of the directed line $\vec{pq}$?

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์˜์™ธ๋กœ ๊ฐ„๋‹จํ•˜๋‹ค!! ๊ทธ๋ƒฅ $\vec{pq}$๋ฅผ ์ง€๋‚˜๋Š” ์ง์„ ๊ณผ $\vec{pr}$์„ ์ง€๋‚˜๋Š” ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉด ๋œ๋‹ค!!

\[\frac{q_y - p_y}{q_x - p_x} \quad \text{vs.} \quad \frac{r_y - p_y}{r_x - p_x}\]

์ด๋•Œ, ์‹์— ๋‚˜๋ˆ—์…ˆ์ด ์žˆ๋Š” ๊ฒƒ์€ โ€œdivide by zeroโ€์˜ ์œ„ํ—˜์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์‹์„ ๊ณฑ์…ˆ์˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

\[(r_x - p_x) \cdot (q_y - p_y) \quad \text{vs.} \quad (q_x - p_x) \cdot (r_y - p_y)\]

๋งŒ์•ฝ, ์  $r$์ด ์ง์„  $\vec{pq}$์˜ ์™ผ์ชฝ์— ์žˆ๋‹ค๋ฉด, ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋„ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, $\vec{pr}$์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ $\vec{pq}$ ๋ณด๋‹ค ํฌ๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ,

\[\frac{q_y - p_y}{q_x - p_x} \quad < \quad \frac{r_y - p_y}{r_x - p_x}\] \[(r_x - p_x) \cdot (q_y - p_y) \quad < \quad (q_x - p_x) \cdot (r_y - p_y)\]

Brute Force

์–ด๋–ค ๋‘ ์  $p$, $q$๊ฐ€ convex hull์„ ์ด๋ฃฌ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž, ๊ทธ๋Ÿฌ๋ฉด $\vec{pq}$์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ๋ชจ๋“  ์ ๋“ค์€ $\vec{pq}$์˜ ์˜ค๋ฅธํŽธ์— ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.

๋งŒ์•ฝ ์ด ๊ณผ์ •์„ naive ํ•˜๊ฒŒ ์ง„ํ–‰ํ•œ๋‹ค๋ฉด,

  1. Pick two points from points set: $\displaystyle\binom{n}{2}$

  2. Scan other points to determine they are in the right-side: $n$

๊ทธ๋ž˜์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” $O(n^3)$์ด ๋œ๋‹ค.


Grahamโ€™s Scan

$O(n^3)$ ์ˆ˜์ค€์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„์ง ๋งŽ์ด ๋Š๋ฆฌ๋‹ค. <Grahamโ€™s Scan>์€ points๋ฅผ ์ •๋ ฌํ•˜๊ณ , ๋˜ ๋ช‡๊ฐ€์ง€ ๊ทœ์น™์„ ํ†ตํ•ด $O(n^3)$๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ œ์‹œํ•œ๋‹ค.

Algorithm.

// initialization
Sort points in $P$ by x-coord and y-coord: $O(n \log n)$

Let $p, q, r$ be the leftmost three points in the sorted list $L$.

Repeat this:
โ€ƒโ€ƒ If $r$ lies to the right of $\vec{pq}$:
โ€ƒโ€ƒโ€ƒโ€ƒ we move one step forward in $L$.
โ€ƒโ€ƒ Otherwise:
โ€ƒโ€ƒโ€ƒโ€ƒ remove $q$ from the sorted list and move one step backward in $L$ if possible.

๊ธ€๋กœ ์‚ดํŽด๋ณด๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ–‰๋™์ด ์ž˜ ๊ทธ๋ ค์ง€์ง€ ์•Š๋Š”๋‹ค. ์ผ๋‹จ ์šฉ์–ด๋ฅผ ์กฐ๊ธˆ ๋‹ค๋“ฌ์œผ๋ฉด,

  • $r$๋Š” ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณด๋Š” ์ง์„  $\vec{pq}$์˜ ๋ฐ”๋กœ ๋‹ค์Œ ์ •๋ ฌ๋œ point
  • $q$๋Š” ์šฐ๋ฆฌ์˜ ์ง์„  $\vec{pq}$์˜ ๋์ ์ด๋‹ค.

  • โ€œone step forwardโ€๋ผ๋Š” ๋ง์€ $p$๋ฅผ acceptํ•˜๊ณ , ๋‹ค์Œ ์ •๋ ฌ๋œ point๋กœ ๋„˜์–ด๊ฐ€๋ผ๋Š” ๋ง์ด๋‹ค.
    // one step forward๊ฐ€ $r$์„ accept ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. $r$์€ ์–ธ์ œ๋“ ์ง€ ์ดํ›„์— reject ๋  ์ˆ˜ ์žˆ๋‹ค!
  • โ€œremove & one step backwardโ€๋Š” ํ˜„์žฌ์˜ $\vec{pq}$์—์„œ $q$๋ฅผ rejectํ•˜๊ณ , ๋งˆ์ง€๋ง‰์— ์‚ดํŽด๋ณธ 2๊ฐœ ์ ์œผ๋กœ ์ง์„ ์„ ๊ฐฑ์‹ 
    // ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๊ฒƒ์€ $r$์„ acceptํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค!

์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด, sorted list $L$์— convex hull์„ ์ด๋ฃจ๋Š” ์ ๋“ค๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค!! ๐Ÿ˜Ž


<Grahamโ€™s Scan>์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž. ๊ฐ ๊ณผ์ •์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜ ๋ณ„๋กœ ์ƒ๊ฐํ•ด๋ณธ๋‹ค๋ฉด,

  • rule (1)๋งŒ ๊ณ„์† ์ ์šฉ โ†’ ์ตœ๋Œ€ $n-2$๋ฒˆ ์ ์šฉ ๊ฐ€๋Šฅ!
  • ๋งŒ์•ฝ sorted list $L$์— convex hull์ด ์•„๋‹Œ ์ ๋“ค์ด ์žˆ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” rule (2)๋ฅผ ํ†ตํ•ด ๊ทธ๋Ÿฐ ์ ๋“ค์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ, rule (2)๋Š” ์ด $n-h$๋ฒˆ ์ ์šฉ๋œ๋‹ค! ($h$ = #. of upper convex hull points)


์ด๋ ‡๊ฒŒ <Grahamโ€™s Scran>์„ ํ•˜๊ฒŒ ๋˜๋ฉด, ์šฐ๋ฆฌ๋Š” convex hull์˜ ์œ„ ๊ป์งˆ์„ ์–ป๊ฒŒ ๋œ๋‹ค!! ๋‚˜๋จธ์ง€ ์•„๋ž˜ ๊ป์งˆ์€ sorted list์˜ ๋ฐ˜๋Œ€์—์„œ๋ถ€ํ„ฐ rule์„ ๋’ค์ง‘์–ด ์ ์šฉํ•˜๋ฉด ์–ป์„ ์ˆ˜ ์žˆ๋‹ค!!


์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ…Œํฌ๋‹‰ ์ค‘์—๋Š” <Convex hull trick>๋ผ๋Š” ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋‹ค. ์‚ฌ์‹ค ์ด๋ฆ„์— โ€œConvex hullโ€์ด ๋ถ™์—ˆ์ง€๋งŒ, <Convex hull algorithm>๊ณผ๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.


์ถ”์ฒœ ๋ฌธ์ œ

Categories:

Updated: