-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathdijkstra_algorithm.cpp
More file actions
154 lines (132 loc) · 2.98 KB
/
Copy pathdijkstra_algorithm.cpp
File metadata and controls
154 lines (132 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Dijkstra’s Algorithm — Single Source Shortest Path
// Problem
// Given a weighted graph with non-negative edge weights,
// find the shortest path from a source vertex to all other vertices.
//
// Approach
// 1. Use a priority queue (min-heap) to always expand the next closest vertex.
// 2. Initialize distances[] with infinity, except source = 0.
// 3. While queue not empty:
// - Extract vertex u with smallest dist[u].
// - For each neighbor v of u, relax edge (u,v):
// if dist[u] + weight(u,v) < dist[v], update dist[v].
//
// Complexity
// Time : O((V + E) log V) — using priority queue
// Space : O(V + E) — adjacency list + distance array + heap
//
// Input
// - Number of vertices (V)
// - Number of edges (E)
// - Edge list {u, v, w} (u → v with weight w)
// - Source vertex (src)
//
// Output
// - Shortest distance from src to all vertices
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int V, E;
cin >> V >> E;
vector<vector<pair<int, int>>> adj(V);
for (int i = 0; i < E; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w}); // undirected; remove if directed
}
int src;
cin >> src;
const int INF = numeric_limits<int>::max();
vector<int> dist(V, INF);
dist[src] = 0;
// min-heap {distance, vertex}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0, src});
while (!pq.empty())
{
int d = pq.top().first;
int u = pq.top().second;
pq.pop();
if (d > dist[u])
continue; // outdated entry
for (auto &edge : adj[u])
{
int v = edge.first, w = edge.second;
if (dist[u] + w < dist[v])
{
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
cout << "Shortest distances from source " << src << ":\n";
for (int i = 0; i < V; i++)
{
if (dist[i] == INF)
cout << i << " : INF\n";
else
cout << i << " : " << dist[i] << "\n";
}
return 0;
}
/*
Example Input:
5 6
0 1 2
0 2 4
1 2 1
1 3 7
2 4 3
3 4 1
0
Visualization:
Graph:
(2)
0 ------- 1
\ / \
(4)\ (1) (7)
\ / \
2 ------- 3
\ /
(3) (1)
\ /
4
Execution:
dist = [0, INF, INF, INF, INF]
Start from src=0
Step 1: Pick 0 → dist[0]=0
Relax edges:
0→1 (2) → dist[1]=2
0→2 (4) → dist[2]=4
dist = [0,2,4,INF,INF]
Step 2: Pick 1 (dist=2)
Relax edges:
1→2 (1) → dist[2]=min(4,2+1)=3
1→3 (7) → dist[3]=2+7=9
dist = [0,2,3,9,INF]
Step 3: Pick 2 (dist=3)
Relax edges:
2→4 (3) → dist[4]=3+3=6
dist = [0,2,3,9,6]
Step 4: Pick 4 (dist=6) → relax 4→3 (1):
dist[3]=min(9,6+1)=7
dist = [0,2,3,7,6]
Step 5: Pick 3 (dist=7) → no better updates
Final distances from 0:
0 : 0
1 : 2
2 : 3
3 : 7
4 : 6
Time : O((V+E) log V)
Space : O(V+E)
*/
// add: Dijkstra PR