Skip to content

Latest commit

ย 

History

History
91 lines (47 loc) ยท 1.87 KB

DFS&BFS.md

File metadata and controls

91 lines (47 loc) ยท 1.87 KB

DFS ์™€ BFS

1. ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS: Depth-First Search)

  • ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ฐ€๋‹ค๊ฐ€ ๋” ์ด์ƒ ๊ฐˆ ์ˆ˜ ์—†์„ ๋•Œ ๊ฐ€์žฅ ์ตœ๊ทผ ๊ฐˆ๋ฆผ๊ธธ๋กœ ๋Œ์•„์™€ ๋‹ค๋ฅธ ๊ธธ์„ ์„ ํƒํ•˜์—ฌ ๋‹ค์‹œ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰
  • ์Šคํƒ ๋˜๋Š” ์žฌ๊ท€ํ•จ์ˆ˜(๊ฐ€์žฅ ๋ณดํŽธ์ ์ด๊ณ  ์ฝ”๋“œ๊ฐ€ ์งง์Œ) ์ด์šฉ

[์ถœ์ฒ˜ : https://developer-mac.tistory.com/64]

๋™์ž‘ ๋ฐฉ์‹



๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„ - ์ธ์ ‘ํ–‰๋ ฌ

๊ฐ„์„ ์œผ๋กœ ์ด์–ด์ง„ ์ •์  i,j๊ฐ€ ๊ทธ๋ž˜ํ”„์— ์กด์žฌํ•œ๋‹ค๋ฉด?

M[i][j] = 1

์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด?

M[i][j] = 0

์žฅ์ 

  • ๊ตฌํ˜„์ด ๊ฐ„๋‹จ
  • ์ •์ ๋ผ๋ฆฌ์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ํ™•์ธ ๊ฐ€๋Šฅ

๋‹จ์ 

  • ๋ฉ”๋ชจ๋ฆฌ์˜ ๋ถ€๋‹ด : ๊ฐ„์„ ์˜ ์ˆ˜์™€๋Š” ๊ด€๊ณ„ ์—†์ด n^2์˜ 2์ฐจ์› ๋ฐฐ์—ด์ด ํ•„์š”
  • ๋…ธ๋“œ์— ๋น„ํ•ด ๊ฐ„์„ ์ด ์ ์œผ๋ฉด ๋น„ํšจ์œจ์  : ๊ฐ„์„ ์˜ ์ˆ˜์™€๋Š” ๊ด€๊ณ„ ์—†์ด ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•จ

2. ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS: Breadth-First Search)

  • ์‹œ์ž‘ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ •์ ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ  ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ์ •์ ์„ ๋‚˜์ค‘์— ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„
  • ์žฌ๊ท€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ

[์ถœ์ฒ˜ : https://developer-mac.tistory.com/64]


๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์˜ ๋ถ„์„

  • ์ธ์ ‘ ํ–‰๋ ฌ -> O(n^2)
  • ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ -> O(n+e)

-> DFS์™€ BFS์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๊ฐ™์Œ


๋‹ค๋งŒ DFS์™€ BFS๋ฅผ ์„ ํƒํ•จ์— ์žˆ์–ด์„œ ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค


DFS์™€ BFS