Graph

Graph

  • ๊ตฌ์„ฑ์š”์†Œ

    • ์ •์  (Vertex)

    • ๊ฐ„์„  (Edge)

  • |v|: ์ •์ ์˜ ๊ฐœ์ˆ˜, |e|: ๊ทธ๋ž˜ํ”„์— ํฌํ•จ๋œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜

    • |v| ๊ฐœ์˜ ์ •์ ์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„๋Š” ์ตœ๋Œ€ |v| (|v|-1) /2 ๊ฐ„์„ ์ด ๊ฐ€๋Šฅ

      • ex) 5๊ฐœ ์ •์ ์ด ์žˆ๋Š” ๊ทธ๋ž˜ํ” ์˜ ์ตœ๋Œ€ ๊ฐ„์„  ์ˆ˜๋Š” 5*4/2 = 10 ๊ฐœ์ด๋‹ค

  • ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋‚˜ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์–ด๋ ค์šด N:N ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š” ์›์†Œ๋“ค์„ ํ‘œํ˜„ํ•˜๊ธฐ์— ์šฉ์ดํ•˜๋‹ค.

๊ทธ๋ž˜ํ”„ ์œ ํ˜•

  • ๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„ (Undirected Graph)

  • ์œ ํ–ฅ ๊ทธ๋ž˜ํ”„ (Directed Graph)

  • ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ (Weighted Graph)

  • ์‚ฌ์ดํด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ (DAG: Directed Acyclic Graph)

    • ์‚ฌ์ดํด

      : ์‹œ์ž‘ ์ •์ ๊ณผ ๋ ์ •์ ์ด ๊ฐ™์€ ๊ฒฝ๋กœ

      • ex)

        • ๊ณผ์ • ์ด์ˆ˜ (์„ ์ˆ˜ ๊ณผ๋ชฉ)

    • ๋‹จ์ˆœ ๊ฒฝ๋กœ

      : ๊ฒฝ๋กœ ์ค‘ ํ•œ ์ •์ ์„ ์ตœ๋Œ€ํ•œ ํ•œ๋ฒˆ๋งŒ ์ง€๋‚˜๋Š” ๊ฒฝ๋กœ

์ธ์ ‘ ์ •์ 

์ธ์ ‘ (Adjacency)

  • ๋‘ ๊ฐœ์˜ ์ •์ ์— ๊ฐ„์„ ์ด ์กด์žฌ (์—ฐ๊ฒฐ๋จ) ํ•˜๋ฉด, ์„œ๋กœ ์ธ์ ‘ํ•ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

  • ์™„์ „ ๊ทธ๋ž˜ํ”„์— ์†ํ•œ ์ž„์˜์˜ ๋‘ ์ •์ ๋“ค์€ ๋ชจ๋‘ ์ธ์ ‘ํ•ด์žˆ๋‹ค

๊ทธ๋ž˜ํ”„ ํ‘œํ˜„

๊ฐ„์„ ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹, ๋ฉ”๋ชจ๋ฆฌ๋‚˜ ์„ฑ๋Šฅ์„ ๊ณ ๋ คํ•ด์„œ ๊ฒฐ์ •

์ธ์ ‘ ํ–‰๋ ฌ (Adjacent matrix)

๋‘ ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ์˜ ์œ ๋ฌด๋ฅผ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„

  • |v| x |v| ํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ๊ฐ„์„  ์ •๋ณด๋ฅผ ์ €์žฅ

  • ๋ฐฐ์—ด์˜ ๋ฐฐ์—ด

  • ํ–‰ ๋ฒˆํ˜ธ์™€ ์—ด ๋ฒˆํ˜ธ๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์— ๋Œ€์‘

  • ๋‘ ์ •์ ์ด ์ธ์ ‘๋˜์–ด ์žˆ์œผ๋ฉด1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์œผ๋กœ ํ‘œํ˜„

  • ๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„

    • i๋ฒˆ์งธ ํ–‰์˜ ํ•ฉ = i๋ฒˆ์งธ ์—ด์˜ ํ•ฉ = Vi์˜ ์ฐจ์ˆ˜

  • ์œ ํ–ฅ ๊ทธ๋ž˜ํ”„

    • ํ–‰ i์˜ ํ•ฉ = Vi์˜ ์ง„์ถœ ์ฐจ์ˆ˜

    • ์—ด i์˜ ํ•ฉ = Vi์˜ ์ง„์ž… ์ฐจ์ˆ˜

  • ๋‹จ์ 

    • ๊ฐ„์„ ์ด ์ ๋”๋ผ๋„ (ํฌ์†Œ ๊ทธ๋ž˜ํ”„) V*V ํฌ๊ธฐ์˜ ๊ณต๊ฐ„ ํ™•๋ณด

    • ์ธ์ ‘ ์ •์ ์„ ์ฐพ์„ ๋•Œ ์ธ์ ‘ ์ •์ ์ด ์ ๋”๋ผ๋„ V๋ฒˆ ์—ฐ์‚ฐ ํ•„์š”

