๐Ÿ“ข  ์ตœ๋‹จ๊ฒฝ๋กœ

  • ์ตœ๋‹จ๊ฒฝ๋กœ ๋ฌธ์ œ(Shortest Path Problem)

    ๐Ÿ‘‰๊ฐ€์ค‘๊ทธ๋ž˜ํ”„์™€ ๋‘ ๊ฐœ์˜ ์ •์  u์™€ v๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, u์™€ v์‚ฌ์ด์˜ ๋ฌด๊ฒŒ์˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

  • ์ตœ๋‹จ๊ฒฝ๋กœ๊ธธ์ด : ๊ฐ„์„ ๋“ค์˜ ๋ฌด๊ฒŒ ํ•ฉ
  • ์‘์šฉ๋ถ„์•ผ

    ๐Ÿ‘‰ ์ธํ„ฐ๋„ท ํŒจํ‚ท ๋ผ์šฐํŒ…, ํ•ญ๊ณตํŽธ ์˜ˆ์•ฝ, ์ž๋™์ฐจ ๋„ค๋น„๊ฒŒ์ด์…˜ ๋“ฑ๋“ฑ

 

๐Ÿ“ข  ์ตœ๋‹จ๊ฒฝ๋กœ ์†์„ฑ

  • ์†์„ฑ 1

    ๐Ÿ‘‰ ์ตœ๋‹จ๊ฒฝ๋กœ ๋ถ€๋ถ„๊ฒฝ๋กœ(Subpath)์—ญ์‹œ ์ตœ๋‹จ๊ฒฝ๋กœ์ด๋‹ค.

  • ์†์„ฑ 2

    ๐Ÿ‘‰ ์ถœ๋ฐœ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์— ์ด๋ฅด๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ๋“ค์˜ ํŠธ๋ฆฌ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    = ๋‹จ์ผ์  ์ตœ๋‹จ๊ฒฝ๋กœ(Single-Source Shortest Paths)
  • ์ตœ๋‹จ๊ฒฝ๋กœ๋Š” ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ์™€ ๋‹ฌ๋ฆฌ

    ๐Ÿ‘‰ ๋ฌด๋ฐฉํ–ฅ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์—๋„ ์ •์˜๋œ๋‹ค.
    ๐Ÿ‘‰ ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์‹ธ์ดํด์ด ์žˆ๊ฑฐ๋‚˜, ๋ฌด๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ์žˆ์œผ๋ฉด ๋ถ€์ •ํ•œ๋‹ค.
    ๐Ÿ‘‰ ์ตœ๋‹จ๊ฒฝ๋กœํŠธ๋ฆฌ(Shortest Path Tree)๋Š” ๋ฃจํŠธํŠธ๋ฆฌ

  • ๊ฐ€์ค‘ ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์‹ธ์ดํด์ด ์กด์žฌ

    ๐Ÿ‘‰ ์ตœ๋‹จ๊ฒฝ๋กœ๋Š” ์กด์žฌํ•˜์ง€ X

  • ๊ฐ€์ค‘ ๋ฌด๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ์กด์žฌ

    ๐Ÿ‘‰ ์ตœ๋‹จ๊ฒฝ๋กœ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • ์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜
๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„
์Œ์ด ์—†๋Š” ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„
๊ฐ„์„ ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„
Dijkstra(๋‹ค์ต์ŠคํŠธ๋ผ) O(Mlog(N)) or O(N^2)
์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„
๊ฐ„์„ ์ด ์žˆ๋Š” ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„
Bellman-Ford(๋ฒจ๋งŒ-ํฌ๋“œ)(BFS์— ์†ํ•จ) O(NM)
๋น„๊ฐ€์ค‘๊ทธ๋ž˜ํ”„ BFS O(N + M)
DAG ( ๋ฐฉํ–ฅ ๋น„์‹ธ์ดํด ๊ทธ๋ž˜ํ”„) Topological Ordering(์œ„์ƒ์ •๋ ฌ) O(N + M)

 

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

  • ์ •์  s๋กœ๋ถ€ํ„ฐ ์ •์  v์˜ ๊ฑฐ๋ฆฌ : s์™€ v์‚ฌ์ด์˜ ์ตœ๋‹จ๊ฒฝ๋กœ์˜ ๊ธธ์ด
  • Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ถœ๋ฐœ์ •์  s๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  • ์ „์ œ

    ๐Ÿ‘‰ ๊ทธ๋ž˜ํ”„๊ฐ€ ์—ฐ๊ฒฐ๋˜์žˆ๋‹ค.
    ๐Ÿ‘‰ ๊ฐ„์„ ๋“ค์€ ๋ฌด๋ฐฉํ–ฅ์ด๋‹ค.
    ๐Ÿ‘‰ ๊ฐ„์„ ์˜ ๋ฌด๊ฒŒ๋Š” ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.

  • ์ •์  s๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ •์ ๋“ค์„ ์ฐจ๋ก€๋กœ ๋ฐฐ๋‚ญ์— ๋„ฃ๋‹ค๊ฐ€ ๊ฒฐ๊ตญ์—” ๋ชจ๋“  ์ •์ ์„ ๋ฐฐ๋‚ญ์— ๋„ฃ๋Š”๋‹ค.
  • ๊ฐ ์ •์  v์— ๊ฑฐ๋ฆฌ ๋ผ๋ฒจ d(v)๋ฅผ ์ €์žฅํ•˜์—ฌ ๋ฐฐ๋‚ญ๊ณผ ์ธ์ ‘ ์ •์ ๋“ค์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๊ทธ๋ž˜ํ”„์—์„œ s๋กœ๋ถ€ํ„ฐ v๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ํ‘œํ˜„
  • ๋ฐ˜๋ณต์˜ ๊ฐ ํšŒ์ „์—์„ 

    ๐Ÿ‘‰ ๋ฐฐ๋‚ญ ๋ฐ–์˜ ์ •์  ๊ฐ€์šด๋ฐ ๊ฑฐ๋ฆฌ ๋ผ๋ฒจ d๊ฐ€ ์ตœ์†Œ์ธ ์ •์  u๋ฅผ ๋ฐฐ๋‚ญ์— ๋„ฃ๋Š”๋‹ค.
    ๐Ÿ‘‰ u์— ์ธ์ ‘ํ•œ ์ •์ ๋“ค์˜ ๋ผ๋ฒจ์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ, ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ์˜ Prim-Jarnik์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋งค์šฐ ํก์‚ฌํ•œ ๊ฒƒ์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค.
Alg DijkstraShortestPaths(G,s)
	input a simple undirected weighted graph G with nonnegative edge weights, vertex s of G
    ouput lable d(u), for each vertex u of G, s.t. d(u) is the distance from s to u in G

for each ∈ G.vertexs()	
    d(v) <- ∞					// ๋ชจ๋“  ์ •์ ์˜ ๊ฑฐ๋ฆฌ๋Š” 0

d(s) <- 0						// ์‹œ์ž‘ ์ •์  s์˜ ๊ฑฐ๋ฆฌ๋Š” 0
Q <- a priority queue containing all the vertexs of G using d labels as keys

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

 

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

    ๐Ÿ‘‰ ๋ฐฐ๋‚ญ(Sack) : ๊ฐ€์ƒ์ด๋ฏ€๋กœ, ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Œ !!
    ๐Ÿ‘‰ ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€, ๋ฐฐ๋‚ญ ๋ฐ–์˜ ์ •์ ๋“ค์„ ๋ณด๊ด€ํ•œ๋‹ค. = ( ํ‚ค(distance)-์›์†Œ(vertex) )
    ๐Ÿ‘‰ ๊ฐ ์ •์  v์— ๋‘ ๊ฐœ์˜ ๋ผ๋ฒจ์„ ์ €์žฅ = ( ๊ฑฐ๋ฆฌ( distance ) : d(v) , ๋ถ€๋ชจ ์ •์ ( parent ) : p(v) )

 

๐Ÿ“ข  ๊ฐ„์„ ์™„ํ™”

  • ๊ฐ„์„  e = (u,z)์— ๋Œ€ํ•ด ๊ณ ๋ คํ•œ๋‹ค.

    ๐Ÿ‘‰ u๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฐฐ๋‚ญ์— ๋“ค์–ด๊ฐ„ ์ •์ 
    ๐Ÿ‘‰ z๋Š” ๋ฐฐ๋‚ญ ๋ฐ–์— ์กด์žฌํ•˜๋Š” ์ •์ 
  • ๊ฐ„์„  e์˜ ์™„ํ™”(relaxation)๋ฅผ ํ†ตํ•ด ๊ฑฐ๋ฆฌ d(z)๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค. ์ฆ‰,

    ๐Ÿ‘‰ d(z) <- min( d(z) , d(u) + w(u,z) )

