MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / shortestPathDAG

Function shortestPathDAG

CPP/graphseries.cpp:280–317  ·  view source on GitHub ↗

shortest path in weighted DAG(Directed Acyclic Graph)

Source from the content-addressed store, hash-verified

278
279// shortest path in weighted DAG(Directed Acyclic Graph)
280void 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
320void dijkstrasAlgorithm(vector<int,int>arr[],int n,int source){

Callers

nothing calls this directly

Calls 3

findTopoSortDFS2Function · 0.85
topMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected