Skip to content

Latest commit

ย 

History

History
36 lines (21 loc) ยท 1.05 KB

Algo-BFS & DFS.md

File metadata and controls

36 lines (21 loc) ยท 1.05 KB

BFS & DFS

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS)

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS) ์€ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐ™์€ ๊นŠ์ด์— ํ•ด๋‹นํ•˜๋Š” ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

BFS

BFS์˜ ํŠน์ง•

  • Queue๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ์ž‘ ์ง€์ ์—์„œ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
  • V๊ฐ€ ์ •์ ์˜ ์ˆ˜, E๊ฐ€ ๊ฐ„์„ ์˜ ์ˆ˜์ผ ๋•Œ BFS์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(V + E)์ž…๋‹ˆ๋‹ค.

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS) ์€ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ๋Œ€ํ•œ ์‹ถ์€ ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

DFS

DFS์˜ ํŠน์ง•

  • Stack์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ์ž‘ ์ •์ ์—์„œ ๊นŠ์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ฐพ์Šต๋‹ˆ๋‹ค.
  • V๊ฐ€ ์ •์ ์˜ ์ˆ˜, E๊ฐ€ ๊ฐ„์„ ์˜ ์ˆ˜์ผ ๋•Œ DFS์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(V + E)์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

https://programmers.co.kr/learn/courses/13213

์ƒˆ ์ฐฝ ์—ด๊ธฐ ๋ฐฉ๋ฒ• : CTRL+click (on Windows and Linux) | CMD+click (on MacOS)