- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to Print all the Non-Reachable Nodes | Using BFS

Non−reachable nodes are nodes in a chart that cannot be reached from a particular source hub. They are hubs that don't have any way of interfacing with the source hub inside the given chart. The recognisable proof of non−reachable hubs makes a difference in determining the detached or disconnected substances inside the chart. Calculations like breadth−first look (BFS) or depth−first look (DFS) can be utilised to navigate the chart and effectively stamp the gone−by hubs, subsequently encouraging the distinguishing proof of non−reachable hubs. Analysing and understanding non−reachable hubs is important in evaluating organised networks, recognising crevices in data or networks, and making educated choices for organised optimisation and advancement.

The programme utilises (BFS) calculation to distinguish and print all non−reachable hubs in a given chart. It makes a difference in assessing organisational structures, identifying data crevices, and making educated choices. The BFS calculation productively navigates the chart to discover detached or inaccessible hubs.

## Methods Used

BSF

## BSF

BFS stands for Breadth−First Look. It could be a chart traversal calculation that investigates all the vertices of a chart in breadth−first order, i.e., it visits all the vertices at the same level some time recently before moving to another level. The BFS calculation begins at a given source vertex and investigates its adjoining vertices. At that point, it moves to another level of vertices, going to their adjoining vertices, and so on. It employs a line information structure to keep track of the vertices to be passed by.

### Algorithm

Initialise a line and a gone−by array:

A line could be an information structure that follows the First−In−First−Out (FIFO) guideline. It'll be used to store the vertices during BFS traversal.

The gone−to cluster is utilised to keep track of the vertices that have been gone by or explored.

Enqueue the source vertex and check it as visited:

The source vertex is the beginning point of the BFS traversal.

Enqueueing implies including the source vertex in the queue.

Marking the source vertex as gone guarantees that it won't be prepared again.

Perform BFS traversal:

Repeat the following steps until the line gets empty:

Dequeue a vertex from the queue:

Dequeuing implies evacuating a vertex from the front of the queue.

The dequeued vertex will be handled, and its adjoining vertices will be enqueued.

Enqueue all unvisited neighbouring vertices:

For each adjoining vertex of the dequeued vertex, on the off chance that it hasn't been gone by, however, enqueue it.

This step guarantees that all reachable vertices are investigated in a breadth−first manner.

Mark each queued vertex as visited:

After enqueuing each vertex, stamp it as gone to avoid returning to it.

Print the non−visited vertices.

After completing the BFS traversal, the target cluster stamps all vertices reachable from the source vertex.

Non−visited vertices speak to hubs that are not reachable from the source vertex.

Print these non−visited vertices to recognise the disconnected or non−reachable hubs within the graph.

The programme execution is total, and the yield has been generated. Exit the programme smoothly.

### Example 1

#include <iostream> #include <vector> #include <queue> using namespace std; // Function to add an edge to graph void addEdge(vector<int> adj[], int v, int w) { // Add w to v's list. adj[v].push_back(w); // Add v to w's list. adj[w].push_back(v); } // BFS traversal of the vertices // reachable from starting node void findNonReachableNodes(vector<int> adj[], int s, int v) { // Mark all the vertices // as not visited bool visited[v] = {false}; // Create a queue for BFS queue<int> q; // Mark the current node as // visited and enqueue it q.push(s); visited[s] = true; while (!q.empty()) { // Dequeue a vertex from // queue int p = q.front(); q.pop(); // Get all adjacent vertices // of the dequeued vertex p. // If an adjacent has not been // visited, then mark it // visited and enqueue it for (auto it = adj[p].begin(); it != adj[p].end(); it++) { if (!visited[*it]) { visited[*it] = true; q.push(*it); } } } for (int i = 0; i < v; i++) { if (!visited[i]) { cout << i << " "; } } cout << "\n"; } // Driver code int main() { // Create a graph given in // the above diagram const int numVertices = 8; vector<int> adj[numVertices]; addEdge(adj, 7, 1); addEdge(adj, 6, 2); addEdge(adj, 5, 7); addEdge(adj, 4, 3); addEdge(adj, 3, 4); addEdge(adj, 1, 6); findNonReachableNodes(adj, 0, numVertices); return 0; }

### Output

1 2 3 4 5 6 7

### Example 2

#include <iostream> #include <vector> #include <queue> using namespace std; void addEdge(vector<vector<int>>& adjList, int v, int w) { adjList[v].push_back(w); adjList[w].push_back(v); } void findNonReachableNodes(vector<vector<int>>& adjList, int source, int numVertices) { vector<bool> visited(numVertices, false); queue<int> q; q.push(source); visited[source] = true; while (!q.empty()) { int vertex = q.front(); q.pop(); for (auto neighbor : adjList[vertex]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } for (int i = 0; i < numVertices; i++) { if (!visited[i]) { cout << i << " "; } } cout << endl; } int main() { const int totalVertices = 8; vector<vector<int>> adjList(totalVertices); addEdge(adjList, 0, 1); addEdge(adjList, 0, 2); addEdge(adjList, 1, 2); addEdge(adjList, 3, 4); addEdge(adjList, 4, 5); addEdge(adjList, 6, 7); int sourceVertex = 0; findNonReachableNodes(adjList, sourceVertex, totalVertices); return 0; }

### Output

3 4 5 6 7

## Conclusion

This article clarifies the concept of finding non−reachable hubs in a chart using the Breadth−first−look (BFS) calculation. It talks about the importance of recognising non−reachable hubs in analysing organised systems, recognising holes in information or systems, and making educated choices for arrangement optimisation and advancement. The BFS calculation is portrayed step−by−step, highlighting the initialization, traversal preparation, and printing of non−reachable hubs. A programme is given as an example to illustrate the execution of BFS for finding non−reachable hubs in a chart. The article points to a clear understanding of BFS and its application in distinguishing non−reachable hubs.