๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ชฉ๋กdfs ๊ทธ๋ž˜ํ”„ (1)

Hey Tech

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS) ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž!(+Python ๊ตฌํ˜„)

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ DFS(Depth-First Search) ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ๐Ÿ“š ๋ชฉ์ฐจ 1. DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? 2. DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋™์ž‘ ๊ณผ์ • 3. DFS ํŒŒ์ด์ฌ ๊ตฌํ˜„ 1. DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€? DFS(Depth-First Search)๋Š” ๊ทธ๋ž˜ํ”„ ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•(i.e., ์™„์ „ ํƒ์ƒ‰) ์ค‘ ํ•˜๋‚˜๋กœ, '๊นŠ์ด'๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. DFS๋Š” ํ•œ ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘์œผ๋กœ ๋‹ค์Œ ๋ถ„๊ธฐ(branch)๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฏธ๋กœ ํƒ์ƒ‰ ์‹œ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋‹ค๊ฐ€ ๋” ์ด์ƒ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์—†๋Š” ๋…ธ๋“œ์— ์ด๋ฅด๋ €์„ ๋•Œ, ๋‹ค์‹œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐˆ๋ž˜๊ธธ๋กœ ๋Œ์•„๊ฐ€ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ์ด์–ด๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค...