Skip to content

Commit 2a83fec

Browse files
feat: add queue improved bellman ford algorithm
1 parent f4c1677 commit 2a83fec

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ def shortest_paths(graph: Graph, algorithm: str,
700700
'bellman_ford' -> Bellman-Ford algorithm as given in [1].
701701
702702
'dijkstra' -> Dijkstra algorithm as given in [2].
703+
704+
'queue_improved_bellman_ford' -> Queue Improved Bellman-Ford algorithm as given in [3].
703705
source: str
704706
The name of the source the node.
705707
target: str
@@ -742,6 +744,7 @@ def shortest_paths(graph: Graph, algorithm: str,
742744
743745
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
744746
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
747+
.. [3] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Improvements
745748
"""
746749
raise_if_backend_is_not_python(
747750
shortest_paths, kwargs.get('backend', Backend.PYTHON))
@@ -811,6 +814,37 @@ def _dijkstra_adjacency_list(graph: Graph, start: str, target: str):
811814

812815
_dijkstra_adjacency_matrix = _dijkstra_adjacency_list
813816

817+
def _queue_improved_bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tuple:
818+
distances, predecessor, visited = {}, {}, {}
819+
820+
for v in graph.vertices:
821+
distances[v] = float('inf')
822+
predecessor[v] = None
823+
visited[v] = False
824+
distances[source] = 0
825+
826+
que = Queue([source])
827+
828+
while que:
829+
u = que.popleft()
830+
visited[u] = False
831+
neighbors = graph.neighbors(u)
832+
for neighbor in neighbors:
833+
v = neighbor.name
834+
edge_str = u + '_' + v
835+
if distances[u] != float('inf') and distances[u] + graph.edge_weights[edge_str].value < distances[v]:
836+
distances[v] = distances[u] + graph.edge_weights[edge_str].value
837+
predecessor[v] = u
838+
if not visited[v]:
839+
que.append(v)
840+
visited[v] = True
841+
842+
if target != "":
843+
return (distances[target], predecessor)
844+
return (distances, predecessor)
845+
846+
_queue_improved_bellman_ford_adjacency_matrix = _queue_improved_bellman_ford_adjacency_list
847+
814848
def all_pair_shortest_paths(graph: Graph, algorithm: str,
815849
**kwargs) -> tuple:
816850
"""

pydatastructs/graphs/tests/test_algorithms.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ def _test_shortest_paths_negative_edges(ds, algorithm):
321321
_test_shortest_paths_negative_edges("Matrix", 'bellman_ford')
322322
_test_shortest_paths_positive_edges("List", 'dijkstra')
323323
_test_shortest_paths_positive_edges("Matrix", 'dijkstra')
324+
_test_shortest_paths_positive_edges("List", 'queue_improved_bellman_ford')
325+
_test_shortest_paths_positive_edges("Matrix", 'queue_improved_bellman_ford')
326+
_test_shortest_paths_negative_edges("List", 'queue_improved_bellman_ford')
327+
_test_shortest_paths_negative_edges("Matrix", 'queue_improved_bellman_ford')
324328

325329
def test_all_pair_shortest_paths():
326330

0 commit comments

Comments
 (0)