forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
55877e7
commit c6d4b73
Showing
4 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .tarjan import * | ||
from .check_bipartite import * | ||
from .bellman_ford import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
''' | ||
This Bellman-Ford Code is for determination whether we can get | ||
shortest path from given graph or not for single-source shortest-paths problem. | ||
In other words, if given graph has any negative-weight cycle that is reachable | ||
from the source, then it will give answer False for "no solution exits". | ||
For argument graph, it should be a dictionary type | ||
such as | ||
graph = { | ||
'a': {'b': 6, 'e': 7}, | ||
'b': {'c': 5, 'd': -4, 'e': 8}, | ||
'c': {'b': -2}, | ||
'd': {'a': 2, 'c': 7}, | ||
'e': {'b': -3} | ||
} | ||
''' | ||
|
||
def bellman_ford(graph, source): | ||
weight = {} | ||
pre_node = {} | ||
|
||
initialize_single_source(graph, source, weight, pre_node) | ||
|
||
for i in range(1, len(graph)): | ||
for node in graph: | ||
for adjacent in graph[node]: | ||
if weight[adjacent] > weight[node] + graph[node][adjacent]: | ||
weight[adjacent] = weight[node] + graph[node][adjacent] | ||
pre_node[adjacent] = node | ||
|
||
for node in graph: | ||
for adjacent in graph[node]: | ||
if weight[adjacent] > weight[node] + graph[node][adjacent]: | ||
return False | ||
|
||
return True | ||
|
||
def initialize_single_source(graph, source, weight, pre_node): | ||
|
||
for node in graph: | ||
weight[node] = float('inf') | ||
pre_node[node] = None | ||
|
||
weight[source] = 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters