์•Œ๊ณ ๋ฆฌ์ฆ˜ 18

[Algorithm] ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (Breadth-First Search)

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS; Breadth First Search) ์ •์ ๋“ค๊ณผ ๊ฐ™์€ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค(ํ˜•์ œ ๋…ธ๋“œ)์„ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹ A - B - C - D - G - H - I - E - F - J ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS; Depth First Search) ์ •์ ์˜ ์ž์‹๋“ค์„ ๋จผ์ € ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹ A - B - D - E - F - C - G - H - I - J

[Algorithm] ์ˆœ์ฐจ ํƒ์ƒ‰ (Sequential Search)

์ˆœ์ฐจ ํƒ์ƒ‰ (Sequential Search) ? - ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธ - ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•ด์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ• data = list() for num in range(10): data.append(randint(1, 100)) #random ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ def sequencial(data, search_data): for index in range(len(data)): if data[index] == search_data: return index return -1 sequencial(data, 4)

[Algorithm] ์ด์ง„ ํƒ์ƒ‰ (Binary Search)

์ด์ง„ ํƒ์ƒ‰ (Binary Search) ? ํƒ์ƒ‰ํ•  ์ž๋ฃŒ๋ฅผ ๋‘˜๋กœ ๋‚˜๋ˆ„์–ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„๋งŒํ•œ ๊ณณ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ด์ง„ ํƒ์ƒ‰ ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Divide and Conquer) Divide : ๋ฌธ์ œ๋ฅผ ํ•˜๋‚˜ ๋˜๋Š” ๋‘˜ ์ด์ƒ์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค. Conquer : ๋‚˜๋ˆ ์ง„ ๋ฌธ์ œ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘๊ณ , ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋ฉด ํ•ด๊ฒฐํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋‹ค์‹œ ๋‚˜๋ˆˆ๋‹ค. ์ด์ง„ ํƒ์ƒ‰ Divide : ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜๋ˆˆ๋‹ค. Conquer ๊ฒ€์ƒ‰ํ•  ์ˆซ์ž(search) > ์ค‘๊ฐ„๊ฐ’ , ๋’ท ๋ถ€๋ถ„์˜ ์„œ๋ธŒ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฒ€์ƒ‰ํ•  ์ˆซ์ž๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ฒ€์ƒ‰ํ•  ์ˆซ์ž(search) < ์ค‘๊ฐ„๊ฐ’ , ์•ž ๋ถ€๋ถ„์˜ ์„œ๋ธŒ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฒ€์ƒ‰ํ•  ์ˆซ์ž๋ฅผ ์ฐพ๋Š”๋‹ค. def binary_search(data, search): if len(data) == ..

[Algorithm] ๋ณ‘ํ•ฉ ์ •๋ ฌ(merge sort)

๋ณ‘ํ•ฉ ์ •๋ ฌ(merge sort) ? ์žฌ๊ท€ ์šฉ๋ฒ•์„ ํ™œ์šฉํ•œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ž˜๋ผ ๋น„์Šทํ•œ ํฌ๊ธฐ์˜ ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜๋ˆ” ๊ฐ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ•ฉ๋ณ‘ ์ •๋ ฌ์„ ์ด์šฉํ•ด ์ •๋ ฌ ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ๋ณ‘ def merge(left, right): merged = list() left_point, right_point = 0,0 #case1: left/right ๋‘˜๋‹ค ๋ฐ์ดํ„ฐ ์žˆ์„ ๋•Œ while len(left) > left_point and len(right) > right_point: it left[left_point] > right[right_point]: merged.append(right[right_point]) right_point += 1 else: merged...

[Algorithm] ํ€ต ์ •๋ ฌ(Quick Sort)

ํ€ต ์ •๋ ฌ(quick sort) ? ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฝƒ ๊ธฐ์ค€์ (pivot)์„ ์ •ํ•ด์„œ, ๊ธฐ์ค€์ ๋ณด๋‹ค ์ž‘์€ ๋ฐ์ดํ„ฐ๋Š” ์™ผ์ชฝ, ํฐ ๋ฐ์ดํ„ฐ๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ชจ์œผ๋Š” ํ•จ์ˆ˜ ๊ฐ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์€ ์žฌ๊ท€์šฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์‹œ ๋™์ผ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์œ„ ์ž‘์—… ๋ฐ˜๋ณต ํ•จ์ˆ˜๋Š” ์™ผ์ชฝ(left) + ๊ธฐ์ค€์ (pivot) + ์˜ค๋ฅธ์ชฝ(right)์„ ๋ฆฌํ„ด def qsort(data): if len(data) data[index]: left.append(data[index]) else: right.append(data[index]) #2.list comprehension ์‚ฌ์šฉ left = [item for item in data[1:] if pivot > item] right = [item for item in data[1:] if pivot

๋™์  ๊ณ„ํš๋ฒ•( Dynamic Programming) ๊ณผ ๋ถ„ํ•  ์ •๋ณต(Divide Conquer)

