-
Notifications
You must be signed in to change notification settings - Fork 313
feat: Implement Yen's Algorithm for K-Shortest Paths #625
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
breadth_first_search_parallel, minimum_spanning_tree, | ||
minimum_spanning_tree_parallel, strongly_connected_components, | ||
depth_first_search, shortest_paths,all_pair_shortest_paths, topological_sort, | ||
topological_sort_parallel, max_flow) | ||
topological_sort_parallel, max_flow, yen_algorithm) | ||
from pydatastructs.utils.raises_util import raises | ||
import pytest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to import this. |
||
|
||
def test_breadth_first_search(): | ||
|
||
|
@@ -448,3 +449,40 @@ def _test_max_flow(ds, algorithm): | |
_test_max_flow("Matrix", "edmonds_karp") | ||
_test_max_flow("List", "dinic") | ||
_test_max_flow("Matrix", "dinic") | ||
|
||
def test_yen_algorithm(): | ||
""" | ||
Test function for Yen's Algorithm to find K shortest paths. | ||
""" | ||
def _test_yen_algorithm(ds): | ||
import pydatastructs.utils.misc_util as utils | ||
GraphNode = getattr(utils, "Adjacency" + ds + "GraphNode") | ||
|
||
V1 = GraphNode("V1") | ||
V2 = GraphNode("V2") | ||
V3 = GraphNode("V3") | ||
V4 = GraphNode("V4") | ||
V5 = GraphNode("V5") | ||
|
||
G = Graph(V1, V2, V3, V4, V5) | ||
|
||
G.add_edge(V1.name, V2.name, 1) | ||
G.add_edge(V2.name, V3.name, 2) | ||
G.add_edge(V3.name, V4.name, 1) | ||
G.add_edge(V4.name, V5.name, 3) | ||
G.add_edge(V1.name, V3.name, 4) | ||
G.add_edge(V3.name, V5.name, 2) | ||
|
||
k_shortest_paths = yen_algorithm(G, V1.name, V5.name, K=3) | ||
|
||
expected_paths = [ | ||
['V1', 'V2', 'V3', 'V5'], | ||
['V1', 'V3', 'V5'], | ||
['V1', 'V2', 'V3', 'V4', 'V5'] | ||
] | ||
|
||
assert len(k_shortest_paths) == 3, "Expected 3 shortest paths" | ||
for i, path in enumerate(k_shortest_paths): | ||
assert path == expected_paths[i], f"Path {i} does not match expected path. Got {path}, expected {expected_paths[i]}" | ||
|
||
_test_yen_algorithm("List") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we support matrix representation of graphs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The AdjacencyMatrix implementation does not support dynamic modifications like removing vertices or edges during runtime. Since Yen's algorithm requires modifying the graph (e.g., removing edges and vertices to find alternative paths), it cannot work with the AdjacencyMatrix implementation in its current form. |
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.
We do have
heapq
implemented in PyDataStructs? I thinkPriorityQueue
andheapq
are the same?