Skip to content

Commit 6dcd516

Browse files
authored
Create main.cpp
1 parent f4d782c commit 6dcd516

File tree

1 file changed

+107
-0
lines changed
  • 23 - Graph Data Structure Problems/13 - Shortest Path in Undirected Graph

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <iostream>
2+
#include <climits>
3+
#include <set>
4+
#include <list>
5+
#include <vector>
6+
#include <unordered_map>
7+
8+
using namespace std;
9+
10+
// Graph class to represent the graph structure
11+
class graph {
12+
public:
13+
// Adjacency list to store the graph. Each node maps to a list of pairs (neighbor, weight)
14+
unordered_map<int, list<pair<int, int>>> adjacencyList;
15+
16+
// Function to add an edge between two nodes with a specified weight
17+
void addEdge(int source, int destination, int weight) {
18+
// Create pairs representing the edge and its weight
19+
pair<int, int> u = make_pair(destination, weight); // Edge from source to destination
20+
pair<int, int> v = make_pair(source, weight); // Edge from destination to source (since the graph is undirected)
21+
22+
// Add the edge to the adjacency list
23+
adjacencyList[source].push_back(u);
24+
adjacencyList[destination].push_back(v);
25+
}
26+
27+
// Function to print the adjacency list representation of the graph
28+
void printAdjacencyList() {
29+
for (auto vertex : adjacencyList) { // Loop through each vertex in the adjacency list
30+
cout << vertex.first << " -> "; // Print the current vertex
31+
for (auto neighbour : vertex.second) // Loop through its neighbors
32+
cout << "(" << neighbour.first << ", " << neighbour.second << "), "; // Print neighbor and weight
33+
cout << endl;
34+
}
35+
}
36+
37+
// Function to implement Dijkstra's Algorithm
38+
void DijkstraAlgorithm(int node, vector<int>& distance, set<pair<int, int>>& Set) {
39+
// Insert the source node with distance 0 into the set
40+
Set.insert({0, node});
41+
distance[node] = 0; // Initialize the source node's distance to 0
42+
43+
// Process the set until it is empty
44+
while (!Set.empty()) {
45+
// Get the node with the smallest distance (top of the set)
46+
auto top = *(Set.begin());
47+
int nodeDistance = top.first; // Distance of the current node
48+
int currNode = top.second; // Current node being processed
49+
50+
// Remove the processed node from the set
51+
Set.erase(Set.begin());
52+
53+
// Traverse all neighbors of the current node
54+
for (auto neighbour : adjacencyList[currNode]) {
55+
int neighborNode = neighbour.first; // Neighbor node
56+
int edgeWeight = neighbour.second; // Weight of the edge to the neighbor
57+
58+
// Check if a shorter path to the neighbor exists
59+
if (nodeDistance + edgeWeight < distance[neighborNode]) {
60+
// If the neighbor is already in the set, remove the old record
61+
auto record = Set.find({distance[neighborNode], neighborNode});
62+
if (record != Set.end())
63+
Set.erase(record);
64+
65+
// Update the distance to the neighbor
66+
distance[neighborNode] = nodeDistance + edgeWeight;
67+
68+
// Insert the updated record into the set
69+
Set.insert({distance[neighborNode], neighborNode});
70+
}
71+
}
72+
}
73+
}
74+
};
75+
76+
int main() {
77+
graph g; // Create a graph object
78+
79+
// Add edges to the graph
80+
g.addEdge(0, 1, 5);
81+
g.addEdge(0, 2, 8);
82+
g.addEdge(1, 2, 9);
83+
g.addEdge(1, 3, 2);
84+
g.addEdge(2, 3, 6);
85+
86+
// Print the adjacency list of the graph
87+
g.printAdjacencyList();
88+
89+
int n = 4; // Number of nodes in the graph
90+
vector<int> distance(n, INT_MAX); // Initialize distances to all nodes as infinity (INT_MAX)
91+
set<pair<int, int>> Set; // Min-heap implemented using a set to store (distance, node) pairs
92+
93+
int src = 0; // Define the source node for Dijkstra's algorithm
94+
g.DijkstraAlgorithm(src, distance, Set); // Call Dijkstra's algorithm
95+
96+
// Print the shortest distances from the source node to all other nodes
97+
cout << "Shortest distances from node " << src << ":" << endl;
98+
for (int i = 0; i < n; i++) {
99+
cout << "Node " << i << ": ";
100+
if (distance[i] == INT_MAX) // If the distance is still infinity, the node is unreachable
101+
cout << "INF" << endl;
102+
else
103+
cout << distance[i] << endl; // Print the distance to the node
104+
}
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)