Skip to content

Commit f182e29

Browse files
committed
Add Dijkstra algorithm
1 parent a7d033f commit f182e29

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

Min_Spanning_Tree/prim_list.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class graph {
1717
adj[v].push_back(make_pair(u, wt));
1818
}
1919
void prims(int s);
20-
void print_mst();
2120
};
2221

2322
void graph::prims(int s) {
@@ -31,12 +30,14 @@ void graph::prims(int s) {
3130
// intialised as INT_MAX.
3231
vector<int> weight(nv, INT_MAX);
3332
// Another vector to store the parent of vertices.
34-
vector<int> parent(nv, -1);
33+
vector<int> parent(nv);
3534
// A vector to keep track of vertices included in MST.
3635
vector<bool> mst(nv, false);
3736

3837
// Set weight of source vertex as zero.
3938
weight[s] = 0;
39+
// Set the parent of source vertex as -1.
40+
parent[s] = -1;
4041

4142
// Insert source vertex in the heap.
4243
heap.push(make_pair(weight[s], s));
@@ -64,6 +65,8 @@ void graph::prims(int s) {
6465
}
6566
}
6667
}
68+
69+
// Print the edges of MST.
6770
for (int i = 1; i < nv; i++)
6871
cout << parent[i] << " -> " << i << endl;
6972
cout << endl;

Shortest_Path/dijkstra_list.cc

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Djikstra's shortest path algorithm for adjacency list representation
3+
* of undirected, connected and weighted graphs.
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
class Graph {
9+
int v;
10+
vector<vector<pair<int, int>>> adj;
11+
public:
12+
Graph(int nv) : v(nv), adj(nv) {}
13+
void addEdge(int u, int v, int wt) {
14+
adj[u].push_back(make_pair(v, wt));
15+
adj[v].push_back(make_pair(u, wt));
16+
}
17+
void djikstra(int s);
18+
};
19+
20+
void Graph::djikstra(int s) {
21+
priority_queue<pair<int, int>, vector<pair<int, int>>,
22+
greater<pair<int, int>>> heap;
23+
24+
vector<int> dist(v, INT_MAX);
25+
vector<int> parent(v);
26+
27+
dist[s] = 0;
28+
parent[s] = -1;
29+
30+
heap.push(make_pair(s, dist[s]));
31+
32+
while (!heap.empty()) {
33+
int u = heap.top().first;
34+
heap.pop();
35+
36+
for (auto it = adj[u].begin(); it != adj[u].end(); ++it) {
37+
int v = (*it).first;
38+
int dist_v = (*it).second;
39+
40+
// Update distance of v from source if there's
41+
// a shorter path to v via u.
42+
if (dist[v] > dist[u] + dist_v) {
43+
dist[v] = dist[u] + dist_v;
44+
// Update parent of v as u.
45+
parent[v] = u;
46+
// Push v and its updated distance in min heap.
47+
heap.push(make_pair(v, dist[v]));
48+
}
49+
}
50+
}
51+
52+
// Print the shortest path edges
53+
for (int i = 1; i < v; i++)
54+
cout << parent[i] << " -- " << i << endl;
55+
}
56+
57+
int main() {
58+
59+
Graph g(9);
60+
61+
g.addEdge(0, 1, 4);
62+
g.addEdge(0, 7, 8);
63+
g.addEdge(1, 2, 8);
64+
g.addEdge(1, 7, 11);
65+
g.addEdge(2, 3, 7);
66+
g.addEdge(2, 8, 2);
67+
g.addEdge(2, 5, 4);
68+
g.addEdge(3, 4, 9);
69+
g.addEdge(3, 5, 14);
70+
g.addEdge(4, 5, 10);
71+
g.addEdge(5, 6, 2);
72+
g.addEdge(6, 7, 1);
73+
g.addEdge(6, 8, 6);
74+
g.addEdge(7, 8, 7);
75+
76+
int source;
77+
cin >> source;
78+
79+
g.djikstra(source);
80+
81+
return 0;
82+
}
83+
// Time complexity : O(E * Log V))

Shortest_Path/dijkstra_matrix.cc

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Djikstra's shortest path algorithm for adjacency matrix representation
3+
* of undirected, connected and weighted graphs.
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
int minDist(vector<bool>& spt, vector<int>& dist, int v) {
9+
int min = INT_MAX, index = 0;
10+
11+
for (int i = 0; i < v; i++) {
12+
if (spt[i] == false && dist[i] < min) {
13+
min = dist[i];
14+
index = i;
15+
}
16+
}
17+
18+
return index;
19+
}
20+
21+
void djikstra(vector<vector<int>>& g, int v, int s) {
22+
23+
vector<bool> spt_set(v, false);
24+
vector<int> dist(v, INT_MAX);
25+
vector<int> parent(v);
26+
27+
dist[s] = 0;
28+
parent[s] = -1;
29+
30+
for (int i = 0; i < v; i++) {
31+
32+
int u = minDist(spt_set, dist, v);
33+
spt_set[u] = true;
34+
35+
for (int j = 0; j < v; j++) {
36+
if (spt_set[j] == false && g[u][j] != 0
37+
&& dist[u] + g[u][j] < dist[j]) {
38+
dist[j] = dist[u] + g[u][j];
39+
parent[j] = u;
40+
}
41+
}
42+
}
43+
44+
// Print the shortest path edges.
45+
for (int i = 1; i < v; i++)
46+
// Starting from i = 1 because parent[0] = -1
47+
cout << parent[i] << " -- " << i << endl;
48+
}
49+
50+
int main() {
51+
int nv;
52+
cin >> nv;
53+
54+
// A square matrix of size nv to represent the graph.
55+
vector<vector<int>> graph(nv, vector<int>(nv));
56+
57+
for (int i = 0; i < nv; i++)
58+
for (int j = 0; j < nv; j++)
59+
cin >> graph[i][j];
60+
61+
int source;
62+
cin >> source;
63+
64+
djikstra(graph, nv, source);
65+
66+
return 0;
67+
}
68+
// Time Complexity: O(V^2). It can be reduced to O(E * logV) by trading
69+
// space for a binary heap.

0 commit comments

Comments
 (0)