์ธ์ ‘ ๋ฆฌ์ŠคํŠธ (Adjacent List)

๊ฐ ์ •์ ์— ๋Œ€ํ•œ ์ธ์ ‘ ์ •์ ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํ‘œํ˜„

  • ํ•˜๋‚˜์˜ ์ •์ ์— ๋Œ€ํ•œ ์ธ์ ‘ ์ •์ ๋“ค์„ ๊ฐ๊ฐ ๋…ธ๋“œ๋กœ ํ•˜๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ

๊ฐ„์„ ์˜ ๋ฐฐ์—ด

  • ๊ฐ„์„  (์‹œ์ž‘ ์ •์ , ๋ ์ •์ )์„ ๋ฐฐ์—ด์— ์—ฐ์†์ ์œผ๋กœ ์ €์žฅ

๊ทธ๋ž˜ํ”„ ์ˆœํšŒ (ํƒ์ƒ‰)

๋น„์„ ํ˜•๊ตฌ์กฐ์ธ Graph๋กœ ๊ตฌํ˜„๋œ ๋ชจ๋“  ์ž๋ฃŒ (์ •์ )๋ฅผ ๋น ์ง์—†์ด ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธ

  • ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•

    • ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS: Depth Fist Search)

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

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

  • ์‹œ์ž‘ ์ •์ ์˜ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š” ๊ณณ๊นŒ์ง€ ๊นŠ์ด ํƒ์ƒ‰ํ•ด ๊ฐ€๋‹ค๊ฐ€ ๋” ์ด์ƒ ๊ฐˆ ๊ณณ์ด ์—†๊ฒŒ ๋˜๋ฉด,

    ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋งŒ๋‚ฌ๋˜ ๊ฐˆ๋ฆผ๊ธธ ๊ฐ„์„ ์ด ์žˆ๋Š” ์ •์ ์œผ๋กœ ๋˜๋Œ์•„์™€์„œ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์˜ ์ •์ ์œผ๋กœ ํƒ์ƒ‰์„ ๊ณ„์† ๋ฐ˜๋ณตํ•˜์—ฌ

    ๊ฒฐ๊ตญ ๋ชจ๋“  ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœํšŒ๋ฐฉ๋ฒ•

  • ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋งŒ๋‚ฌ๋˜ ๊ฐˆ๋ฆผ๊ธธ์˜ ์ •์ ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ DFS๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ›„์ž…์„ ์ถœ (LIFO) ๊ตฌ์กฐ์˜ Stack ์‚ฌ์šฉ

