forked from rajnishmaurya73/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneighourhood node.cpp
More file actions
66 lines (48 loc) · 1.12 KB
/
neighourhood node.cpp
File metadata and controls
66 lines (48 loc) · 1.12 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
//Print all neighbour nodes within distance K
#include <bits/stdc++.h>
using namespace std;
struct arr {
int from, to;
};
void dfs(int k, int node,
int parent,
const vector<vector<int> >& tree)
{
if (k < 0)
return;
cout << node << ' ';
for (int i : tree[node]) {
if (i != parent) {
dfs(k - 1, i, node, tree);
}
}
}
void print_under_dis_K(struct arr graph[],
int node, int k,
int v, int e)
{
vector<vector<int> > tree(v + 1,
vector<int>());
for (int i = 0; i < e; i++) {
int from = graph[i].from;
int to = graph[i].to;
tree[from].push_back(to);
tree[to].push_back(from);
}
dfs(k, node, -1, tree);
}
int main()
{
int v = 7, e = 6;
struct arr graph[v + 1] = {
{ 2, 1 },
{ 2, 5 },
{ 5, 4 },
{ 5, 7 },
{ 4, 3 },
{ 7, 6 }
};
int node = 4, k = 2;
print_under_dis_K(graph, node, k, v, e);
return 0;
}