๊ฐ„์„  ์™„ํ™”(edge relaxation) ์˜ˆ์‹œ

 

Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ ( A๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ, ๋ฐฐ๋‚ญ์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ, ๋™์‹œ์— ์ฃผ๋ณ€ ์ •์ ์— ์จ์žˆ๋Š” distance์˜ ๋ณ€ํ™”๋ฅผ ์œ ์˜ )

 

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

    ๐Ÿ‘‰ ์šฐ์„ ์ˆœ์œ„ ํ์˜ ๊ตฌํ˜„์—, ๋‘ ๊ฐ€์ง€ ์„ ํƒ ๊ฐ€๋Šฅ

    1. ํž™ ๊ตฌํ˜„
    2. ๋ฌด์ˆœ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„

  • ํž™์— ๊ธฐ์ดˆํ•œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

    ๊ฐ ์ •์ ์€ ์šฐ์„ ์ˆœ์œ„ ์— ํ•œ๋ฒˆ ์‚ฝ์ž…, ํ•œ๋ฒˆ ์‚ญ์ œ๋˜๋ฉฐ, ๊ฐ๊ฐ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ์— = O(log(N)) ์‹œ๊ฐ„ ์†Œ์š”
    ์šฐ์„ ์ˆœ์œ„ ํ ๋‚ด์˜ ์ •์  z์˜ ํ‚ค๋Š” ์ตœ๋Œ€ deg(z)๋ฒˆ ๊ฐฑ์‹ ๋˜๋ฉฐ ๊ฐ๊ฐ์˜ ํ‚ค ๊ฐฑ์‹ ์— = O(log(N)) ์‹œ๊ฐ„ ์†Œ์š”

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

  • ๋ฌด์ˆœ๋ฆฌ์ŠคํŠธ์— ๊ธฐ์ดˆํ•œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

    ๊ฐ ์ •์ ์€ ์šฐ์„ ์ˆœ์œ„ ํ์— ํ•œ๋ฒˆ ์‚ฝ์ž…, ํ•œ๋ฒˆ ์‚ญ์ œ๋˜๋ฉฐ, ์‚ฝ์ž…๊ณผ ์‚ญ์ œ์— = ์‚ฝ์ž…( O(1) ), ์‚ญ์ œ( O(N) ) ์‹œ๊ฐ„ ์†Œ์š”
    ์šฐ์„ ์ˆœ์œ„ ํ ๋‚ด์˜ ์ •์  z์˜ ํ‚ค๋Š” ์ตœ๋Œ€ deg(z)๋ฒˆ ๊ฐฑ์‹ ๋˜๋ฉฐ ๊ฐ๊ฐ์˜ ํ‚ค ๊ฐฑ์‹ ์— O(1) ์‹œ๊ฐ„ ์†Œ์š”

    ๐Ÿ‘‰ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋กœ ํ‘œํ˜„๋œ ๊ฒฝ์šฐ, Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜ = O(N^2 + M) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰
    ๐Ÿ‘‰ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‹จ์ˆœํ•˜๋‹ค๊ณ  ์ „์ œํ•˜๋ฏ€๋กœ = O(N^2)๋กœ ๋‹จ์ˆœํ™” ๊ฐ€๋Šฅ

  • ๊ฒฐ๊ณผ์ ์œผ๋กœ, ๋‘ ๊ฐ€์ง€ ์šฐ์„ ์ˆœ์œ„ ํ ๊ตฌํ˜„ ๋น„๊ต ( ํž™ vs ๋ฌด์ˆœ๋ฆฌ์ŠคํŠธ )

    ๐Ÿ‘‰ ํž™์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด = O(Mlog(N)) ์‹œ๊ฐ„ ์†Œ์š”
    ๐Ÿ‘‰ ๋ฌด์ˆœ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด = O(N^2) ์‹œ๊ฐ„ ์†Œ์š”

    ๐Ÿ‘‰ ๋‘ ๊ตฌํ˜„ ๋ชจ๋‘ : ์ฝ”๋”ฉํ•˜๊ธฐ ์‰ฝ๊ณ , ์ƒ์ˆ˜์  ์š”์†Œ๋กœ ๋ณด๋ฉด ๋น„์Šทํ•˜๋‹ค.
    ๐Ÿ‘‰ (์ตœ์•…์˜ ๊ฒฝ์šฐ๋งŒ์„) ์ƒ๊ฐํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค.

    1. ํฌ์†Œ๊ทธ๋ž˜ํ”„( ์ฆ‰, M < (N^2)/log(N) )์— ๋Œ€ํ•ด์„œ๋Š” ํž™ ๊ตฌํ˜„์ด ์œ ๋ฆฌ
    2. ๋ฐ€์ง‘๊ทธ๋ž˜ํ”„( ์ฆ‰, M > (N^2)/log(N) )์— ๋Œ€ํ•ด์„œ๋Š” ๊ตฌํ˜„์ด ์œ ๋ฆฌ

 

