Given a graph of **N** nodes, **E** edges, a node **X** and a distance **K**. The task is to print all the nodes within the distance **K** from **X**.

Input:

Output:4 5 2 7 3Neigbour nodes within distance 2 of node 4 are: 4 5 2 7 3

**Approach: **

To print all the nodes that are at distance **K** or less than **K**. We can do it by applying dfs variation, that takes K node from where we have to print the distance until distance K.

dfs(K, node, -1, tree)

Here -1 indicates node parent.

This recursive function basically prints the node and then calls the **dfs(K-1, neighbour of node, node, tree)**.

Base condition is K>0.

Below is the implementation of the above approach:

## C++

`// C++ program to print ` `// the nearest K neighbour ` `// nodes (including itself) ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure of an edge ` `struct` `arr { ` ` ` `int` `from, to; ` `}; ` ` ` `// Recursive function to print ` `// the neighbor nodes of a node ` `// until K distance ` `void` `dfs(` `int` `k, ` `int` `node, ` ` ` `int` `parent, ` ` ` `const` `vector<vector<` `int` `> >& tree) ` `{ ` ` ` ` ` `// Base condition ` ` ` `if` `(k < 0) ` ` ` `return` `; ` ` ` ` ` `// Print the node ` ` ` `cout << node << ` `' '` `; ` ` ` ` ` `// Traverse the connected ` ` ` `// nodes/adjacency list ` ` ` `for` `(` `int` `i : tree[node]) { ` ` ` ` ` `if` `(i != parent) { ` ` ` ` ` `// node i becomes the parent ` ` ` `// of its child node ` ` ` `dfs(k - 1, i, node, tree); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to print nodes under ` `// distance k ` `void` `print_under_dis_K(` `struct` `arr graph[], ` ` ` `int` `node, ` `int` `k, ` ` ` `int` `v, ` `int` `e) ` `{ ` ` ` ` ` `// To make graph with ` ` ` `// the given edges ` ` ` `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); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Number of vertex and edges ` ` ` `int` `v = 7, e = 6; ` ` ` ` ` `// Given edges ` ` ` `struct` `arr graph[v + 1] = { ` ` ` `{ 2, 1 }, ` ` ` `{ 2, 5 }, ` ` ` `{ 5, 4 }, ` ` ` `{ 5, 7 }, ` ` ` `{ 4, 3 }, ` ` ` `{ 7, 6 } ` ` ` `}; ` ` ` ` ` `// k is the required distance ` ` ` `// upto which are neighbor ` ` ` `// nodes should get printed ` ` ` `int` `node = 4, k = 2; ` ` ` ` ` `// function calling ` ` ` `print_under_dis_K(graph, node, k, v, e); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print ` `# the nearest K neighbour ` `# nodes (including itself) ` ` ` `tree ` `=` `[[] ` `for` `i ` `in` `range` `(` `100` `)] ` ` ` `# Recursive function to print ` `# the neighbor nodes of a node ` `# until K distance ` `def` `dfs(k, node, parent): ` ` ` ` ` `# Base condition ` ` ` `if` `(k < ` `0` `): ` ` ` `return` ` ` ` ` `# Print the node ` ` ` `print` `(node, end ` `=` `" "` `) ` ` ` ` ` `# Traverse the connected ` ` ` `# nodes/adjacency list ` ` ` `for` `i ` `in` `tree[node]: ` ` ` ` ` `if` `(i !` `=` `parent): ` ` ` ` ` `# node i becomes the parent ` ` ` `# of its child node ` ` ` `dfs(k ` `-` `1` `, i, node) ` ` ` `# Function to print nodes under ` `# distance k ` `def` `print_under_dis_K(graph, node, k, v, e): ` ` ` ` ` `for` `i ` `in` `range` `(e): ` ` ` ` ` `fro ` `=` `graph[i][` `0` `] ` ` ` `to ` `=` `graph[i][` `1` `] ` ` ` ` ` `tree[fro].append(to) ` ` ` `tree[to].append(fro) ` ` ` ` ` `dfs(k, node, ` `-` `1` `) ` ` ` `# Driver Code ` ` ` `# Number of vertex and edges ` `v ` `=` `7` `e ` `=` `6` ` ` `# Given edges ` `graph ` `=` `[[ ` `2` `, ` `1` `], ` ` ` `[ ` `2` `, ` `5` `], ` ` ` `[ ` `5` `, ` `4` `], ` ` ` `[ ` `5` `, ` `7` `], ` ` ` `[ ` `4` `, ` `3` `], ` ` ` `[ ` `7` `, ` `6` `]] ` ` ` `# k is the required distance ` `# upto which are neighbor ` `# nodes should get pred ` `node ` `=` `4` `k ` `=` `2` ` ` `# function calling ` `print_under_dis_K(graph, node, k, v, e) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

4 5 2 7 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count nodes within K-distance from all nodes in a set
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Print all nodes at distance k from a given node
- Print all nodes at distance K from given node: Iterative Approach
- Check if a given array contains duplicate elements within k distance from each other
- Print nodes at k distance from root
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Print levels with odd number of nodes and even number of nodes
- Sum of all odd nodes in the path connecting two given nodes
- Maximize count of nodes disconnected from all other nodes in a Graph
- Find distance between two nodes of a Binary Tree
- Shortest distance between two nodes in BST
- Find the shortest distance between any pair of two different good nodes
- Find count of pair of nodes at even distance
- Print all nodes that don't have sibling
- Print all nodes in a binary tree having K leaves
- Print all leaf nodes of an n-ary tree using DFS
- Program to print all the non-reachable nodes | Using BFS
- Print node whose each neighboring Tree has all nodes of same color
- Delete N nodes after M nodes of a linked list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.