Stack

  • ๋ฌผ๊ฑด์„ ์Œ“์•„ ์˜ฌ๋ฆฌ๋“ฏ ์ž๋ฃŒ๋ฅผ ์Œ“์•„ ์˜ฌ๋ฆฐ ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ

  • ์„ ํ˜• ๊ตฌ์กฐ

    • ์ž๋ฃŒ ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ 1:1์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š”๋‹ค

      • ๋น„์„ ํ˜• ๊ตฌ์กฐ๋Š” 1:N ๊ด€๊ณ„ (ํŠธ๋ฆฌ)

  • ๋งˆ์ง€๋ง‰์— ์‚ฝ์ž…ํ•œ ์ž๋ฃŒ๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ๊บผ๋‚ธ๋‹ค

    • ํ›„์ž… ์„ ์ถœ (LIFO: Last In Fist Out)

  • ํƒ์ƒ‰ ์‹œ์ž‘์ ์˜ ์น์ ‘ํ•œ ์ •์ ๋“ค์„ ๋จผ์ € ๋ชจ๋‘ ์ฐจ๋ก€๋กœ ๋ฐฉ๋ฌธํ•œ ํ›„์—, ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ •์ ์„ ์‹œ์ž‘์œผ๋กœ ํ•˜์—ฌ ๋‹ค์‹œ ์ธ์ ‘ํ•œ ์ •์ ๋“ค์„ ์ฐจ๋ก€๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ์‹

  • ์ธ์ ‘ํ•œ ์ •์ ๋“ค์— ๋Œ€ํ•ด ํƒ์ƒ‰์„ ํ•œ ํ›„, ์ฐจ๋ก€๋กœ ๋‹ค์‹œ BFS๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์„ ์ž…์„ ์ถœ (FIFO) ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ธ Queue๋ฅผ ํ™œ์šฉํ•จ<

Queue

  • Stack๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ์˜ ์œ„์น˜๊ฐ€ ์ œํ•œ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ

    • Queue์˜ ๋’ค์—์„œ๋Š” ์‚ฝ์ž…๋งŒ, ์•ž์—์„œ๋Š” ์‚ญ์ œ๋งŒ ์ด๋ฃจ์–ด์ง€๋Š” ๊ตฌ์กฐ

  • Queue์— ์‚ฝ์ž…ํ•œ ์ˆœ์„œ๋Œ€๋กœ ์›์†Œ๊ฐ€ ์ €์žฅ๋˜์–ด, ๊ฐ€์žฅ ๋จผ์ € ์‚ฝ์ž…๋œ ์›์†Œ๋Š” ๊ฐ€์žฅ ๋จผ์ € ์‚ญ์ œ๋œ๋‹ค

    • ์„ ์ž…์„ ์ถœ (FIFO: FIst In FIst Out)

  • ๊ธฐ๋ณธ ์—ฐ์„ 

    • ์‚ฝ์ž…

      • enQueue

    • ์‚ญ์ œ

      • deQueue

  • ๊ณต๋ฐฑ์ƒํƒœ ๋ฐ ํฌํ™”์ƒํƒœ ๊ฒ€์‚ฌ:isEmpty(), isFull()

    • ๊ณต๋ฐฑ์ƒํƒœ

      • front =rear

    • ํฌํ™”์ƒํƒœ

      • rear = n-1 (n: ๋ฒ ์—ด์˜ ํฌ๊ธฐ, n-1: ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ index)

์ƒํ˜ธ๋ฐฐํƒ€/์„œ๋กœ์†Œ ์ง‘ํ•ฉ (Disjoint-sets)

  • ์„œ๋กœ์†Œ or ์ƒํ˜ธ๋ฐฐํƒ€ ์ง‘ํ•ฉ๋“ค์€ ์„œ๋กœ ์ค‘๋ณต ํฌํ•จ๋œ ์›์†Œ๊ฐ€ ์—†๋Š” ์ง‘ํ•ฉ๋“ค์„ ๋งํ•จ

    • ๊ต์ง‘ํ•ฉ์ด ์—†๋‹ค!

  • ์ง‘ํ•ฉ์— ์†ํ•œ ํ•˜๋‚˜์˜ ํŠน์ • ๋ฉค๋ฒ„๋ฅผ ํ†ตํ•ด ๊ฐ ์ง‘ํ•ฉ๋“ค์„ ๊ตฌ๋ถ„

    • ์ด๋ฅผ ๋Œ€ํ‘œ์ž(representative) ๋ผ๊ณ  ํ•œ๋‹ค!

  • ์ƒํ˜ธ๋ฐฐํƒ€ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

    • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

    • ํŠธ๋ฆฌ

  • ์ƒํ˜ธ๋ฐฐํƒ€ ์ง‘ํ•ฉ ์—ฐ์‚ฐ

    • Make-Set(x)

      • ์›์†Œ x๋ฅผ ์œ ์ผํ•œ ์›์†Œ๋กœ ํ•˜๋Š” ์ง‘ํ•ฉ ์ƒ์„ฑ

    • FInd-Set(x)

      • x๊ฐ€ ํฌํ•จ๋œ ์ง‘ํ•ฉ์˜ ๋Œ€ํ‘œ์ž ๋ฆฌํ„ด

    • Union(x,y)

      • x๊ฐ€ ํฌํ•จ๋œ ์ง‘ํ•ฉ๊ณผ y๊ฐ€ ํฌํ•จ๋œ ์ง‘ํ•ฉ์„ ํ•ฉ์ณ์คŒ