๐Ÿ“ข  Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ์žˆ๋”๋ผ๋„ ์ž‘๋™ํ•œ๋‹ค.
  • ๋ฐฉํ–ฅ๊ฐ„์„ ์œผ๋กœ ์ „์ œํ•œ๋‹ค = If ) ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์‹ธ์ดํด์ด ์žˆ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • i - ๋ฒˆ์งธ ํšŒ์ „ ์‹œ์— i ๊ฐœ์˜ ๊ฐ„์„ ์„ ์‚ฌ์šฉํ•˜๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ์‹คํ–‰์‹œ๊ฐ„ : O(NM)
  • BFS์— ๊ธฐ์ดˆํ•œ๋‹ค. = ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง€๋Š” ์‹ธ์ดํด์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ์ด๋ฅผ ๋ฐœ๊ฒฌ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ธ์ ‘์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ๊ฐ„์„ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ ๊ทธ๋ž˜ํ”„์—์„œ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.
Alg Bellman-FordShortestPaths(G,s)
	input a weighted digraph G with n vertexs, and a vertex s of G
    output label d(u), for each vertex u of G, s.t. d(u) is the distance from s to u in G

// ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๋ชจ๋“  ์ •์  distance = ∞
for each v ∈ G.vertexs()
    d(v) <- ∞
    
d(s) <- 0						// ์‹œ์ž‘ ์ •์  s์— distance = 0
for i <- 1 to n - 1		
    for each e ∈ G.edges()			// ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๋ชจ๋“  ๊ฐ„์„ ์œผ๋กœ ๋ถ€ํ„ฐ
        // ๊ฐ„์„ ์™„ํ™”
        u <- G.Origin(e)			// u๋Š” ๊ฐ„์„  e์— ์ €์žฅ๋œ origin(์‹œ์ž‘) ์ •์  
        z <- G.opposite(u,e)			// z๋Š” ์ •์  u์™€ ๊ฐ„์„  e๋กœ ๋ถ€ํ„ฐ ์ด์–ด์ง€๋Š” ๋ฐ˜๋Œ€ํŽธ ์ •์ 
        d(z) <- min(d(z),d(u) + w(u,z))	// ํ˜„์žฌ ์ •์  z์˜ distance > u์ •์ ๊นŒ์ง€ ๊ฑฐ์ณ + u์™€ z์ •์ ์œผ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ„์„  e์˜ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ ์ค‘์— ์ž‘์€ ๊ฒƒ์„ z์˜ ๊ฑฐ๋ฆฌ๋กœ ์—…๋ฐ์ดํŠธ

Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ ( BFS ํŠน์ง• : ํ•œ ์นธ์‹ ๋Š˜๋ ค๊ฐ€๋ฉด์„œ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๋Š๋‚Œ ์œ ์˜ )

 

๐Ÿ“ข  ๋ชจ๋“  ์Œ ์ตœ๋‹จ๊ฒฝ๋กœ

  • ๋ชจ๋“  ์Œ ์ตœ๋‹จ๊ฒฝ๋กœ(all-pair shortest paths) ๋ฌธ์ œ : ๊ฐ€์ค‘ ๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„ G์˜ ๋ชจ๋“  ์ •์ ์Œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ
  • ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ์—†๋‹ค๋ฉด, Dijkstra ์•Œ๊ณ ๋ฆฌ์ฆ˜ N๋ฒˆ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค. = O(NMlog(N))
  • ๊ทธ๋ž˜ํ”„์— ์Œ์˜ ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ์žˆ๋‹ค๋ฉด, Bellman-Ford ์•Œ๊ณ ๋ฆฌ์ฆ˜ N๋ฒˆ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค = O((N^2)M)
  • ๋Œ€์•ˆ

    ๐Ÿ‘‰ ๋™์ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Dynamic Programming)์„ ์‚ฌ์šฉ = O(N^3) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.
    ( Floyed-Warshall ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค. )

Alg allPairsShortestPaths(G)
	input a simple weighted digraph G without negative-weight cycle	// ์Œ์˜ ๊ฐ€์ค‘์น˜ ์‹ธ์ดํด ๊ทธ๋ž˜ํ”„
    output a numbering V1,V2,....,Vn of the vertexs of G and a matrix D 
    (s.t D[i,j] = distance of Vi,Vj in G

Let V1,V2,.....,Vn be an arbitrary numbering of the vertexs of G

for i <-1 to n
    for j <- 1 to n
        if(i == j)
            D[i,j] <- 0			// ์ž๊ธฐ ์ž์‹  distacne = 0
        elseif((Vi,Vj) ∈ G.edges())
            D[i,j] <- w(Vi,Vj)
        else
            D[i,j] <- ∞

for k <- 1 to n
    for i <-1 to n
        for j <- 1 to n
            D[i,j] <- min( D[i,j], D[i,k] + D[k,j] )

๋™์ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์„ ์ด์šฉํ•œ ์ตœ๋‹จ๊ฒฝ๋กœ ์ˆ˜ํ–‰ ์˜ˆ์‹œ

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts