Algorithm Theory

์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ(Minimum Spanning Tree)

Meng's Computer 2020. 12. 15. 01:40

๐Ÿ“ข  ๊ฐ€์ค‘๊ทธ๋ž˜ํ”„

  • ๊ฐ€์ค‘๊ทธ๋ž˜ํ”„(Weighted Graph) : ๊ฐ ๊ฐ„์„ ์ด ๋ฌด๊ฒŒ(weight)๋ผ ๋ถ€๋ฅด๋Š” ์ˆ˜์น˜๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„
  • ๋ฌด๊ฒŒ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์€ ๊ฑฐ๋ฆฌ,๋น„์šฉ,์‹œ๊ฐ„ ๋“ฑ ๋‹ค์–‘ํ•˜๋‹ค.

๊ฐ€์ค‘๊ทธ๋ž˜ํ”„์˜ ์˜ˆ์‹œ

 

๐Ÿ“ข  ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ

  • ์‹ ์žฅ ๋ถ€๊ทธ๋ž˜ํ”„(Spanning Subgraph) : ๊ทธ๋ž˜ํ”„ G์˜ ๋ชจ๋“  ์ •์ ๋“ค์„ ํฌํ•จํ•˜๋Š” ๋ถ€๊ทธ๋ž˜ํ”„
  • ์‹ ์žฅํŠธ๋ฆฌ(Spanning Tree) : (์ž์œ )ํŠธ๋ฆฌ์ธ ์‹ ์žฅ ๋ถ€๊ทธ๋ž˜ํ”„
  • ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ(MST, Minimum Spanning Tree) : ๊ฐ€์ค‘๊ทธ๋ž˜ํ”„์˜ ์ด ๊ฐ„์„  ๋ฌด๊ฒŒ๊ฐ€ ์ตœ์†Œ์ธ ์‹ ์žฅํŠธ๋ฆฌ
  • ์‘์šฉ๋ถ„์•ผ

    ๐Ÿ‘‰ ๊ตํ†ต๋ง, ํ†ต์‹ ๋ง ๋“ฑ๋“ฑ

์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ ์˜ˆ์‹œ ( ํŒŒ๋ž€์ƒ‰ ๊ฒฝ๋กœ๊ฐ€ ์ถ”๊ฐ€๋  ๊ฒฝ์šฐ Cycle ์ƒ์„ฑ๋จ X )

  • ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ ์†์„ฑ

    ๐Ÿ‘‰ 1. ์‹ธ์ดํด์†์„ฑ
    ๐Ÿ‘‰ 2. ๋ถ„ํ•  ์†์„ฑ

    ์ด ๋‘๊ฐœ์˜ ์†์„ฑ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ •ํ™•์„ฑ ๊ฒ€์ฆ์— ์ด์šฉํ•œ๋‹ค.

 

๐Ÿ“ข  ์‹ธ์ดํด ์†์„ฑ

f๋ฅผ e๋กœ ๋Œ€์ฒดํ•˜๋ฉด ๋” ์ข‹์€ ์‹ ์žฅํŠธ๋ฆฌ๋ฅผ ์–ป๋Š”๋‹ค.

  • ์‹ธ์ดํด ์†์„ฑ

    ๐Ÿ‘‰ T๋ฅผ ๊ฐ€์ค‘๊ทธ๋ž˜ํ”„ G์˜ ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ๋ผ ๊ฐ€์ •ํ•˜์ž.
    ๐Ÿ‘‰ e๋ฅผ T์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” G์˜ ๊ฐ„์„ ์œผ๋กœ, C๋ฅผ e๋ฅผ T์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ˜•์„ฑ๋œ ์‹ธ์ดํด๋กœ ๊ฐ€์ •ํ•˜์ž.
    ๐Ÿ‘‰ ๊ทธ๋Ÿฌ๋ฉด C์˜ ๋ชจ๋“  ๊ฐ„์„  f์— ๋Œ€ํ•ด, weight(f) <= weight(e) ์ด๋‹ค.

  • ์ฆ๋ช…

    ๐Ÿ‘‰ ๋ชจ์ˆœ๋ฒ• : ๋งŒ์•ฝ weight(f) > weight(e)๋ผ๋ฉด, f๋ฅผ e๋กœ ๋Œ€์ฒดํ•จ์œผ๋กœ์จ ๋ฌด๊ฒŒ๊ฐ€ ๋” ์ž‘์€ ์‹ ์žฅํŠธ๋ฆฌ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ

 

