-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#289 add floyd warshall algorithm #302
#289 add floyd warshall algorithm #302
Conversation
…in dijsktra algorithm
Could you please resolve the conflicts? |
What things to change? |
Codecov Report
@@ Coverage Diff @@
## master #302 +/- ##
=============================================
+ Coverage 98.855% 98.866% +0.011%
=============================================
Files 25 25
Lines 2970 2999 +29
=============================================
+ Hits 2936 2965 +29
Misses 34 34
|
pydatastructs/graphs/algorithms.py
Outdated
@@ -762,6 +767,40 @@ def _dijkstra_adjacency_list(graph: Graph, start: str, target: str): | |||
|
|||
_dijkstra_adjacency_matrix = _dijkstra_adjacency_list | |||
|
|||
def _floyd_warshall_adjacency_list(graph: Graph, source: str, target: str): | |||
dist, predecessor, dist_temp, pred_temp = dict(), dict(), dict(), dict() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unable to understand the purpose of dist_temp
, pred_temp
. In L778 to L780 if they are being copied to dist[v]
and predecessor[v]
. May be, we could have directly followed, https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Pseudocode_[11] word to word.
_test_shortest_paths("List", 'floyd_warshall') | ||
_test_shortest_paths("Matrix", 'floyd_warshall') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
floyd_warshall
is a pair wise shortest path algorithm so tests of dijkstra
and bellman_ford
may not work. As can be seen, API for both are different.
We need to define, |
References to other Issues or PRs or Relevant literature
Fixes #289
Brief description of what is fixed or changed
I added _floyd_warshall_adjacency_list function and _floyd_warshall_adjacency_matrix function in pydatastructs.graphs.algorithms to determine the distances and predecessors for given source and target.
Added shortest_path construction function to construct the shortest path from the predecessor's details determined using the function _floyd_warshall_adjacency_list or _floyd_warshall_adjacency_matrix.
In that floyd_warshall algorithm, function added conditions for checking infinity cases to avoid unnecessary computations when adding with other valid numbers.
Other comments
The Floyd-Warshall algorithm can be applied on a graph in a way similar to that of the Bellman-Ford algorithm.
Example: shortest_paths(G, 'floyd_warshall', 'V1')
A small modification will be required in the shortest_paths function in order to utilize the all pair shortest path advantage of the Floyd-Warshall algorithm. The current implementation of shortest_paths function supports only the single-source shortest path nature of the Bellman-Ford algorithm.
It works for both Source and target cases and also Dynamic programming approach is used. That condition checking of infinity helps to avoid key_error problems.