์ƒํ˜ธ ๋ฐฐํƒ€ ์ง‘ํ•ฉ ํ‘œํ˜„ - ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

  • ๊ฐ™์€ ์ง‘ํ•ฉ์˜ ์›์†Œ๋“ค์€ ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ด€๋ฆฌ

  • ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋งค ์•ž์˜ ์›์†Œ๋ฅผ ์ง‘ํ•ฉ์˜ ๋Œ€ํ‘œ ์›์†Œ๋กœ ์‚ผ๋Š”๋‹ค

  • ๊ฐ ์›์†Œ๋Š” ์ง‘ํ•ฉ์˜ ๋Œ€ํ‘œ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋งํฌ๋ฅผ ๊ฐ–๋Š”๋‹ค

์ƒํ˜ธ๋ฐฐํƒ€ ์ง‘ํ•ฉ ํ‘œํ˜„ - Tree

  • ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ(a disjoint set)์„ ํ•˜๋‚˜์˜ tree๋กœ ํ‘œํ˜„ํ•œ๋‹ค

  • ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, root node๊ฐ€ ๋Œ€ํ‘œ์ž๊ฐ€ ๋œ๋‹ค

Union find ์˜ˆ์ œ

"""
์„œ๋กœ์†Œ ์ง‘ํ•ฉ
N = 6
union(1,3)
union(2,3)
union(5,6)
print(p)
print(find_set(6))

๊ฒฐ๊ณผ
[0,2,2,1,4,5,5]
"""

def make_set(x):
    p[x] = x

def find_set(x):
    if p[x] == x:
        return x
    else:
        return find_set(p[x])

def union(x,y):
    # x์˜ ๋Œ€ํ‘œ์ž, y์˜ ๋Œ€ํ‘œ์ž๋ฅผ ์ฐพ์Œ
    p[find_set(y)] = find_set(x)

N = 6

p = [0] * (N+1)

for i in range(1,N+1):
    make_set(i)


union(1,3)
union(2,3)
union(5,6)
print(p)
print(find_set(6))


# ๋ฌธ์ œ์ 
# : ๋Œ€ํ‘œ์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์žฌ๊ท€ํ˜ธ์ถœ ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰
#   -> ๋Œ€ํ‘œ์ž๋ฅผ ๋ฐ”๋กœ ์ฐพ์•„๊ฐ€๋„๋ก ๊ฑด๋„ˆ ๋„์–ด ๊ฐ€์•ผํ•จ
#      p[b] <- f

+

์—ฐ์‚ฐ์˜ ํšจ์œจ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•

  1. Rank ๋ฅผ ์ด์šฉํ•œ Union

    • ๊ฐ node๋Š” ์ž์‹ ์„ root๋กœ ํ•˜๋Š” subtree์˜ ๋†’์ด๋ฅผ Rank ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•œ๋‹ค

    • ๋‘ ์ง‘ํ•ฉ์„ ํ•ฉ์น  ๋•Œ rank๊ฐ€ ๋‚ฎ์€ ์ง‘ํ•ฉ์„ rank๊ฐ€ ๋†’์€ ์ง‘ํ•ฉ์— ๋ถ™์ธ๋‹ค

  2. Path compression

    • Find-Set์„ ํ–‰ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋งŒ๋‚˜๋Š” ๋ชจ๋“  node๋“ค์ด ์ง์ ‘ root๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํฌ์ธํ„ฐ๋ฅผ ๋ฐ”๊พธ์–ด์ค€๋‹ค

Last updated