๐Ÿ“ข  ๋ถ„ํ•  ์†์„ฑ

  • ๋ถ„ํ•  ์†์„ฑ

    ๐Ÿ‘‰ G์˜ ์ •์ ๋“ค์„ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ U์™€ V๋กœ ๋ถ„ํ• ํ•œ๋‹ค๊ณ  ํ•˜์ž.
    ๐Ÿ‘‰ e๋ฅผ ๋ถ„ํ• ์„ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์ตœ์†Œ ๋ฌด๊ฒŒ์˜ ๊ฐ„์„ ์ด๋ผ๊ณ  ํ•˜์ž.
    ๐Ÿ‘‰ ๊ฐ„์„  e๋ฅผ ํฌํ•จํ•˜๋Š” G์˜ ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ๊ฐ€ ๋ฐ˜๋“œ๋ฆฌ ์กด์žฌํ•œ๋‹ค.

  • ์ฆ๋ช…

    ๐Ÿ‘‰ T๋ฅผ G์˜ MST๋ผ ํ•˜์ž.
    ๐Ÿ‘‰ ๋งŒ์•ฝ T๊ฐ€ e๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, e๋ฅผ T์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ˜•์„ฑ๋œ ์‹ธ์ดํด C๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ„์„ ๋“ค ๊ฐ€์šด๋ฐ ๋ถ„ํ• ์„
    ๊ฐ€๋กœ์ง€๋ฅด๋Š” ๊ฐ„์„  f๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์ด๋‹ค.
    ๐Ÿ‘‰ ์‹ธ์ดํด ์†์„ฑ์— ์˜ํ•ด, weight(f) <= weight(e)
    ๐Ÿ‘‰ ๊ทธ๋Ÿฌ๋ฏ€๋กœ, weight(f) == weight(e)
    ๐Ÿ‘‰ f๋ฅผ e๋กœ ๋Œ€์ฒดํ•˜๋ฉด ๋˜ ํ•˜๋‚˜์˜ MST๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ข  ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํฌ๊ฒŒ 3๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•œ๋‹ค.

    ๐Ÿ‘‰ 1. Prim-Jarnik ์•Œ๊ณ ๋ฆฌ์ฆ˜ ( ๋งŽ์ด ์”€ )
    ๐Ÿ‘‰ 2. Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜ ( ๋งŽ์ด ์”€ )
    ๐Ÿ‘‰ Baruvka ์•Œ๊ณ ๋ฆฌ์ฆ˜ ( 1๋ฒˆ + 2๋ฒˆ ๋Š๋‚Œ )

 

๐Ÿ“ข  Prim-Jarnik ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํƒ์š•(Greedy) ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ผ์ข…
  • ๋Œ€์ƒ : ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„
  • ์•„์ด๋””์–ด

    ๐Ÿ‘‰ ์ž„์˜์˜ ์ •์  s๋ฅผ ํƒํ•˜์—ฌ, s๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ, ์ •์ ๋“ค์„ (๊ฐ€์ƒ์˜)๋ฐฐ๋‚ญ์— ๋„ฃ์–ด๊ฐ€๋ฉฐ ๋ฐฐ๋‚ญ ์•ˆ์—์„œ MST T๋ฅผ ํ‚ค์›Œ ๋‚˜๊ฐ
    ๐Ÿ‘‰ s๋Š” T์˜ ๋ฃจํŠธ(root)๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.
    ๐Ÿ‘‰ ๊ฐ ์ •์  v์— ๋ผ๋ฒจ d(v)๋ฅผ ์ •์˜ = d(v)๋Š” ๋ฐฐ๋‚ญ ์•ˆ์— ์ •์ ๊ณผ ๋ฐฐ๋‚ญ ๋ฐ– ์ •์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ์˜ weight
    ๐Ÿ‘‰ ๋ฐ˜๋ณต์˜ ๊ฐ ํšŒ์ „์—์„œ
    : ๋ฐฐ๋‚ญ ๋ฐ– ์ •์ ๋“ค ๊ฐ€์šด๋ฐ ์ตœ์†Œ d(z) ๋ผ๋ฒจ์„ ๊ฐ€์ง„ ์ •์  z๋ฅผ ๋ฐฐ๋‚ญ์— ๋„ฃ๊ณ , z์— ์ธ์ ‘ํ•œ ์ •์ ๋“ค์˜ ๋ผ๋ฒจ์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
