|
| 1 | +--- |
| 2 | + |
| 3 | +id: multistage-graph |
| 4 | +title: Multistage Graph Algorithm |
| 5 | +sidebar_label: Multistage Graph Algorithm |
| 6 | +tags: [python, java, c++, javascript, programming, algorithms, graph, shortest-path, data structures, tutorial, in-depth] |
| 7 | +description: In this tutorial, we will learn about the Multistage Graph Algorithm and its implementation in Python, Java, C++, and JavaScript with detailed explanations and examples. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +# Multistage Graph Algorithm |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +A Multistage Graph is a directed graph in which vertices are divided into stages, with edges only directed from one stage to the next. The Multistage Graph Algorithm finds the shortest path from a source vertex in the first stage to a destination vertex in the last stage. |
| 16 | + |
| 17 | +## Key Concepts |
| 18 | + |
| 19 | +- **Stages**: The graph is divided into several stages. |
| 20 | +- **Directed Edges**: Each edge connects a vertex in one stage to a vertex in the next stage. |
| 21 | +- **Shortest Path**: The path from the source to the destination with the minimum total edge weight. |
| 22 | + |
| 23 | +## Steps |
| 24 | + |
| 25 | +1. Initialize a table to store the shortest path costs from each vertex to the destination. |
| 26 | +2. Start from the destination vertex and move backwards to the source vertex. |
| 27 | +3. For each vertex, calculate the shortest path cost to the destination by considering all possible paths through the subsequent stages. |
| 28 | +4. The final value at the source vertex will be the shortest path cost. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +## Pseudocode |
| 33 | + |
| 34 | +Here’s the pseudocode for the Multistage Graph Algorithm: |
| 35 | + |
| 36 | +```python |
| 37 | +function multistage_graph(graph, stages): |
| 38 | + n = number of vertices in graph |
| 39 | + cost = array of size n with initial values as infinity |
| 40 | + cost[destination] = 0 |
| 41 | + |
| 42 | + for each vertex from destination to source: |
| 43 | + for each edge from vertex: |
| 44 | + cost[vertex] = min(cost[vertex], edge cost + cost[edge to vertex]) |
| 45 | + |
| 46 | + return cost[source] |
| 47 | +``` |
| 48 | + |
| 49 | +## Implementation in Various Languages |
| 50 | + |
| 51 | +### Python |
| 52 | + |
| 53 | +```python |
| 54 | +def multistage_graph(graph, stages): |
| 55 | + n = len(graph) |
| 56 | + cost = [float('inf')] * n |
| 57 | + cost[-1] = 0 # Cost to reach destination from itself is 0 |
| 58 | + |
| 59 | + for i in range(n - 2, -1, -1): |
| 60 | + for j in range(i + 1, n): |
| 61 | + if graph[i][j] != float('inf'): |
| 62 | + cost[i] = min(cost[i], graph[i][j] + cost[j]) |
| 63 | + |
| 64 | + return cost[0] |
| 65 | + |
| 66 | +# Example usage |
| 67 | +graph = [ |
| 68 | + [float('inf'), 1, 2, 5, float('inf'), float('inf'), float('inf')], |
| 69 | + [float('inf'), float('inf'), float('inf'), float('inf'), 4, 11, float('inf')], |
| 70 | + [float('inf'), float('inf'), float('inf'), float('inf'), 9, 5, 16], |
| 71 | + [float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), 2], |
| 72 | + [float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), 18], |
| 73 | + [float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), 13], |
| 74 | + [float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), float('inf'), float('inf')] |
| 75 | +] |
| 76 | + |
| 77 | +stages = 4 |
| 78 | +print(multistage_graph(graph, stages)) # Output: 17 |
| 79 | +``` |
| 80 | + |
| 81 | +### Java |
| 82 | + |
| 83 | +```java |
| 84 | +import java.util.Arrays; |
| 85 | + |
| 86 | +public class MultistageGraph { |
| 87 | + public static int multistageGraph(int[][] graph, int stages) { |
| 88 | + int n = graph.length; |
| 89 | + int[] cost = new int[n]; |
| 90 | + Arrays.fill(cost, Integer.MAX_VALUE); |
| 91 | + cost[n - 1] = 0; // Cost to reach destination from itself is 0 |
| 92 | + |
| 93 | + for (int i = n - 2; i >= 0; i--) { |
| 94 | + for (int j = i + 1; j < n; j++) { |
| 95 | + if (graph[i][j] != Integer.MAX_VALUE) { |
| 96 | + cost[i] = Math.min(cost[i], graph[i][j] + cost[j]); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return cost[0]; |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String[] args) { |
| 105 | + int[][] graph = { |
| 106 | + {Integer.MAX_VALUE, 1, 2, 5, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE}, |
| 107 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 4, 11, Integer.MAX_VALUE}, |
| 108 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 9, 5, 16}, |
| 109 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 2}, |
| 110 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 18}, |
| 111 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 13}, |
| 112 | + {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE} |
| 113 | + }; |
| 114 | + |
| 115 | + int stages = 4; |
| 116 | + System.out.println(multistageGraph(graph, stages)); // Output: 17 |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### C++ |
| 122 | + |
| 123 | +```cpp |
| 124 | +#include <iostream> |
| 125 | +#include <vector> |
| 126 | +#include <algorithm> |
| 127 | +#include <climits> |
| 128 | + |
| 129 | +using namespace std; |
| 130 | + |
| 131 | +int multistageGraph(vector<vector<int>> &graph, int stages) { |
| 132 | + int n = graph.size(); |
| 133 | + vector<int> cost(n, INT_MAX); |
| 134 | + cost[n - 1] = 0; // Cost to reach destination from itself is 0 |
| 135 | + |
| 136 | + for (int i = n - 2; i >= 0; i--) { |
| 137 | + for (int j = i + 1; j < n; j++) { |
| 138 | + if (graph[i][j] != INT_MAX) { |
| 139 | + cost[i] = min(cost[i], graph[i][j] + cost[j]); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return cost[0]; |
| 145 | +} |
| 146 | + |
| 147 | +int main() { |
| 148 | + vector<vector<int>> graph = { |
| 149 | + {INT_MAX, 1, 2, 5, INT_MAX, INT_MAX, INT_MAX}, |
| 150 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, 4, 11, INT_MAX}, |
| 151 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, 9, 5, 16}, |
| 152 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 2}, |
| 153 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 18}, |
| 154 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 13}, |
| 155 | + {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX} |
| 156 | + }; |
| 157 | + |
| 158 | + int stages = 4; |
| 159 | + cout << multistageGraph(graph, stages) << endl; // Output: 17 |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +### JavaScript |
| 166 | + |
| 167 | +```javascript |
| 168 | +function multistageGraph(graph, stages) { |
| 169 | + const n = graph.length; |
| 170 | + const cost = new Array(n).fill(Infinity); |
| 171 | + cost[n - 1] = 0; // Cost to reach destination from itself is 0 |
| 172 | + |
| 173 | + for (let i = n - 2; i >= 0; i--) { |
| 174 | + for (let j = i + 1; j < n; j++) { |
| 175 | + if (graph[i][j] !== Infinity) { |
| 176 | + cost[i] = Math.min(cost[i], graph[i][j] + cost[j]); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return cost[0]; |
| 182 | +} |
| 183 | + |
| 184 | +// Example usage |
| 185 | +const graph = [ |
| 186 | + [Infinity, 1, 2, 5, Infinity, Infinity, Infinity], |
| 187 | + [Infinity, Infinity, Infinity, Infinity, 4, 11, Infinity], |
| 188 | + [Infinity, Infinity, Infinity, Infinity, 9, 5, 16], |
| 189 | + [Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 2], |
| 190 | + [Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 18], |
| 191 | + [Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 13], |
| 192 | + [Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity] |
| 193 | +]; |
| 194 | + |
| 195 | +const stages = 4; |
| 196 | +console.log(multistageGraph(graph, stages)); // Output: 17 |
| 197 | +``` |
| 198 | + |
| 199 | +## Example |
| 200 | + |
| 201 | +Consider a multistage graph with vertices and edges: |
| 202 | + |
| 203 | +```plaintext |
| 204 | +graph = [ |
| 205 | + [∞, 1, 2, 5, ∞, ∞, ∞], |
| 206 | + [∞, ∞, ∞, ∞, 4, 11, ∞], |
| 207 | + [∞, ∞, ∞, ∞, 9, 5, 16], |
| 208 | + [∞, ∞, ∞, ∞, ∞, ∞, 2], |
| 209 | + [∞, ∞, ∞, ∞, ∞, ∞, 18], |
| 210 | + [∞, ∞, ∞, ∞, ∞, ∞, 13], |
| 211 | + [∞, ∞, ∞, ∞, ∞, ∞, ∞] |
| 212 | +] |
| 213 | +``` |
| 214 | + |
| 215 | +1. Start from the destination vertex and move backwards. |
| 216 | +2. Calculate the shortest path cost |
| 217 | + |
| 218 | + for each vertex considering all possible paths. |
| 219 | +3. The shortest path cost from the source vertex will be the result. |
| 220 | + |
| 221 | +The shortest path cost is 17. |
| 222 | + |
| 223 | +## Conclusion |
| 224 | + |
| 225 | +The Multistage Graph Algorithm efficiently finds the shortest path in a graph divided into stages. It operates in $O(V^2)$ time complexity, making it suitable for graphs with a structured stage-based layout. Understanding and implementing this algorithm is essential for solving complex optimization problems in various applications. |
0 commit comments