# Find all reachable nodes from every node present in a given set

• Difficulty Level : Medium
• Last Updated : 22 Jul, 2022

Given an undirected graph and a set of vertices, find all reachable nodes from every vertex present in the given set.

Consider below undirected graph with 2 disconnected components.

arr[] = {1 , 2 , 5}
Reachable nodes from 1 are  1, 2, 3, 4
Reachable nodes from 2 are 1, 2, 3, 4
Reachable nodes from 5 are 5, 6, 7

Method 1 (Simple)

One straight forward solution is to do a BFS traversal for every node present in the set and then find all the reachable nodes.

Assume that we need to find reachable nodes for n nodes, the time complexity for this solution would be O(n*(V+E)) where V is number of nodes in the graph and E is number of edges in the graph.

Please note that we need to call BFS as a separate call for every node without using the visited array of previous traversals because a same vertex may need to be printed multiple times. This seems to be an effective solution but consider the case when E = Î˜(V2) and n = V, time complexity becomes O(V3).

Method 2 (Efficient)

Since the given graph is undirected, all vertices that belong to same component have same set of reachable nodes. So we keep track of vertex and component mappings. Every component in the graph is assigned a number and every vertex in this component is assigned this number. We use the visit array for this purpose, the array which is used to keep track of visited vertices in BFS.

For a node u,
if visit[u] is 0 then
u has not been visited before
else // if not zero then
visit[u] represents the component number.

For any two nodes u and v belonging to same
component, visit[u] is equal to visit[v]

To store the reachable nodes, use a map m with key as component number and value as a vector which stores all the reachable nodes.

To find reachable nodes for a node u return m[visit[u]]
Look at the pseudo code below in order to understand how to assign component numbers.

componentNum = 0
for i=1 to n
If visit[i] is NOT 0 then
componentNum++

// bfs() returns a list (or vector)
// for given vertex 'i'
list = bfs(i, componentNum)
m[visit[i]]] = list

For the graph shown in the example the visit array would be.

For the nodes 1, 2, 3 and 4 the component number is 1. For nodes 5, 6 and 7 the component number is 2.

Implementation of above idea

## Python3

Output

Reachable Nodes from 2 are
2 1 3 4
Reachable Nodes from 4 are
2 1 3 4
Reachable Nodes from 5 are
5 6 7

Time Complexity Analysis:

n = Size of the given set
E = Number of Edges
V = Number of Nodes
O(V+E) for BFS
In worst case all the V nodes are displayed for each node present in the given, i.e only one component in the graph so it takes O(n*V) time.

Worst Case Time Complexity : O(V+E) + O(n*V)

This article is contributed by Chirag Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up