let's get IT with DAVINA ๐Ÿ’ป

์ž๋ฃŒ๊ตฌ์กฐ/๋น„์„ ํ˜•๊ตฌ์กฐ/๊ทธ๋ž˜ํ”„ ๋ณธ๋ฌธ

DEV_IN/Algorithm

์ž๋ฃŒ๊ตฌ์กฐ/๋น„์„ ํ˜•๊ตฌ์กฐ/๊ทธ๋ž˜ํ”„

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 23. 02:47

Graph๋ž€?

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ ๋“ค์ด ์„œ๋กœ ๋ณต์žกํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ

  • ๋ชฉ์  โ–ท ๊ทธ๋ž˜ํ”„์˜ ํƒ์ƒ‰์€ ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธ(ํƒ์ƒ‰)ํ•˜๋Š” ๊ฒƒ!
  • ๊ทธ๋ž˜ํ”„์˜ ๋ฐ์ดํ„ฐ๋Š” ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ •๋ ฌ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ์ž๋ฃŒ๋ฅผ ์ฐพ์œผ๋ ค๋ฉด, ํ•˜๋‚˜์”ฉ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•จ
    • ๋ฐฉ์‹ โ–ท BFS & DFS

Graph์˜ ๊ตฌ์กฐ

  • ์ง์ ‘์ ์ธ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋‘ ์  ์‚ฌ์ด๋ฅผ ์ด์–ด์ฃผ๋Š” ์„ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ„์ ‘์ ์ธ ๊ด€๊ณ„๋ผ๋ฉด, ๋ช‡ ๊ฐœ์˜ ์ ๊ณผ ์„ ์— ๊ฑธ์ณ ์ด์–ด์ง‘๋‹ˆ๋‹ค

Graph terms

  • ์ •์  (vertex)
    • ๋…ธ๋“œ(node)๋ผ๊ณ ๋„ ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ทธ๋ž˜ํ”„์˜ ๊ธฐ๋ณธ ์›์†Œ
  • ๊ฐ„์„  (edge)
    • ์ •์  ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (์ •์ ์„ ์ด์–ด์ฃผ๋Š” ์„ )
  • ์ธ์ ‘ ์ •์  (adjacent vertex)
    • ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๊ฐ„์„ ์— ์˜ํ•ด ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •์ 
  • ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ (weighted Graph)
    • ์—ฐ๊ฒฐ์˜ ๊ฐ•๋„(์ถ”๊ฐ€์ ์ธ ์ •๋ณด, ex. ์„œ์šธ-๋ถ€์‚ฐ์œผ๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ ๋“ฑ)๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€ ์ ํ˜€์ ธ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„
  • ๋น„๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ (unweighted Graph)
    • ์—ฐ๊ฒฐ์˜ ๊ฐ•๋„๊ฐ€ ์ ํ˜€์ ธ ์žˆ์ง€ ์•Š๋Š” ๊ทธ๋ž˜ํ”„
  • ๋ฌด(๋ฐฉ)ํ–ฅ ๊ทธ๋ž˜ํ”„ (undirected graph)
    • ์„œ์šธ์—์„œ ๋ถ€์‚ฐ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋“ฏ, ๋ฐ˜๋Œ€๋กœ ๋ถ€์‚ฐ์—์„œ ์„œ์šธ๋กœ ๊ฐ€๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ ๋‹จ๋ฐฉํ–ฅ(directed) ๊ทธ๋ž˜ํ”„๋กœ ๊ตฌํ˜„๋œ๋‹ค๋ฉด ์„œ์šธ์—์„œ ๋ถ€์‚ฐ์„ ๊ฐˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ถ€์‚ฐ์—์„œ ์„œ์šธ๋กœ ๊ฐ€๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค(ํ˜น์€ ๊ทธ ๋ฐ˜๋Œ€).
    • ๋งŒ์•ฝ ๋‘ ์ง€์ ์ด ์ผ๋ฐฉํ†ตํ–‰ ๋„๋กœ๋กœ ์ด์–ด์ ธ ์žˆ๋‹ค๋ฉด ๋‹จ๋ฐฉํ–ฅ์ธ ๊ฐ„์„ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„
    • ์ •์ ๋“ค์ด ๊ฐ„์„ ์œผ๋กœ ์ „๋ถ€ ์—ฐ๊ฒฐ์ด ๋˜์–ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„ (๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค)
  • ๋น„์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„
    • ์ •์  ์‚ฌ์ด์— ์–ด๋– ํ•œ ๊ฐ„์„ ๋„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†๋Š” ๊ทธ๋ž˜ํ”„ (๊ด€๊ณ„๊ฐ€ ์—†๋‹ค)
  • ์ง„์ž…์ฐจ์ˆ˜ (in-degree) / ์ง„์ถœ์ฐจ์ˆ˜ (out-degree)
    • ํ•œ ์ •์ ์— ์ง„์ž…(๋“ค์–ด์˜ค๋Š” ๊ฐ„์„ )ํ•˜๊ณ  ์ง„์ถœ(๋‚˜๊ฐ€๋Š” ๊ฐ„์„ )ํ•˜๋Š” ๊ฐ„์„ ์ด ๋ช‡ ๊ฐœ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ์ธ์ ‘ (adjacency)
    • ๋‘ ์ •์  ๊ฐ„์— ๊ฐ„์„ ์ด ์ง์ ‘ ์ด์–ด์ ธ ์žˆ๋‹ค๋ฉด ์ด ๋‘ ์ •์ ์€ ์ธ์ ‘ํ•œ ์ •์ ์ž…๋‹ˆ๋‹ค.
  • ์ž๊ธฐ ๋ฃจํ”„ (self loop)
    • ์ •์ ์—์„œ ์ง„์ถœํ•˜๋Š” ๊ฐ„์„ ์ด ๊ณง๋ฐ”๋กœ ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ ์ง„์ž…ํ•˜๋Š” ๊ฒฝ์šฐ "์ž๊ธฐ ๋ฃจํ”„๋ฅผ ๊ฐ€์กŒ๋‹ค" ๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
    • ๋‹ค๋ฅธ ์ •์ ์„ ๊ฑฐ์น˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด ํŠน์ง•์ž…๋‹ˆ๋‹ค.
  • ์‚ฌ์ดํด (cycle)
    • ํ•œ ์ •์ ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค์‹œ ํ•ด๋‹น ์ •์ ์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‚ฌ์ดํด์ด ์žˆ๋‹ค๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
    • ๋‚ด๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„๋Š” ์„œ์šธ → ๋Œ€์ „ → ๋ถ€์‚ฐ → ์„œ์šธ ๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค.

