mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
Reduce the complexity of graphs/bi_directional_dijkstra.py (#8165)
* Reduce the complexity of graphs/bi_directional_dijkstra.py * Try to lower the --max-complexity threshold in the file .flake8 * Lower the --max-complexity threshold in the file .flake8 * updating DIRECTORY.md * updating DIRECTORY.md * Try to lower max-complexity * Try to lower max-complexity * Try to lower max-complexity --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
86b2ab09aa
commit
ac111ee463
|
@ -17,6 +17,32 @@ from typing import Any
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def pass_and_relaxation(
|
||||||
|
graph: dict,
|
||||||
|
v: str,
|
||||||
|
visited_forward: set,
|
||||||
|
visited_backward: set,
|
||||||
|
cst_fwd: dict,
|
||||||
|
cst_bwd: dict,
|
||||||
|
queue: PriorityQueue,
|
||||||
|
parent: dict,
|
||||||
|
shortest_distance: float | int,
|
||||||
|
) -> float | int:
|
||||||
|
for nxt, d in graph[v]:
|
||||||
|
if nxt in visited_forward:
|
||||||
|
continue
|
||||||
|
old_cost_f = cst_fwd.get(nxt, np.inf)
|
||||||
|
new_cost_f = cst_fwd[v] + d
|
||||||
|
if new_cost_f < old_cost_f:
|
||||||
|
queue.put((new_cost_f, nxt))
|
||||||
|
cst_fwd[nxt] = new_cost_f
|
||||||
|
parent[nxt] = v
|
||||||
|
if nxt in visited_backward:
|
||||||
|
if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
|
||||||
|
shortest_distance = cst_fwd[v] + d + cst_bwd[nxt]
|
||||||
|
return shortest_distance
|
||||||
|
|
||||||
|
|
||||||
def bidirectional_dij(
|
def bidirectional_dij(
|
||||||
source: str, destination: str, graph_forward: dict, graph_backward: dict
|
source: str, destination: str, graph_forward: dict, graph_backward: dict
|
||||||
) -> int:
|
) -> int:
|
||||||
|
@ -51,53 +77,36 @@ def bidirectional_dij(
|
||||||
if source == destination:
|
if source == destination:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
while queue_forward and queue_backward:
|
while not queue_forward.empty() and not queue_backward.empty():
|
||||||
while not queue_forward.empty():
|
_, v_fwd = queue_forward.get()
|
||||||
_, v_fwd = queue_forward.get()
|
|
||||||
|
|
||||||
if v_fwd not in visited_forward:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
visited_forward.add(v_fwd)
|
visited_forward.add(v_fwd)
|
||||||
|
|
||||||
while not queue_backward.empty():
|
_, v_bwd = queue_backward.get()
|
||||||
_, v_bwd = queue_backward.get()
|
|
||||||
|
|
||||||
if v_bwd not in visited_backward:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
visited_backward.add(v_bwd)
|
visited_backward.add(v_bwd)
|
||||||
|
|
||||||
# forward pass and relaxation
|
shortest_distance = pass_and_relaxation(
|
||||||
for nxt_fwd, d_forward in graph_forward[v_fwd]:
|
graph_forward,
|
||||||
if nxt_fwd in visited_forward:
|
v_fwd,
|
||||||
continue
|
visited_forward,
|
||||||
old_cost_f = cst_fwd.get(nxt_fwd, np.inf)
|
visited_backward,
|
||||||
new_cost_f = cst_fwd[v_fwd] + d_forward
|
cst_fwd,
|
||||||
if new_cost_f < old_cost_f:
|
cst_bwd,
|
||||||
queue_forward.put((new_cost_f, nxt_fwd))
|
queue_forward,
|
||||||
cst_fwd[nxt_fwd] = new_cost_f
|
parent_forward,
|
||||||
parent_forward[nxt_fwd] = v_fwd
|
shortest_distance,
|
||||||
if nxt_fwd in visited_backward:
|
)
|
||||||
if cst_fwd[v_fwd] + d_forward + cst_bwd[nxt_fwd] < shortest_distance:
|
|
||||||
shortest_distance = cst_fwd[v_fwd] + d_forward + cst_bwd[nxt_fwd]
|
|
||||||
|
|
||||||
# backward pass and relaxation
|
shortest_distance = pass_and_relaxation(
|
||||||
for nxt_bwd, d_backward in graph_backward[v_bwd]:
|
graph_backward,
|
||||||
if nxt_bwd in visited_backward:
|
v_bwd,
|
||||||
continue
|
visited_backward,
|
||||||
old_cost_b = cst_bwd.get(nxt_bwd, np.inf)
|
visited_forward,
|
||||||
new_cost_b = cst_bwd[v_bwd] + d_backward
|
cst_bwd,
|
||||||
if new_cost_b < old_cost_b:
|
cst_fwd,
|
||||||
queue_backward.put((new_cost_b, nxt_bwd))
|
queue_backward,
|
||||||
cst_bwd[nxt_bwd] = new_cost_b
|
parent_backward,
|
||||||
parent_backward[nxt_bwd] = v_bwd
|
shortest_distance,
|
||||||
|
)
|
||||||
if nxt_bwd in visited_forward:
|
|
||||||
if cst_bwd[v_bwd] + d_backward + cst_fwd[nxt_bwd] < shortest_distance:
|
|
||||||
shortest_distance = cst_bwd[v_bwd] + d_backward + cst_fwd[nxt_bwd]
|
|
||||||
|
|
||||||
if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
|
if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
|
||||||
break
|
break
|
||||||
|
|
|
@ -61,7 +61,7 @@ show-source = true
|
||||||
target-version = "py311"
|
target-version = "py311"
|
||||||
|
|
||||||
[tool.ruff.mccabe] # DO NOT INCREASE THIS VALUE
|
[tool.ruff.mccabe] # DO NOT INCREASE THIS VALUE
|
||||||
max-complexity = 20 # default: 10
|
max-complexity = 17 # default: 10
|
||||||
|
|
||||||
[tool.ruff.pylint] # DO NOT INCREASE THESE VALUES
|
[tool.ruff.pylint] # DO NOT INCREASE THESE VALUES
|
||||||
max-args = 10 # default: 5
|
max-args = 10 # default: 5
|
||||||
|
|
Loading…
Reference in New Issue
Block a user