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

Added multistage-graph in dynamic programming #853

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shuvojitss
Copy link

@shuvojitss shuvojitss commented Oct 26, 2024

Pull Request for PyVerse 💡

Requesting to submit a pull request to the PyVerse repository.


Issue Title

Please enter the title of the issue related to your pull request.
Multistage-graph.

  • I have provided the issue title.

Info about the Related Issue

What's the goal of the project?
This program calculates the shortest path from a given source node to a target node in a directed graph using a dynamic programming approach. The graph is represented as an adjacency matrix, where the user provides input for the number of vertices and the adjacency matrix values.

  • I have described the aim of the project.

Name

Please mention your name.
Shuvojit Samanta.

  • I have provided my name.

GitHub ID

Please mention your GitHub ID.
shuvojitss

  • I have provided my GitHub ID.

Email ID

Please mention your email ID for further communication.
[email protected].

  • I have provided my email ID.

Identify Yourself

Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC, SWOC).
GSSOC

  • I have mentioned my participant role.

Closes

Enter the issue number that will be closed through this PR.
Closes: #845

  • I have provided the issue number.

Describe the Add-ons or Changes You've Made

Give a clear description of what you have added or modified.
I have added the algorithm of multistage-graph.
1. Initialization:
The dist[] array is set to INF for all nodes initially, except the target node, which is set to 0 since the distance from the target to itself is zero. This array will be used to record the minimum known distance from each node to the target.
The path[] array is initialized to record the next node in the shortest path for each node, allowing us to reconstruct the full path later.

2. Dynamic Programming Update:
The function evaluates each node’s distance to the target, starting from the node just before the target and moving backward to the source. For each node i, it checks connections to subsequent nodes (j). If the sum of the current node-to-node distance graph[i][j] and dist[j] (distance from j to the target) is less than the current dist[i], it updates dist[i] and records j as the next node in path[i] for the shortest route.

3. Path Reconstruction and Output:
If a valid path exists, the function outputs the shortest path distance from the source to the target. Using the path[] array, it then traces and displays the path from the source to the target by following each recorded next node in path[].

  • I have described my changes.

Type of Change

Select the type of change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Describe how your changes have been tested.
*I have tetsted it in all python editors and its working. I am providing a sample input output to describe it.
Sample Input and output:
Input:

  • Enter the number of vertices in the graph: 12
    Enter row 1 (use 99 for INF): 99 9 7 3 2 99 99 99 99 99 99 99
    Enter row 2 (use 99 for INF): 99 99 99 99 99 4 2 1 99 99 99 99
    Enter row 3 (use 99 for INF): 99 99 99 99 99 2 7 99 99 99 99 99
    Enter row 4 (use 99 for INF): 99 99 99 99 99 99 99 11 99 99 99 99
    Enter row 5 (use 99 for INF): 99 99 99 99 99 99 11 8 99 99 99 99
    Enter row 6 (use 99 for INF): 99 99 99 99 99 99 99 99 6 5 99 99
    Enter row 7 (use 99 for INF): 99 99 99 99 99 99 99 99 4 3 99 99
    Enter row 8 (use 99 for INF): 99 99 99 99 99 99 99 99 99 5 6 99
    Enter row 9 (use 99 for INF): 99 99 99 99 99 99 99 99 99 99 99 4
    Enter row 10 (use 99 for INF): 99 99 99 99 99 99 99 99 99 99 99 2
    Enter row 11 (use 99 for INF): 99 99 99 99 99 99 99 99 99 99 99 5
    Enter row 12 (use 99 for INF): 99 99 99 99 99 99 99 99 99 99 99 0

    Enter the source node: 1
    Enter the target node: 12

  • Output:

    • The shortest path distance from node 1 to node 12 is: 16
      Path: 1 -> 2 -> 7 -> 10 -> 12.
      Screenshot 2024-10-22 184618*
  • I have described my testing process.


Checklist

Please confirm the following:

  • My code follows the guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly wherever it was hard to understand.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added things that prove my fix is effective or that my feature works.
  • Any dependent changes have been merged and published in downstream modules.

Copy link

👋 Thank you for opening this pull request! We're excited to review your contribution. Please give us a moment, and we'll get back to you shortly!

Feel free to join our community on Discord to discuss more!

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.

[Code Addition Request]: MULTISTAGE GRAPH in dynamic programming
1 participant