forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code6.cpp
67 lines (60 loc) · 1.48 KB
/
code6.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
// Shortest Path in Unweighted Graph
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1e9 + 7;
void add_edge(vector<int> vec[], int u, int v)
{
vec[u].push_back(v);
vec[v].push_back(u);
}
void find_sd(vector<int> vec[], int v, int source)
{
vector<bool> visited(v, false);
vector<int> dist(v, INT_MAX);
queue<int> q;
q.push(source);
dist[source] = 0;
visited[source] = true;
while (q.empty() == false)
{
int curr = q.front();
q.pop();
// cout<<"curr: "<<curr<<endl;
for (int i = 0; i < vec[curr].size(); i++)
{
int adjacent = vec[curr][i];
// cout<<"curr: "<<curr<<" adjacent: "<<adjacent<<endl;
if (visited[adjacent] == false)
{
dist[adjacent] = dist[curr] + 1;
visited[adjacent] = true;
q.push(adjacent);
}
}
}
cout << "CHECK DISTANCE: " << endl;
for (int i = 0; i < dist.size(); i++)
{
cout << dist[i] << " ";
}
cout << endl;
return;
}
int main()
{
int v;
v = 6;
vector<int> adj[v];
// adding edge to graph
add_edge(adj, 0, 1);
add_edge(adj, 0, 2);
add_edge(adj, 0, 3);
add_edge(adj, 1, 4);
add_edge(adj, 1, 5);
add_edge(adj, 2, 5);
// finding shortes distance
int source = 0;
find_sd(adj, v, source);
return 0;
}