BFS(Breadth-First Search)→Queue

  • ๋„ˆ๋น„๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Breadth-First Search, ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰

  • ๋‘ ์ •์  ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ๋‹ค ์‚ดํŽด๋ด์•ผ ํ•จ.
  • ๊ธฐ์ค€์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋ชฉํ‘œ์ ๊นŒ์ง€ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•œ๋‹ค.

DFS(Depth-First Search)→Stack

  • ๊นŠ์ด๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Depth-First Search, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰

  • DFS๋Š” ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋ฅผ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•œ ํ›„, ๋ฏธ๊ตญ ๋„์ฐฉ์ด ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์Œ ๊ฒฝ๋กœ๋กœ ๋„˜์–ด๊ฐ€ ํƒ์ƒ‰
  • ํ•œ ์ •์ ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๊ฒฝ๋กœ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉ
  • BFS๋ณด๋‹ค ํƒ์ƒ‰ ์‹œ๊ฐ„์€ ์กฐ๊ธˆ ์˜ค๋ž˜ ๊ฑธ๋ฆด์ง€๋ผ๋„ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์™„์ „ํžˆ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • tree์—์„œ inorder, preorder, postorder์ด DFS๋ฐฉ์‹์— ํ•ด๋‹น๋จ
DFS์™€ BFS๋Š” ๋ชจ๋“  ์ •์ ์„ ํ•œ ๋ฒˆ๋งŒ ๋ฐฉ๋ฌธํ•œ๋‹ค๋Š” ๊ณตํ†ต์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ์‚ฌ์šฉํ•  ๋•Œ์˜ ์žฅ๋‹จ์ ์€ ๋ถ„๋ช…ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์ƒํ™ฉ์— ๋งž๋Š” ํƒ์ƒ‰ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
Comments