Skip to content
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

Merged

Conversation

Kiran-Venkatesh
Copy link
Contributor

@Kiran-Venkatesh Kiran-Venkatesh commented Nov 14, 2020

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.

@czgdp1807
Copy link
Member

Could you please resolve the conflicts?

@Kiran-Venkatesh
Copy link
Contributor Author

What things to change?

@codecov
Copy link

codecov bot commented Nov 15, 2020

Codecov Report

Merging #302 (db2ab80) into master (8877bf7) will increase coverage by 0.011%.
The diff coverage is 100.000%.

@@              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               
Impacted Files Coverage Δ
pydatastructs/graphs/__init__.py 100.000% <ø> (ø)
pydatastructs/graphs/algorithms.py 99.491% <100.000%> (+0.040%) ⬆️

Impacted file tree graph

@@ -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()
Copy link
Member

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.

Comment on lines 295 to 296
_test_shortest_paths("List", 'floyd_warshall')
_test_shortest_paths("Matrix", 'floyd_warshall')
Copy link
Member

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.

@czgdp1807
Copy link
Member

We need to define, pair_wise_shortest_path(graph: Graph) and that should call, __floyd_warshall_adjacency_list. APIs for bellman_ford and dijsktra will not work because they are single source shortest path algorithms.

@czgdp1807 czgdp1807 merged commit 3f3faad into codezonediitj:master Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Floyd–Warshall algorithm
2 participants