shortest path in weighted DAG(Directed Acyclic Graph)
| 278 | |
| 279 | // shortest path in weighted DAG(Directed Acyclic Graph) |
| 280 | void shortestPathDAG(vector<pair<int, int>> arr[], int n, int source) |
| 281 | { |
| 282 | stack<int> s; |
| 283 | vector<int> vis(n, 0); |
| 284 | for (int i = 0; i < n; i++) |
| 285 | { |
| 286 | if (!vis[i]) |
| 287 | { |
| 288 | findTopoSortDFS2(i, arr, s, vis); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | vector<int> distance(n, INT_MAX); |
| 293 | distance[source] = 0; |
| 294 | while (!s.empty()) |
| 295 | { |
| 296 | int temp = s.top(); |
| 297 | s.pop(); |
| 298 | |
| 299 | if (distance[temp] != INT_MAX) |
| 300 | { |
| 301 | for (auto i : arr[temp]) |
| 302 | { |
| 303 | distance[i.first] = min(distance[i.first], distance[temp] + i.second); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | for (auto i : distance) |
| 309 | { |
| 310 | if (i == INT_MAX) |
| 311 | { |
| 312 | cout << "This node is not reachable"; |
| 313 | } |
| 314 | else |
| 315 | cout << i << " "; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | //shortest path in weighted undirected graph |
| 320 | void dijkstrasAlgorithm(vector<int,int>arr[],int n,int source){ |
nothing calls this directly
no test coverage detected