Skip to content

Commit f2dce09

Browse files
committed
Day 49 Dijkstra shortest path algorithm
1 parent 917bc54 commit f2dce09

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 67 |
8-
| Current Streak | 48 |
9-
| Longest Streak | 48 ( August 17, 2015 - October 3, 2015 ) |
7+
| Total Problems | 68 |
8+
| Current Streak | 49 |
9+
| Longest Streak | 49 ( August 17, 2015 - October 4, 2015 ) |
1010

1111
</center>
1212

@@ -96,6 +96,8 @@ Include contains single header implementation of data structures and some algori
9696
| Find next permutation of a given string, ie. rearrange the given string sucht a way that is next lexicographically greater string than given string | [next_permutation.cpp](string_problems/next_permutation.cpp)|
9797

9898
### Common Data Structure and logic problems
99+
| Problem | Solution |
100+
| :------------ | :----------: |
99101
| Print the contents of matrix in a spiral order | [matrix_spiral_print.cpp](common_ds_problems/matrix_spiral_print.cpp)
100102
| Given a M x N matrix, rotate it by R rotations anticlockwise, and show the resulting matrix. | [rotate_matrix.cpp](common_ds_problems/rotate_matrix.cpp)|
101103
| Rotate an array by r elements ( left or right ) | [array_rotation.cpp](common_ds_problems/array_rotation.cpp)
@@ -125,3 +127,4 @@ Include contains single header implementation of data structures and some algori
125127
| :------------ | :----------: |
126128
| Depth First Traversal of a Graph | [dfsDemo.cpp](graph_problems/dfsDemo.cpp) |
127129
| Breadth First Traversal of a Graph | [bfsDemo.cpp](graph_problems/bfsDemo.cpp) |
130+
| calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph using Dijkstra algorithm. | [dijkstra-shortest-reach.cpp](graph_problems/dijkstra-shortest-reach.cpp)|
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Given a graph consisting N nodes (labelled 1 to N) where a specific given node S represents the starting position S
3+
* and an edge between two nodes is of a given length, which may or may not be equal to other lengths in the graph.
4+
* It is required to calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph.
5+
* Note 1: If a node is unreachable , the distance is assumed as −1.
6+
*
7+
* Input Format
8+
* The first line contains T, denoting the number of test cases.
9+
* First line of each test case has two integers N, denoting the number of nodes in the graph and M,
10+
* denoting the number of edges in the graph.
11+
* The next M lines each consist of three space separated integers x y w, where x and y
12+
* denote the two nodes between which the undirected edge exists, w denotes the length of edge between these corrresponding nodes.
13+
* The last line has an integer S, denoting the starting position.
14+
*
15+
* Output Format
16+
* For each of the T test cases, Print a single line consisting N−1 space separated integers denoting
17+
* the shortest distance of N−1 nodes from starting position S. For unreachable nodes, print −1.
18+
*
19+
* If there are edges between the same pair of nodes with different weights,
20+
* they are to be considered as is, like multiple edges.
21+
*/
22+
23+
#include <cmath>
24+
#include <cstdio>
25+
#include <vector>
26+
#include <iostream>
27+
#include <algorithm>
28+
#include <list>
29+
#include <climits>
30+
#include <set>
31+
using namespace std;
32+
33+
vector<int> dist;
34+
struct lstDist {
35+
bool operator()(int u, int v) const {
36+
return make_pair(dist[u], u) < make_pair(dist[v], v);
37+
}
38+
};
39+
40+
class Graph {
41+
int N;
42+
vector<vector<int>> weights;
43+
int S;
44+
45+
public:
46+
Graph( int n )
47+
: N{n}, weights(N, vector<int>(N, -1)){}
48+
49+
void addEdge( int x, int y, int w) {
50+
if (weights[x-1][y-1] == -1 || (weights[x-1][y-1] != -1 && weights[x-1][y-1] > w )) {
51+
weights[x-1][y-1] = w;
52+
weights[y-1][x-1] = w;
53+
}
54+
}
55+
void setStart(int s) {
56+
S = s-1;
57+
dist[S] = 0;
58+
}
59+
60+
void distance() {
61+
set<int, lstDist> q;
62+
q.insert(S);
63+
while(!q.empty()) {
64+
int u = *q.begin();
65+
q.erase(q.begin());
66+
for( int v = 0; v < N ; ++v ) {
67+
if (weights[u][v] != -1) {
68+
int newDist = dist[u] + weights[u][v];
69+
if (newDist < dist[v]) {
70+
dist[v] = newDist;
71+
if (q.count(v)) {
72+
q.erase(v);
73+
}
74+
q.insert(v);
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
void printDistance() {
82+
for ( unsigned int i = 0; i < dist.size(); ++i ) {
83+
if ( int(i) != S) {
84+
if (dist[i] == INT_MAX) dist[i] = -1;
85+
std::cout << dist[i] << " ";
86+
}
87+
}
88+
std::cout << std::endl;
89+
}
90+
};
91+
92+
int main() {
93+
int T, N, M, x, y, w, S;
94+
cin >> T;
95+
while( T ) {
96+
cin >> N >> M;
97+
dist.resize(N);
98+
for( int i = 0; i < N; ++i) {
99+
dist[i] = INT_MAX;
100+
}
101+
Graph G(N);
102+
for ( int i = 0; i < M; ++i) {
103+
cin >> x >> y >> w;
104+
G.addEdge(x, y, w);
105+
}
106+
cin >> S;
107+
G.setStart(S);
108+
G.distance();
109+
G.printDistance();
110+
--T;
111+
}
112+
return 0;
113+
}
114+

0 commit comments

Comments
 (0)