# Maximum number of nodes which can be reached from each node in a graph.

Given a graph with N nodes and K bidirectional edges between them find the number of nodes which can be reachable from a particular. Two nodes X and Y are said to be reachable if we can start at X and end at Y using any number of edges.
Note : A Node is reachable to itself.

```Input : N = 7
1            5
\          / \
2        6 __ 7
\
3
\
4

Output :4 4 4 4 3 3 3
From node 1 ,nodes {1,2,3,4} can be visited
hence the answer is equal to 4
From node 5,nodes {5,6,7} can be visited.
hence the answer is equal to 3.
Similarly, print the count for every node.

Input : N = 8
1              7
/   \             \
2     3              8
\   / \
5    6
Output :6 6 6 6 6 6 2 2

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To find the number of nodes reachable from a particular node, one thing to observe is that we can reach from a node X to a node Y only when they are in the same connected component.
Since the graph is bidirectional, any node in a connected component to any other node in the same connected components. Hence for a particular node X number of nodes that will be reachable will be the number of nodes in that particular component.Use depth-first search to find the answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` `const` `int` `maxx = 100005; ` `vector<``int``> graph[maxx]; ` ` `  `// Function to perform the DFS calculating the  ` `// count of the elements in a connected component ` `void` `dfs(``int` `curr, ``int``& cnt, ``int``*  ` `             ``visited, vector<``int``>& duringdfs) ` `{ ` `    ``visited[curr] = 1; ` ` `  `    ``// Number of nodes in this component ` `    ``++cnt; ` ` `  `    ``// Stores the nodes which belong  ` `    ``// to current component ` `    ``duringdfs.push_back(curr); ` `    ``for` `(``auto``& child : graph[curr]) { ` ` `  `        ``// If the child is not visited ` `        ``if` `(visited[child] == 0) { ` `            ``dfs(child, cnt, visited, duringdfs); ` `        ``} ` `    ``} ` `} ` ` `  `// Function to return the desired  ` `// count for every node in the graph ` `void` `MaximumVisit(``int` `n, ``int` `k) ` `{ ` `    ``// To keep track of nodes we visit ` `    ``int` `visited[maxx]; ` ` `  `    ``// Mark every node unvisited ` `    ``memset``(visited, 0, ``sizeof` `visited); ` `    ``int` `ans[maxx]; ` ` `  `    ``// No of connected elements for each node ` `    ``memset``(ans, 0, ``sizeof` `ans); ` `    ``vector<``int``> duringdfs;  ` `    ``for` `(``int` `i = 1; i <= n; ++i) { ` `        ``duringdfs.clear(); ` `        ``int` `cnt = 0; ` ` `  `        ``// If a node is not visited, perform a DFS as  ` `        ``// this node belongs to a different component ` `        ``// which is not yet visited ` `        ``if` `(visited[i] == 0) { ` `            ``cnt = 0; ` `            ``dfs(i, cnt, visited, duringdfs); ` `        ``} ` ` `  `        ``// Now store the count of all the visited ` `        ``// nodes for any particular component. ` `        ``for` `(``auto``& x : duringdfs) { ` `            ``ans[x] = cnt; ` `        ``} ` `    ``} ` `    `  `    ``// Print the result ` `    ``for` `(``int` `i = 1; i <= n; ++i) { ` `        ``cout << ans[i] << ``" "``; ` `    ``} ` `    ``cout << endl; ` `    ``return``; ` `} ` ` `  `// Function to build the graph ` `void` `MakeGraph(){ ` `    ``graph.push_back(2); ` `    ``graph.push_back(1); ` `    ``graph.push_back(3); ` `    ``graph.push_back(2); ` `    ``graph.push_back(4); ` `    ``graph.push_back(3); ` `    ``graph.push_back(6); ` `    ``graph.push_back(5); ` `    ``graph.push_back(7); ` `    ``graph.push_back(6); ` `    ``graph.push_back(7); ` `    ``graph.push_back(5); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 7, K = 6; ` `     `  `    ``// Build the graph ` `    ``MakeGraph(); ` `    `  `    ``MaximumVisit(N, K); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `    ``static` `final` `int` `maxx = ``100005``; ` `    ``@SuppressWarnings``(``"unchecked"``) ` `    ``static` `Vector[] graph = ``new` `Vector[maxx]; ` `    ``static` `Vector duringdfs = ``new` `Vector<>(); ` `    ``static` `int` `cnt = ``0``; ` ` `  `    ``// Function to perform the DFS calculating the ` `    ``// count of the elements in a connected component ` `    ``static` `void` `dfs(``int` `curr, ``int``[] visited)  ` `    ``{ ` `        ``visited[curr] = ``1``; ` ` `  `        ``// Number of nodes in this component ` `        ``++cnt; ` ` `  `        ``// Stores the nodes which belong ` `        ``// to current component ` `        ``duringdfs.add(curr); ` `        ``for` `(``int` `child : graph[curr]) ` `        ``{ ` ` `  `            ``// If the child is not visited ` `            ``if` `(visited[child] == ``0``) ` `                ``dfs(child, visited); ` `        ``} ` `    ``} ` ` `  `    ``// Function to return the desired ` `    ``// count for every node in the graph ` `    ``static` `void` `maximumVisit(``int` `n, ``int` `k) ` `    ``{ ` ` `  `        ``// To keep track of nodes we visit ` `        ``int``[] visited = ``new` `int``[maxx]; ` ` `  `        ``// Mark every node unvisited ` `        ``Arrays.fill(visited, ``0``); ` ` `  `        ``int``[] ans = ``new` `int``[maxx]; ` ` `  `        ``// No of connected elements for each node ` `        ``Arrays.fill(ans, ``0``); ` ` `  `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `        ``{ ` `            ``duringdfs.clear(); ` ` `  `            ``// If a node is not visited, perform a DFS as ` `            ``// this node belongs to a different component ` `            ``// which is not yet visited ` `            ``if` `(visited[i] == ``0``)  ` `            ``{ ` `                ``cnt = ``0``; ` `                ``dfs(i, visited); ` `            ``} ` ` `  `            ``// Now store the count of all the visited ` `            ``// nodes for any particular component. ` `            ``for` `(``int` `x : duringdfs) ` `                ``ans[x] = cnt; ` `        ``} ` ` `  `        ``// Print the result ` `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `            ``System.out.print(ans[i] + ``" "``); ` `        ``System.out.println(); ` `        ``return``; ` `    ``} ` ` `  `    ``// Function to build the graph ` `    ``static` `void` `makeGraph() ` `    ``{ ` `        ``// Initializing graph ` `        ``for` `(``int` `i = ``0``; i < maxx; i++) ` `            ``graph[i] = ``new` `Vector<>(); ` ` `  `        ``graph[``1``].add(``2``); ` `        ``graph[``2``].add(``1``); ` `        ``graph[``2``].add(``3``); ` `        ``graph[``3``].add(``2``); ` `        ``graph[``3``].add(``4``); ` `        ``graph[``4``].add(``3``); ` `        ``graph[``5``].add(``6``); ` `        ``graph[``6``].add(``5``); ` `        ``graph[``6``].add(``7``); ` `        ``graph[``7``].add(``6``); ` `        ``graph[``5``].add(``7``); ` `        ``graph[``7``].add(``5``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `N = ``7``, K = ``6``; ` ` `  `        ``// Build the graph ` `        ``makeGraph(); ` ` `  `        ``maximumVisit(N, K); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

Output:

```4 4 4 4 3 3 3
```

Time Complexity : O(N)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : sanjeev2552

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.