Skip to content

Commit f4d782c

Browse files
authored
Create Solution.cpp
1 parent a5156af commit f4d782c

File tree

1 file changed

+73
-0
lines changed
  • 23 - Graph Data Structure Problems/12 - Shortest path in Directed Acyclic Graph

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution {
2+
public:
3+
// Helper function to perform a topological sort using DFS
4+
void topologicalSort(int node, vector<int>& visited, stack<int>& Stack, unordered_map<int, list<pair<int, int>>>& adjacencyList) {
5+
visited[node] = true; // Mark the current node as visited
6+
7+
// Traverse all the neighbors of the current node
8+
for (auto neighbour : adjacencyList[node]) {
9+
if (!visited[neighbour.first]) { // If the neighbor is not visited, recursively sort
10+
topologicalSort(neighbour.first, visited, Stack, adjacencyList);
11+
}
12+
}
13+
14+
// Push the current node to the stack after visiting all its neighbors
15+
Stack.push(node);
16+
}
17+
18+
// Helper function to calculate the shortest path from the source node
19+
void getShortestPath(int src, vector<int>& distance, unordered_map<int, list<pair<int, int>>>& adjacencyList, stack<int>& Stack) {
20+
distance[src] = 0; // Set the source node's distance to 0
21+
22+
// Process nodes in topological order
23+
while (!Stack.empty()) {
24+
int currNode = Stack.top(); // Get the current node from the stack
25+
Stack.pop();
26+
27+
// Update distances to all neighbors if the current node has a valid distance
28+
if (distance[currNode] != INT_MAX) {
29+
for (auto neighbour : adjacencyList[currNode]) {
30+
int newDistance = distance[currNode] + neighbour.second; // Calculate new distance
31+
if (newDistance < distance[neighbour.first]) { // If the new distance is shorter, update it
32+
distance[neighbour.first] = newDistance;
33+
}
34+
}
35+
}
36+
}
37+
}
38+
39+
// Main function to find the shortest path in a Directed Acyclic Graph (DAG)
40+
vector<int> shortestPath(int V, int E, vector<vector<int>>& edges) {
41+
// Step 1: Create an adjacency list for the graph
42+
unordered_map<int, list<pair<int, int>>> adjacencyList;
43+
for (auto edge : edges) {
44+
int source = edge[0];
45+
int destination = edge[1];
46+
int weight = edge[2];
47+
adjacencyList[source].push_back({destination, weight}); // Add the edge with weight
48+
}
49+
50+
// Step 2: Perform a topological sort
51+
stack<int> Stack; // Stack to store the topological order
52+
vector<int> visited(V, false); // Visited array to track visited nodes
53+
for (int i = 0; i < V; i++) {
54+
if (!visited[i]) { // For unvisited nodes, perform a topological sort
55+
topologicalSort(i, visited, Stack, adjacencyList);
56+
}
57+
}
58+
59+
// Step 3: Calculate shortest paths from the source node
60+
int src = 0; // Assuming 0 is the source node
61+
vector<int> distance(V, INT_MAX); // Initialize distances to infinity
62+
getShortestPath(src, distance, adjacencyList, Stack);
63+
64+
// Step 4: Replace distances that are still INT_MAX with -1 to indicate no path
65+
for (int& d : distance) {
66+
if (d == INT_MAX) {
67+
d = -1;
68+
}
69+
}
70+
71+
return distance; // Return the vector containing shortest distances
72+
}
73+
};

0 commit comments

Comments
 (0)