-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijksta.cpp
More file actions
78 lines (66 loc) · 2.06 KB
/
dijksta.cpp
File metadata and controls
78 lines (66 loc) · 2.06 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
#include <iostream>
using namespace std;
// Maximum number of vertices
#define MAX_V 100
// Infinity for unreachable vertices
#define INF 99999
// Dijkstra's Algorithm
void dijkstra(int graph[MAX_V][MAX_V], int V, int src) {
int dist[MAX_V]; // Shortest distances from source
bool visited[MAX_V]; // Track visited vertices
// Initialize
for (int i = 0; i < V; i++) {
dist[i] = INF; // All distances unknown
visited[i] = false; // All unvisited
}
dist[src] = 0; // Source distance is 0
// Process all vertices
for (int count = 0; count < V; count++) {
// Find unvisited vertex with minimum distance
int minDist = INF, u = -1;
for (int i = 0; i < V; i++) {
if (!visited[i] && dist[i] < minDist) {
minDist = dist[i];
u = i;
}
}
// If no unvisited vertex, break
if (u == -1) break;
// Mark vertex u as visited
visited[u] = true;
// Update distances to neighbors
for (int v = 0; v < V; v++) {
if (!visited[v] && graph[u][v] != 0 && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
// Print results
cout << "Shortest distances from vertex " << src << ":\n";
for (int i = 0; i < V; i++) {
cout << "To vertex " << i << ": ";
if (dist[i] == INF) cout << "Unreachable\n";
else cout << dist[i] << "\n";
}
}
int main() {
int V, E, src;
int graph[MAX_V][MAX_V] = {0}; // Adjacency matrix
// Input
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
cout << "Enter edges (u v weight, 0-based vertices):\n";
for (int i = 0; i < E; i++) {
int u, v, w;
cin >> u >> v >> w;
graph[u][v] = w; // Directed edge
graph[v][u] = w; // For undirected graph
}
cout << "Enter source vertex (0 to " << V-1 << "): ";
cin >> src;
// Run Dijkstra's
dijkstra(graph, V, src);
return 0;
}