-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortest_path_in_undirected_graph.cpp
72 lines (54 loc) · 1.72 KB
/
sortest_path_in_undirected_graph.cpp
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
#include<iostream>
#include<vector>
#include<queue>
#include<limits.h>
using namespace std;
// problem link -> https://www.geeksforgeeks.org/problems/shortest-path-in-undirected-graph-having-unit-distance/1
// simply needs the BFS traversal technique method.
vector<int> shortestPath(vector<vector<int>>& edges, int N,int M, int src){
vector<int> adj[N];
for(auto it:edges){
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
vector<int> dist(N , INT_MAX);
dist[src] = 0;
// simply push the src node in the queue with dist 0, and normally do the BFS traversal.
/* ********pair of queue is not mandatory *****
queue<pair<int,int>> q;
q.push({src,0});
while(!q.empty())
{
int node = q.front().first;
int d = q.front().second;
q.pop();
for(auto it:adj[node]){
int v = it;
if(d + 1 < dist[v]){
dist[v] = d + 1;
q.push({v, dist[v]});
}
}
}
*/
queue<int> q;
q.push(src);
while(!q.empty()){
int node = q.front();
q.pop();
for(auto it:adj[node]){
if(dist[node] + 1 < dist[it]){
dist[it] = dist[node] + 1;
q.push(it);
}
}
}
for(int i = 0; i<N; i++){
if(dist[i] == INT_MAX) dist[i] = -1;
}
return dist;
}
int main()
{
return 0;
}