๋™์  ๊ณ„ํš๋ฒ• ( DP; Dynamic Programming) ? ์ž…๋ ฅ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•œ ํ›„, ํ•ด๋‹น ๋ถ€๋ถ„ ๋ฌธ์ œ์˜ ํ•ด๋ฅผ ํ™œ์šฉํ•ด ๋ณด๋‹ค ํฐ ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ์ƒํ–ฅ์‹ ์ ‘๊ทผ๋ฒ• Memoization ๊ธฐ๋ฒ• : ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ, ์ด์ „์— ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ์ €์žฅํ•˜์—ฌ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋„๋ก ํ•จ -> ์‹คํ–‰์†๋„ ํ–ฅ์ƒ ๋ฌธ์ œ๋ฅผ ์ž˜๊ฒŒ ์ชผ๊ฐค ๋•Œ, ๋ถ€๋ถ„ ๋ฌธ์ œ๋Š” ์ค‘๋ณต๋˜์–ด ์žฌํ™œ์šฉ๋จ -> ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๋ถ„ํ•  ์ •๋ณต (Divide and Conquer ) ? ๋ฌธ์ œ๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋‚˜๋ˆ„์–ด์„œ, ๊ฐ๊ฐ ํ’€๋ฉด์„œ ๋‹ค์‹œ ํ•ฉ๋ณ‘ํ•˜์—ฌ ๋ฌธ์ œ์˜ ๋‹ต์„ ์–ป๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•˜ํ–ฅ์‹ ์ ‘๊ทผ๋ฒ• (์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„) ๋ฌธ์ œ๋ฅผ ์ž˜๊ฒŒ ์กฐ๊ฐค ๋•Œ, ๋ถ€๋ถ„ ๋ฌธ์ œ๋Š” ์„œ๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š์Œ ๊ณตํ†ต์ ๊ณผ ์ฐจ์ด์  ๊ณตํ†ต์  : ๋ฌธ์ œ๋ฅผ ์ž˜๊ฒŒ ์ชผ๊ฐœ์„œ, ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„๋กœ ๋ถ„ํ•  ์ฐจ์ด์  ๋™..

[Algorithm] ์žฌ๊ท€ ์šฉ๋ฒ• (recursive call, ์žฌ๊ท€ ํ˜ธ์ถœ)

์žฌ๊ท€ ์šฉ๋ฒ•(recursive call) ? - ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋™์ผํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ - ์—ฌ๋Ÿฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉ ์žฌ๊ท€ํ˜ธ์ถœ์˜ ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ ๋‚ด๋ถ€์ ์œผ๋กœ ์Šคํƒ(Stack)์ฒ˜๋Ÿผ ๊ด€๋ฆฌ๋œ๋‹ค #case1 def function(์ž…๋ ฅ): if ์ž…๋ ฅ > ์ผ์ • ๊ฐ’: # ์ž…๋ ฅ์ด ์ผ์ • ๊ฐ’ ์ด์ƒ์ด๋ฉด return function(์ž…๋ ฅ - 1) # ์ž…๋ ฅ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’ else: return ์ผ์ • ๊ฐ’, ์ž…๋ ฅ ๊ฐ’, ๋˜๋Š” ํŠน์ • ๊ฐ’ # ์žฌ๊ท€ํ˜ธ์ถœ ์ข…๋ฃŒ #case2 def function(์ž…๋ ฅ): if ์ž…๋ ฅ

[Algorithm] ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Sort Algorithm)

์ •๋ ฌ (Sorting) ? - ์–ด๋–ค ๋ฐ์ดํ„ฐ๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ - ์ •๋ ฌ์€ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ ์‹œ, ๋นˆ๋ฒˆํ•˜๊ฒŒ ํ•„์š” - ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ณ ์•ˆ๋˜์—ˆ์œผ๋ฉฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต์˜ ํ•„์ˆ˜ ๋ฒ„๋ธ” ์ •๋ ฌ(bubble sort) : ๋‘ ์ธ์ ‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•ด์„œ, ์•ž์—์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋’ค์—์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํฌ๋ฉด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟˆ def bubblesort(data): for index in range(len(data) - 1 ): swap = False for index2 in range(len(data) - index - 1): if data[index2] > data[index2 + 1]: data[index2], data[index2 + 1] = data[index2 + 1], data[index2] swap..

[Data Structure] ํž™ (Heap)์˜ ๊ตฌํ˜„ - ํž™๊ณผ ๋ฐฐ์—ด

ํž™๊ณผ ๋ฐฐ์—ด ์ผ๋ฐ˜์ ์œผ๋กœ ํž™ ๊ตฌํ˜„ ์‹œ, ๋ฐฐ์—ด ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, ํž™ ๊ตฌํ˜„์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด root ๋…ธ๋“œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ 1๋กœ ์ง€์ • ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) = ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(child node's index) //2 ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(left child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 ์˜ค๋ฅธ์ชฝ ์ž์‹  ๋…ธ๋“œ ์ธ๋ฑ์Šค(right child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 + 1 ํž™๊ณผ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…(Max Heap) class Heap: def __init__(self, data): self.heap_array = list() self.h..

[Data Structure] ํž™ (Heap)

ํž™ (Heap) ? ๋ฐ์ดํ„ฐ์—์„œ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ(Complete Binary Tree) - ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ : ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ, ์ตœํ•˜๋‹จ ์™ผ์ชฝ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•˜๋Š” ํŠธ๋ฆฌ ํž™์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ , ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์œผ๋ ค๋ฉด O(n)์ด ๊ฑธ๋ฆผ