Added multistage-graph in dynamic programming #853
+47
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
Name
Please mention your name.
Shuvojit Samanta.
GitHub ID
Please mention your GitHub ID.
shuvojitss
Email ID
Please mention your email ID for further communication.
[email protected].
Identify Yourself
Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC, SWOC).
GSSOC
Closes
Enter the issue number that will be closed through this PR.
Closes: #845
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[].
Type of Change
Select the type of change:
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:
Path: 1 -> 2 -> 7 -> 10 -> 12.
*
I have described my testing process.
Checklist
Please confirm the following: