3.5 Graph - 3.5.3 Bellman Ford’s Algorithm

2024. 1. 29. 19:55ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.5 Graph - 3.5.3 Bellman Ford’s Algorithm

Bellman Ford’s Algorithm

๐Ÿ’กBellman ford’s algorithm is a graph algorithm that finds the shortest path from a source vertex to all other vertices in a graph. It is a dynamic programming algorithm that uses a bottom-up approach to find the shortest path. It is similar to Dijkstra’s algorithm but it can handle negative weights. It is also similar to Floyd-Warshall’s algorithm but it can handle negative weights and it is faster than Floyd-Warshall’s algorithm.

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

 

์ „ ๊ธ€๊นŒ์ง€ DFS ์™€ BFS๋ฅผ ๋‹ค๋ฃจ์—ˆ๋Š”๋ฐ, ์ด์ œ๋Š” ์•ฝ๊ฐ„ ๋ฐœ์ „๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 

๋ฒจ๋งŒํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

A* ์•Œ๊ณ ๋ฆฌ์ฆ˜ (A ์Šคํƒ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

3๊ฐ€์ง€๋ฅผ ๋‹ค๋ค„ ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋จผ์ € 3.5.3 ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๋ชจ๋“  ์ •์  ๊ฐ„์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ํ•œ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋ฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ธฐ๋ณธ ์›๋ฆฌ๋Š”

"Relaxation" ์ด๋ผ๋Š” ๊ฐœ๋…์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹จ์–ด์˜ ์˜๋ฏธ๋Š” ํœด์‹, ๊ฒฝ๊ฐ, ์™„ํ™” ์ธ๋ฐ, ์ด ๊ธ€์—์„œ๋Š” ์™„ํ™”๊ฐ€ ์ ๋‹นํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

(์•„๋ž˜ ๋งํฌ์— Lagrangian Relaxation(๋ผ๊ทธ๋ž‘์ฃผ ์™„ํ™”๋ฒ•)์— ๋Œ€ํ•œ ์„ค๋ช…์ด ์ž˜ ๋‚˜์™€์žˆ๋‹ค.

https://charstring.tistory.com/481)

 

๋ผ๊ทธ๋ž‘์ฃผ ์™„ํ™”๋ฒ•(Lagrangian Relaxation)

๋ผ๊ทธ๋ž‘์ฃผ ์—ญํ•™ https://youtu.be/QXf95_EKS6E 1788 *๋ผ๊ทธ๋ž‘์ง€์–ธ ๊ฐ’์œผ๋กœ ๋ฌผ์ฒด์˜ ์šด๋™์„ ์„ค๋ช… ๋ผ๊ทธ๋ž‘์ง€์–ธ : ์šด๋™ ์—๋„ˆ์ง€ - ์œ„์น˜ ์—๋„ˆ์ง€ = ์Šค์นผ๋ผ ๊ฐ’ ์Šค์นผ๋ผ ๊ฐ’: ์งˆ๋Ÿ‰ ๋˜๋Š” ์˜จ๋„์™€ ๊ฐ™์ด ๋ฒกํ„ฐ์™€ ๋‹ฌ๋ฆฌ ๋ฐฉํ–ฅ์„ ๊ฐ€

charstring.tistory.com

์ด ๊ธ€์—์„œ Relaxation์€ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํ•œ ์ •์ ์—์„œ ๋‹ค๋ฅธ ์ •์ ๊นŒ์ง€์˜ ํ˜„์žฌ๊นŒ์ง€ ์•Œ๋ ค์ง„ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๋ฐธ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด๋Ÿฐ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

1. ์‹œ์ž‘ ์ •์ ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ •์ ์˜ ๊ฑฐ๋ฆฌ ๊ฐ’์„ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

2. ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•ด, ๋งŒ์•ฝ ์‹œ์ž‘ ์ •์ ์—์„œ ํ•ด๋‹น ๊ฐ„์„ ์˜ ์‹œ์ž‘ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์™€ ํ•ด๋‹น ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด ํ•ด๋‹น ๊ฐ„์„ ์˜ ๋ ์ •์ ๊นŒ์ง€์˜ ํ˜„์žฌ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ๋ ์ •์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•ฉ๋‹ˆ๋‹ค.

3. ์ด ๊ณผ์ •์„ ์ •์ ์˜ ์ˆ˜ -1๋ฒˆ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

4. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์Œ์˜ ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์Œ์˜ ์‚ฌ์ดํด์ด ์žˆ๋‹ค๋ฉด, ์Œ์˜ ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

 

In Python
def BellmanFord(graph, V, E, src):
     
    # 1๋‹จ๊ณ„ : src๋ถ€ํ„ฐ ๋ชจ๋“  ๋ฒ„ํ…์Šค๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฌดํ•œ๋Œ€๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
    dis = [float("Inf")] * V
    dis[src] = 0
 
    # 2๋‹จ๊ณ„: ๋ชจ๋“  ๊ฐ€์žฅ์ž๋ฆฌ V - 1๋ฐฐ ์™„ํ™”ํ•˜๊ธฐ
    for i in range(V - 1):
        for j in range(E):
            if dis[graph[j][0]] != float("Inf") and dis[graph[j][0]] + graph[j][2] < dis[graph[j][1]]:
                dis[graph[j][1]] = dis[graph[j][0]] + graph[j][2]
 
    # 3๋‹จ๊ณ„: ์Œ์˜ ์‚ฌ์ดํด ํ™•์ธ
    for i in range(E):
        x = graph[i][0]
        y = graph[i][1]
        weight = graph[i][2]
        if dis[x] != float("Inf") and dis[x] + weight < dis[y]:
            print("์Œ์˜ ๊ฐ€์ค‘์น˜ ์‚ฌ์ดํด ์žˆ์œผ์š”!")
            return

    # ์Œ์˜ ๋ฌด๊ฒŒ ์ฃผ๊ธฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋ฉด ๊ฑฐ๋ฆฌ์™€ ์ด์ „ ๋ฐฐ์—ด์„ ์ถœ๋ ฅ
    print("์‹œ์ž‘ ์ •์ ์œผ๋กœ ๋ถ€ํ„ฐ ์ตœ๋‹จ๊ฑฐ๋ฆฌ")
    for i in range(V):
        print("%d\t\t%d" % (i, dis[i]))

 

 

0 - 0 ์‹œ์ž‘ ์ •์ ์ธ 0์—์„œ 0๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ 0

1 -1  ์‹œ์ž‘ ์ •์  0 ์—์„œ 1๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ -1 

 

๊ทธ๋ž˜ํ”„๋ฅผ ์‹œ๊ฐํ™” ํ•œ๋‹ค๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ ์ •์ ์ด 0, ๋„์ฐฉ ์ •์ ์ด 2์ผ๋•Œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด์ž.

์ •์  0 ์—์„œ ์ •์ 1์„ ๊ฑฐ์ณ ์ •์ 2๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์ด ๊ฐ€์ค‘์น˜๋Š” -1 +3 = 2 ๊ฐ€๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, ์ •์ 0 ์—์„œ ๋ฐ”๋กœ ์ €์—Šใ…2๋กœ ์ด๋™ํ•˜๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 4์ด๋‹ค.

๋”ฐ๋ผ์„œ, 0-2 ๋ณด๋‹ค 0-1-2 ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋” ์งง์œผ๋ฏ€๋กœ, ์‹œ์ž‘ ์ •์  0์—์„œ ์ •์  2๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” 2๊ฐ€ ๋œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์Œ์˜ ๊ฐ€์ค‘์น˜ ์‚ฌ์ดํด์ด ๋‚˜ํƒ€๋‚œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

edges = [(0, 1, -1), (1, 2, -3), (2, 3, 2), (3, 1, -1)]

์ด๋Ÿฐ ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค.

 

์œ„ ๊ทธ๋ž˜ํ”„์—์„œ๋Š”

์ •์  1์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ •์  2, ์ •์  3์„ ๊ฑฐ์ณ ๋‹ค์‹œ ์ •์  1๋กœ ๋Œ์•„์˜ค๋Š” ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉฐ, ์ด ์‚ฌ์ดํด์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์€ -1 + 2 - 3 = -2๋กœ ์Œ์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ณ„์† 1-2-3-1-2-3-... ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€๋ฉด ๊ณ„์† ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์•„์ง€๋ฏ€๋กœ ์˜ค๋ฅ˜์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ™œ์šฉ


์ด ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์œ ์šฉํ•œ์ ์€ '์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค' ์ž…๋‹ˆ๋‹ค.

๋‹ค์‹œ๋งํ•ด, ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ ๊ฐ„์„ ์ด ํฌํ•จ๋œ ๊ทธ๋ž˜ํ”„์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋„คํŠธ์›Œํฌ ๋ผ์šฐํŒ…, ํ†ตํ™” ๊ฑฐ๋ž˜, ๊ฒฝ๋กœ ์ฐพ๊ธฐ ๋“ฑ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.5 Graph - 3.5.5 A* Algorithm  (0) 2024.01.31
3.5 Graph - 3.5.4 Dijkstraโ€™s Algorithm  (2) 2024.01.30
3.5 Graph - 3.5.2 Depth First Search(DFS)  (1) 2024.01.29
3.5 Graph - 3.5.1 Breadth First Search(BFS)  (0) 2024.01.28
3.5 Graph & 3.6 Tree  (0) 2024.01.27