ํฌ์ŠคํŠธ

Binary-Search

Binary-Search

์ด์ง„ํƒ์ƒ‰, ์ด๋ถ„ํƒ์ƒ‰.
์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด, ํŠน์ • ์›์†Œ๋ฅผ Log N ์•ˆ์— ์ฐพ์•„๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ๋“ฌ

๐Ÿ’ซ Use


  • Data-Structure-Binary-Search-Tree

  • ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์—์„œ ์ด๋ฆ„์ด K๋กœ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ ์ฐพ๊ธฐ
    • ์ฑ… ํ•œ๊ฐ€์šด๋ฐ๋ฅผ ํŽผ์น˜๊ณ  ์ฐพ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•
      • ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋Œ€๋กœ๋ผ๋ฉด ์ค‘๊ฐ„์ฏค์— K๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์ด ์žˆ์„ ํ…Œ๋‹ˆ๊นŒ
  • ์‚ฌ์ „์—์„œ ๋‹จ์–ด ์ฐพ๊ธฐ
  • ํŽ˜์ด์Šค๋ถ์— ๋กœ๊ทธ์ธํ•˜๊ธฐ (ํŽ˜์ด์Šค๋ถ์ด ๋‚ด ๊ณ„์ •์ด ์‹ค์กดํ•˜๋Š” ๊ณ„์ •์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์•„์ด๋”” ์ฐพ๊ธฐ)

๐Ÿ’ซ ์™œ Why


Updown ๊ฒŒ์ž„.
์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ˆซ์ž ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š”, ๋” ์ข‹์€ ๋ฐฉ๋ฒ•.

ํƒ์ƒ‰์‹œ๊ฐ„์„ค๋ช…
์„ ํ˜•ํƒ์ƒ‰O(n)๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰
์ด์ง„ํƒ์ƒ‰O(log n)์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ ˆ๋ฐ˜์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ํƒ์ƒ‰
  • ๋ฆฌ์ŠคํŠธ์— ์ˆซ์ž N๊ฐœ ์žˆ๋‹ค๋ฉด
    • ์„ ํ˜•ํƒ์ƒ‰: ์ตœ์•…์˜ ๊ฒฝ์šฐ N๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•ด์•ผ ํ•จ.
    • ์ด์ง„ํƒ์ƒ‰: ์ตœ์•…์˜ ๊ฒฝ์šฐ logN๋งŒ ํ™•์ธํ•˜๋ฉด ๋จ.
  • if N = 1,024
    • ์„ ํ˜•ํƒ์ƒ‰: ์ตœ์•…์˜ ๊ฒฝ์šฐ 1,024
    • ์ด์ง„ํƒ์ƒ‰: ์ตœ์•…์˜ ๊ฒฝ์šฐ log21,024 = 10

๐Ÿ’ซ ๊ตฌํ˜„


๐Ÿซง ์ฐพ๋Š” ๊ฐ’์ด ํ•˜๋‚˜

low/high (or start/end).
์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ์„ ๊ฑฐ๋ผ๊ณ  ์˜ˆ์ƒํ•˜๋Š” ๋ฒ”์œ„.
(๊ฐ’์ด ๋ฆฌ์ŠคํŠธ ์ž์ฒด์— ์—†์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ƒ ๋ฒ”์œ„)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int binarySearch(int arr[], int target, int size)
{
	// ์ฒ˜์Œ์—๋Š” ๋ชจ๋“  ๋ฒ”์œ„๋ฅผ ํƒ์ƒ‰
	int low = 0;
	int high = size - 1;

	// ์„œ๋กœ ์œ„์น˜๊ฐ€ ์—ญ์ „๋˜๋ฉด, ์˜ˆ์ƒ ๋ฒ”์œ„๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ.
	while (low <= high)
	{
		// ์ ˆ๋ฐ˜์”ฉ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ ๋‚˜๊ฐ€๋Š”
		// ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” ๋‚ด๋ฆผ
		int mid = (low + high) / 2;

		if (arr[mid] == target)
			return mid;
		else if (arr[mid] < target)
			low = mid + 1;
		else
			high = mid - 1;
	}

	return -1;
}

๐Ÿซง ์ฐพ๋Š” ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ

์ด๋ถ„ ํƒ์ƒ‰ ํ›„ ์ฐพ์€ ์œ„์น˜์—์„œ ์–‘ ์˜†์œผ๋กœ ํ™•์žฅํ•ด๋„ ๋˜์ง€๋งŒ,
์ตœ์•…์˜ ๊ฒฝ์šฐ O(N)์ด ๋  ์ˆ˜ ์žˆ์Œ. (์ „๋ถ€ ๋‹ค ๊ฐ™์€ ๊ฐ’์ผ ๊ฒฝ์šฐ)

๋Œ€์‹ ,
๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค - ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฐ’ ์ธ๋ฑ์Šค + 1 = ๊ฐœ์ˆ˜

๋‹ค๋ฅด๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด,
์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ์‚ฝ์ž…ํ–ˆ์„ ๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์œ ์ง€๋˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ.
์˜ค๋ฆ„์ฐจ์ˆœ์ด ์œ ์ง€๋˜๋Š” ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์œ„์น˜ - ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์œ ์ง€๋˜๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„์น˜ = ๊ฐœ์ˆ˜

๋ฐ”ํ‚น๋…๋‹˜ ์ด๋ฏธ์ง€

๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค
= ์‚ฝ์ž…ํ–ˆ์„ ๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์œ ์ง€๋˜๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„์น˜.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int lowerBound(int arr[], int target, int size)
{
	int low = 0;
	int high = size;

	while (low < high)
	{
		int mid = (low + high) / 2;

		if (arr[mid] >= target)
			high = mid;
		else
			low = mid + 1;
	}

	return low;
}

๋ฐ”ํ‚น๋…๋‹˜ ์ด๋ฏธ์ง€

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค
= ์‚ฝ์ž…ํ–ˆ์„ ๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์œ ์ง€๋˜๋Š” ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์œ„์น˜.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int upperBound(int arr[], int target, int size)
{
	int low = 0;
	int high = size;

	while (low < high)
	{
		int mid = (low + high) / 2;

		if (arr[mid] > target)
			high = mid;
		else
			low = mid + 1;
	}

	return low;
}

๐Ÿ’ซ Memo


  • C++ STL
    • binary_search ํ•จ์ˆ˜
      • ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ.
      • ์š”์†Œ ์œ ๋ฌด๋ฅผ true/false๋กœ ๋ฐ˜ํ™˜
    • lower_bound ํ•จ์ˆ˜
      • ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’ ์ด์ƒ์ด ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜
    • upper_bound ํ•จ์ˆ˜
      • ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’ ์ดˆ๊ณผ๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜
    • equal_range ํ•จ์ˆ˜
      • lower_bound์™€ upper_bound๋ฅผ ํ•œ ๋ฒˆ์— ํ˜ธ์ถœ
      • pair๋กœ ๋ฐ˜ํ™˜
  • Parametric Search ์ฒ˜๋Ÿผ STL๊ฐ€ ๋„์›€์ด ์•ˆ๋˜๊ณ  ์ง์ ‘ ์ด๋ถ„ํƒ์ƒ‰์„ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.
    • ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜
  • ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง€์ง€ ์•Š์œผ๋ ค๋ฉด
    • ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฐฉ์ง€: int mid = low + (high - low) / 2;
    • ๊ท ๋“ฑํ•˜๊ฒŒ ๋‚˜๋ˆ„๊ธฐ: mid = (high - low + 1) / 2;
  • TODO:
์ด ๊ธฐ์‚ฌ๋Š” ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.