Alg PrimJarnikMST(G)
	input a simple connected weighted graph G with n vertexs and m edges
    output an MST T for G
    
for each v ∈ G.vertexs()
    d(v) <- ∞		// ์—ฌ๊ธฐ์„œ, d(v)๋Š” ๊ฐ ์ •์ ๋“ค์˜ ๊ฑฐ๋ฆฌ
    p(v) <- ∅		// ์—ฌ๊ธฐ์„œ, p(v)๋Š” ๊ฐ ์ •์ ๋“ค์˜ MST๋กœ ์ด๋ค„์ง€๊ธฐ์œ„ํ•œ ์ง๊ณ„ ๋ถ€๋ชจ ์ •์  ์ •๋ณด

s <- a vertex of G
d(s) <- 0
Q <- a priority queue containing all the vertexs of G using d labels as keys

while(!(Q.isEmpty())	// ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋น„์šธ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต์ธ๋ฐ, ๋ฐ˜๋ณต ์ž‘์—…์€, ๋ฐฐ๋‚ญ(MST)์— ์ •์ •์„ ํ•˜๋‚˜์”ฉ ๋„“ํ˜€๊ฐ€๋Š” ๊ฒƒ
    u <- Q.removeMin()	// ์šฐ์„ ์ˆœ์œ„ ํ์— ๋“ค์–ด๊ฐ€์žˆ๋Š”, ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ d ๊ฐ€ ์ž‘์€ ์ •์  ๋ฐ˜ํ™˜ u
    for each e ∈ G.incidentEdges(u)	// ์ •์  u์— ๋ถ€์ฐฉ๋œ ๊ฐ„์„  ์ „๋ถ€๋ฅผ ๋ณธ๋‹ค.
        z <- G.opposite(u,e)	// ์ •์  u์™€ ์ด์–ด์ง„ ๊ฐ„์„  e๋ฅผ ํ†ตํ•œ ๋ฐ˜๋Œ€ํŽธ ์ •์ ์„ z๋ผ ํ•œ๋‹ค.
        if((z ∈ Q) & (w(u,z) < d(z)))	// ์ •์  z๊ฐ€ ์šฐ์„ ์ˆœ์œ„ ํ์— ์žˆ๋Š” ๊ฒƒ์ด๊ณ , ์ฆ‰ ์•„์ง ๋ฐฉ๋ฌธ ์•ˆํ•œ ์ •์ ์ด๊ณ , ์ •์  u๋ฅผ ๊ฑฐ์ณ ์ •์  z๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๊ฐ€, ์•„์ง ํ˜„์žฌ ์ •์  z๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ ์œผ๋ฉด
            d(z) <- w(u,z)		// ์ •์  z์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
            p(z) <- e		// ๊ทธ๋ฆฌ๊ณ , ์ •์  z์˜ ๋ถ€๋ชจ๊ฐ„์„ ์„ e๋กœ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
            Q.replaceKey(z,w(u,z))	// ์ด๋ ‡๊ฒŒ, ์ •์  z์˜ ๊ฑฐ๋ฆฌ(d)๊ฐ€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋ฐ”๊ผˆ์„ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ, ์šฐ์„ ์ˆœ์œ„ํ ์žฌ๋ฐฐ์น˜๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
            

 

  • ์ถ”๊ฐ€์„ค๋ช…

    ๐Ÿ‘‰ (๊ฐ€์ƒ์˜)๋ฐฐ๋‚ญ ๋ฐ–์˜ ์ •์ ๋“ค์„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅ : ํ‚ค(๊ฑฐ๋ฆฌ)-์›์†Œ(์ •์ ์ •๋ณด)
    ๐Ÿ‘‰ ๊ฐ ์ •์  v์— ์„ธ ๊ฐœ์˜ ๋ผ๋ฒจ์„ ์ €์žฅํ•œ๋‹ค.

    1. ๊ฑฐ๋ฆฌ(distance) : d(v)
    2. ์œ„์น˜์ž(locator) : ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ์˜ v์˜ ์œ„์น˜
    3. ๋ถ€๋ชจ(parent) : p(v), MST์—์„œ v์˜ ๋ถ€๋ชจ๋ฅผ ํ–ฅํ•œ ๊ฐ„์„ 

ใ…ŒPrim-Jarnik ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„์„

    ๐Ÿ‘‰ ๋ผ๋ฒจ ์ž‘์—… : ์ ์ • z์˜ ๊ฑฐ๋ฆฌ, ๋ถ€๋ชจ, ์œ„์น˜์ž ๋ผ๋ฒจ์„ O(deg(z))์‹œ๊ฐ„ ์ฝ๊ณ  ์“ด๋‹ค. ๋ผ๋ฒจ์„ ์ฝ๊ณ  ์“ฐ๋Š”๋ฐ O(1)์‹œ๊ฐ„ ์†Œ์š”
    ๐Ÿ‘‰ ์šฐ์„ ์ˆœ์œ„ ํ( ํž™์œผ๋กœ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ ) ์ž‘์—…

    1. ๊ฐ ์ •์ ์€ ์šฐ์„ ์ˆœ์œ„ ํ์— ํ•œ๋ฒˆ ์‚ฝ์ž…๋˜๊ณ  ํ•œ๋ฒˆ ์‚ญ์ œ : ์‚ฝ์ž…๊ณผ ์‚ญ์ œ์— ๊ฐ๊ฐ O(log(N))์‹œ๊ฐ„ ์†Œ์š”
    2. ์šฐ์„ ์ˆœ์œ„ ํ ๋‚ด์˜ ์ •์  w์˜ ํ‚ค๋Š” ์ตœ๋Œ€ deg(w)๋ฒˆ ๋ณ€๊ฒฝ : ๊ฐ ํ‚ค ๋ณ€๊ฒฝ์— O(log(N))์‹œ๊ฐ„ ์†Œ์š” (ํž™ ์ด๋‹ˆ๊น)

    ๐Ÿ‘‰ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋กœ ํ‘œํ˜„๋˜์–ด ์žˆ๋‹ค๋ฉด, Prim-Jarnik ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ O((N+M)log(N))์‹œ๊ฐ„์— ์ˆ˜ํ–‰ํ•œ๋‹ค.
    ๐Ÿ‘‰ ๋‹จ์ˆœ ์—ฐ๊ฒฐ๊ทธ๋ž˜ํ”„์—์„œ N = O(M)์ด๋ฏ€๋กœ, ์ด ์‹คํ–‰์‹œ๊ฐ„ : O(Mlog(N))

 

๐Ÿ“ข Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ํƒ์š•(Greedy) ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์œ„ํ•œ ์ดˆ๊ธฐ ์ž‘์—…

    ๐Ÿ‘‰ ๋ชจ๋“  ์ •์ ์„ ๊ฐ๊ฐ์˜ ๋…์ž์ ์ธ (์‹ค์ œ์˜)๋ฐฐ๋‚ญ์— ๋„ฃ๋Š”๋‹ค.
    ๐Ÿ‘‰ ๋ฐฐ๋‚ญ ๋ฐ– ๊ฐ„์„ ๋“ค์„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค. = ํ‚ค(๋ฌด๊ฒŒ)-์›์†Œ(๊ฐ„์„ )
    ๐Ÿ‘‰ ๋น„์–ด ์žˆ๋Š” MST T๋ฅผ ์ดˆ๊ธฐํ™”

  • ๋ฐ˜๋ณต์˜ ๊ฐ ํšŒ์ „์—์„œ
    : ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๋ฐฐ๋‚ญ ์†์— ์–‘๋์ ์„ ๊ฐ€์ง„ ์ตœ์†Œ ๋ฌด๊ฒŒ์˜  ๊ฐ„์„ ์„ MST T์— ํฌํ•จํ•˜๊ณ  ๋‘ ๋ฐฐ๋‚ญ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นœ๋‹ค.
  • ๋ฐ˜๋ณต์ด ์™„๋ฃŒ๋˜๋ฉด
    : MST T๋ฅผ ํฌํ•จํ•˜๋Š” ํ•œ ๊ฐœ์˜ ๋ฐฐ๋‚ญ๋งŒ ๋‚จ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.
Alg KruskalMST(G)
	input a simple connected weighted graph G with n vertexs and m edges
    output an MST T for G

for each v ∈ G.vertexs()
    define a Sack(v) <- {v}		// Sack์€ ๋ฐฐ๋‚ญ์„ ์˜๋ฏธ, ์ด๊ฒƒ์€ ์‹ค์ œ ๊ตฌํ˜„์—์„œ๋Š” Parent(v)๋กœ ์ œ์–ดํ•˜๋ฉด ํŽธํ•˜๋‹ค.

Q <- a priority queue containnig all the edges of G using weights as keys
T <- ∅

while(T has fewer than n - 1 edges)
   (u,v) <- Q.removeMin()
   if(Sack(u) ≠ Sack(v))
       Add edge (u,v) to T
       Merge Sack(u) and Sack(v)

return T

์•„Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„์„

    ๐Ÿ‘‰ ์šฐ์„ ์ˆœ์œ„ ํ ์ดˆ๊ธฐํ™” ์ž‘์—…

    ํž™์„ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ Q๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด, ๋ฐ˜๋ณต์ ์ธ ์‚ฝ์ž… ๋ฐฉ์‹์— ์˜ํ•ด์„œ๋Š” O(Mlog(M))์‹œ๊ฐ„์—,
    ์ƒํ•ญ์‹ ํž™ ์ƒ์„ฑ ๋ฐฉ์‹์— ์˜ํ•ด์„œ๋Š” O(M) ์‹œ๊ฐ„์— Q ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    ๐Ÿ‘‰ ๋ฐ˜๋ณต์˜ ๊ฐ ํšŒ์ „์—์„œ๋Š”

    ์ตœ์†Œ ๋ฌด๊ฒŒ์˜ ๊ฐ„์„ ์„ O(log(M))์‹œ๊ฐ„์—  ์‚ญ์ œ ๊ฐ€๋Šฅ = ์—ฌ๊ธฐ์„œ, G๊ฐ€ ๋‹จ์ˆœ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ, O(log(M)) = O(log(N))
    ๊ฐ ๋ฐฐ๋‚ญ์„ ์œ„ํ•œ ๋ถ„๋ฆฌ์ง‘ํ•ฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด, find(u) ≠ find(v) ๊ฒ€์‚ฌ์— O(1) ์‹œ๊ฐ„ ( ๋˜๋Š” parent ๋ฐฐ์—ด์„ ์ด์šฉ ! )
    ๋‘ ๊ฐœ์˜ ๋ฐฐ๋‚ญ Sack(u), Sack(v)๋ฅผ ํ•ฉ์น˜๋Š”๋ฐ O(min( |Sack(u)|, |Sack(v)| )) ์‹œ๊ฐ„ ์†Œ์š” ( ๋” ์ž‘์€ ๋ฐฐ๋‚ญ์„ ์˜ฎ๊น€ )

    ๐Ÿ‘‰ ์ด ๋ฐ˜๋ณต ํšŒ์ˆ˜ : (์ตœ์•…์˜ ๊ฒฝ์šฐ) M๋ฒˆ
    ๐Ÿ‘‰ ๊ทธ๋Ÿฌ๋ฏ€๋กœ, Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ O((N + M)log(N)) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰
    ๐Ÿ‘‰ G๊ฐ€ ๋‹จ์ˆœ ์—ฐ๊ฒฐ๊ทธ๋ž˜ํ”„์ธ ๊ฒฝ์šฐ N = O(M)์ด๋ฏ€๋กœ, ์‹คํ–‰์‹œ๊ฐ„์€ O(Mlog(N)) ์ด๋‹ค.

 

๐Ÿ“ข  MST ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„๊ต

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฃผ์š”์ „๋žต ์ˆ˜ํ–‰์‹œ๊ฐ„ ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๊ตฌ์กฐ
Prim-Jarnik ํƒ์š• O(Mlog(N)) ์ •์ ๋“ค์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์šฐ์„ ์ˆœ์œ„ ํ
Kruskal ํƒ์š• O(Mlog(N)) 1. ๊ฐ„์„ ๋“ค์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์šฐ์„ ์ˆœ์œ„ ํ
2. ๋ฐฐ๋‚ญ๋“ค์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋ถ„๋ฆฌ์ง‘ํ•ฉ(๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๊ฐ€๋Šฅ)
or ๊ฐ ์ •์ ์— ๋Œ€ํ•ด ๋ถ€๋ชจ์ •์ (Parent๋ฐฐ์—ด)๋ฐฐ์—ด์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

728x90
๋ฐ˜์‘ํ˜•