Skip to content

Commit c6d4b73

Browse files
ahnhyunjun417keon
authored andcommitted
Add bellman-ford algorithm (keon#601)
* fix the comment on line 33 (left->right) * [Add] bellman-ford algorithm * Update bellman_ford.py * Update merge_sort.py * Update README.md * Update bellman_ford.py * Update test_graph.py * Update bellman_ford.py * Update test_graph.py * Update test_graph.py * Update test_graph.py * Update __init__.py * Update test_graph.py * Update bellman_ford.py * Update test_graph.py
1 parent 55877e7 commit c6d4b73

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ If you want to uninstall algorithms, it is as simple as:
160160
- [satisfiability](algorithms/graph/satisfiability.py)
161161
- [tarjan](algorithms/graph/tarjan.py)
162162
- [traversal](algorithms/graph/traversal.py)
163+
- [bellman_ford](algorithms/graph/bellman_ford.py)
163164
- [heap](algorithms/heap)
164165
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
165166
- [skyline](algorithms/heap/skyline.py)

algorithms/graph/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .tarjan import *
22
from .check_bipartite import *
3+
from .bellman_ford import *

algorithms/graph/bellman_ford.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
This Bellman-Ford Code is for determination whether we can get
3+
shortest path from given graph or not for single-source shortest-paths problem.
4+
In other words, if given graph has any negative-weight cycle that is reachable
5+
from the source, then it will give answer False for "no solution exits".
6+
For argument graph, it should be a dictionary type
7+
such as
8+
graph = {
9+
'a': {'b': 6, 'e': 7},
10+
'b': {'c': 5, 'd': -4, 'e': 8},
11+
'c': {'b': -2},
12+
'd': {'a': 2, 'c': 7},
13+
'e': {'b': -3}
14+
}
15+
'''
16+
17+
def bellman_ford(graph, source):
18+
weight = {}
19+
pre_node = {}
20+
21+
initialize_single_source(graph, source, weight, pre_node)
22+
23+
for i in range(1, len(graph)):
24+
for node in graph:
25+
for adjacent in graph[node]:
26+
if weight[adjacent] > weight[node] + graph[node][adjacent]:
27+
weight[adjacent] = weight[node] + graph[node][adjacent]
28+
pre_node[adjacent] = node
29+
30+
for node in graph:
31+
for adjacent in graph[node]:
32+
if weight[adjacent] > weight[node] + graph[node][adjacent]:
33+
return False
34+
35+
return True
36+
37+
def initialize_single_source(graph, source, weight, pre_node):
38+
39+
for node in graph:
40+
weight[node] = float('inf')
41+
pre_node[node] = None
42+
43+
weight[source] = 0
44+

tests/test_graph.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from algorithms.graph import Tarjan
22
from algorithms.graph import check_bipartite
33
from algorithms.graph.dijkstra import Dijkstra
4+
from algorithms.graph import bellman_ford
45

56
import unittest
67

@@ -74,4 +75,26 @@ def test_dijkstra(self):
7475
[0, 0, 2, 0, 0, 0, 6, 7, 0]
7576
];
7677

77-
self.assertEqual(g.dijkstra(0), [0, 4, 12, 19, 21, 11, 9, 8, 14]);
78+
self.assertEqual(g.dijkstra(0), [0, 4, 12, 19, 21, 11, 9, 8, 14]);
79+
80+
class TestBellmanFord(unittest.TestCase):
81+
def test_bellman_ford(self):
82+
graph1 = {
83+
'a': {'b': 6, 'e': 7},
84+
'b': {'c': 5, 'd': -4, 'e': 8},
85+
'c': {'b': -2},
86+
'd': {'a': 2, 'c': 7},
87+
'e': {'b': -3}
88+
}
89+
90+
self.assertEqual(True, bellman_ford(graph1, 'a'))
91+
92+
graph2 = {
93+
'a': {'d': 3, 'e': 4},
94+
'b': {'a': 7, 'e':2},
95+
'c': {'a': 12, 'd':9, 'e':11},
96+
'd': {'c': 5, 'e': 11},
97+
'e': {'a': 7, 'b': 5, 'd': 1}
98+
}
99+
100+
self.assertEqual(True, bellman_ford(graph2, 'a'))

0 commit comments

Comments
 (0)