Skip to content

Commit a500b57

Browse files
authored
Create main.cpp
1 parent 5dd9a17 commit a500b57

File tree

1 file changed

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

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
#include <list>
5+
#include <stack>
6+
#include <unordered_map>
7+
8+
using namespace std;
9+
10+
// Class representing a graph
11+
class Graph {
12+
public:
13+
// Adjacency list to store the graph as source -> {destination, weight}
14+
unordered_map<int, list<pair<int, int>>> adjacencyList;
15+
16+
// Method to add a directed edge with a weight
17+
void addEdge(int source, int destination, int weight) {
18+
pair<int, int> edge = make_pair(destination, weight); // Create a pair of destination and weight
19+
adjacencyList[source].push_back(edge); // Add the edge to the adjacency list
20+
}
21+
22+
// Method to print the adjacency list for visualization
23+
void printAdjacencyList() {
24+
for (auto vertex : adjacencyList) {
25+
cout << vertex.first << " -> ";
26+
for (auto neighbour : vertex.second) {
27+
cout << "(" << neighbour.first << ", " << neighbour.second << "), ";
28+
}
29+
cout << endl;
30+
}
31+
}
32+
33+
// Helper method for topological sorting (DFS-based)
34+
void topologicalSort(int node, vector<int>& visited, stack<int>& sortedNodes) {
35+
visited[node] = true; // Mark the current node as visited
36+
37+
// Recursively visit all unvisited neighbors
38+
for (auto neighbour : adjacencyList[node]) {
39+
if (!visited[neighbour.first]) {
40+
topologicalSort(neighbour.first, visited, sortedNodes);
41+
}
42+
}
43+
44+
// Push the current node onto the stack after processing all its neighbors
45+
sortedNodes.push(node);
46+
}
47+
48+
// Method to calculate the shortest path from a given source in a DAG
49+
void getShortestPath(int source, vector<int>& distance, stack<int>& sortedNodes) {
50+
distance[source] = 0; // Distance to the source itself is 0
51+
52+
// Process nodes in topological order
53+
while (!sortedNodes.empty()) {
54+
int currentNode = sortedNodes.top();
55+
sortedNodes.pop();
56+
57+
// Update distances for all neighbors if the current node has a valid distance
58+
if (distance[currentNode] != INT_MAX) {
59+
for (auto neighbour : adjacencyList[currentNode]) {
60+
if (distance[currentNode] + neighbour.second < distance[neighbour.first]) {
61+
distance[neighbour.first] = distance[currentNode] + neighbour.second;
62+
}
63+
}
64+
}
65+
}
66+
}
67+
};
68+
69+
int main() {
70+
Graph graph;
71+
72+
// Add edges with weights to the graph
73+
graph.addEdge(0, 1, 5);
74+
graph.addEdge(0, 2, 3);
75+
graph.addEdge(1, 2, 2);
76+
graph.addEdge(1, 3, 6);
77+
graph.addEdge(2, 3, 7);
78+
graph.addEdge(2, 4, 4);
79+
graph.addEdge(2, 5, 2);
80+
graph.addEdge(3, 4, -1);
81+
graph.addEdge(4, 5, -2);
82+
83+
// Print the adjacency list to visualize the graph
84+
graph.printAdjacencyList();
85+
86+
int numNodes = 6; // Number of nodes in the graph
87+
stack<int> sortedNodes; // Stack to store nodes in topological order
88+
vector<int> visited(numNodes, 0); // Visited array to keep track of visited nodes
89+
90+
// Perform topological sort for all unvisited nodes
91+
for (int i = 0; i < numNodes; i++) {
92+
if (!visited[i]) {
93+
graph.topologicalSort(i, visited, sortedNodes);
94+
}
95+
}
96+
97+
int source = 1; // Source node for shortest path calculation
98+
vector<int> distance(numNodes, INT_MAX); // Initialize distances to infinity
99+
100+
// Compute the shortest paths from the source
101+
graph.getShortestPath(source, distance, sortedNodes);
102+
103+
// Print the shortest path distances
104+
cout << "Shortest Path is : ";
105+
for (int i = 0; i < numNodes; i++) {
106+
if (distance[i] == INT_MAX) {
107+
cout << "INF ";
108+
} else {
109+
cout << distance[i] << " ";
110+
}
111+
}
112+
cout << endl;
113+
114+
return 0;
115+
}

0 commit comments

Comments
 (0)