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

feat: Implement Yen's Algorithm for K-Shortest Paths #625

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

asmit27rai
Copy link

@asmit27rai asmit27rai commented Mar 10, 2025

✨ Feature: Yen's Algorithm for K-Shortest Paths

FIXES: #622

📌 Description

This PR introduces Yen's Algorithm, which finds the K shortest paths between a source and destination node in a graph. The algorithm efficiently computes multiple alternative paths, making it useful for applications such as:

  • Network Routing (finding alternative paths in case of congestion)
  • Transportation Planning (evaluating different routes)
  • Logistics Optimization (determining the best delivery paths)

🛠️ Changes

  • Implemented Yen's Algorithm in the graph module.
  • Added helper functions to extract deviations and manage path modifications.
  • Included test cases to validate correctness and efficiency.

- Added Yen's Algorithm implementation to find the K shortest paths between two nodes in a graph.
- Supports applications in network routing, transportation planning, and logistics.
- Includes test cases to validate the correctness of the implementation.
- Optimized for performance and usability within the graph algorithms module.
@@ -11,6 +11,8 @@
from pydatastructs.graphs.graph import Graph
from pydatastructs.linear_data_structures.algorithms import merge_sort_parallel
from pydatastructs import PriorityQueue
from copy import deepcopy
import heapq
Copy link
Member

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 think PriorityQueue and heapq are the same?

from pydatastructs.utils.raises_util import raises
import pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to import this.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we support matrix representation of graphs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Yen's Algorithm
2 participants