Given an undirected graph and a set of vertices, we have to print all the non-reachable nodes from the given head node using a breadth-first search.
For example:
Consider below undirected graph with two disconnected components:

In this graph, if we consider 0 as a head node, then the node 0, 1 and 2 are reachable. We mark all the reachable nodes as visited. All those nodes which are not marked as visited i.e, node 3 and 4 are non-reachable nodes.
Examples:
Input: 5
0 1
0 2
1 2
3 4
Output: 3 4
Input: 8
0 1
0 2
1 2
3 4
4 5
6 7
Output: 3 4 5 6 7Approach:
- We can either use BFS or DFS for this purpose. Set 1 of this article implements the DFS approach. In this article, BFS approach is used.
- We do BFS from a given source. Since the given graph is undirected, all the vertices that belong to the disconnected component are non-reachable nodes. We use the visited array for this purpose, the array which is used to keep track of non-visited vertices in BFS.
- BFS is a traversing algorithm which starts traversing from a selected node (source or starting node) and traverses the graph layer-wise thus exploring the neighbour nodes (nodes which are directly connected to source node). Then, move towards the next-level neighbour nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void add_edge(vector<int> adj[],
int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void BFS(vector<int> adj[], int s,
int v)
{
bool visited[v] = { false };
queue<int> q;
q.push(s);
visited[s] = true;
while (!q.empty()) {
int p = q.front();
q.pop();
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";
}
int main()
{
vector<int> adj[8];
add_edge(adj, 0, 1);
add_edge(adj, 0, 2);
add_edge(adj, 1, 2);
add_edge(adj, 3, 4);
add_edge(adj, 4, 5);
add_edge(adj, 6, 7);
BFS(adj, 0, 8);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void add_edge(Vector<Integer> adj[],
int v, int w)
{
adj[v].add(w);
adj[w].add(v);
}
static void BFS(Vector<Integer> adj[], int s,
int v)
{
boolean []visited = new boolean[v];
Queue<Integer> q = new LinkedList<>();
q.add(s);
visited[s] = true;
while (!q.isEmpty()) {
int p = q.peek();
q.remove();
for (int it : adj[p]) {
if (!visited[it]) {
visited[it] = true;
q.add(it);
}
}
}
for (int i = 0; i < v; i++) {
if (!visited[i]) {
System.out.print(i+ " ");
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
Vector<Integer> []adj = new Vector[8];
for (int i = 0; i < 8; i++)
adj[i] = new Vector<Integer>();
add_edge(adj, 0, 1);
add_edge(adj, 0, 2);
add_edge(adj, 1, 2);
add_edge(adj, 3, 4);
add_edge(adj, 4, 5);
add_edge(adj, 6, 7);
BFS(adj, 0, 8);
}
}
|
Python3
def add_edge(adj, v, w):
adj[v].append(w)
adj[w].append(v)
def BFS(adj, s, v):
visited = [False for i in range(v)]
q = []
q.append(s)
visited[s] = True
while (len(q) != 0):
p = q[0]
q.pop(0)
for it in adj[p]:
if (not visited[it]):
visited[it] = True
q.append(it)
for i in range(v):
if (not visited[i]):
print(i, end = ' ')
print()
if __name__=='__main__':
adj = [[] for i in range(8)]
add_edge(adj, 0, 1)
add_edge(adj, 0, 2)
add_edge(adj, 1, 2)
add_edge(adj, 3, 4)
add_edge(adj, 4, 5)
add_edge(adj, 6, 7)
BFS(adj, 0, 8)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void add_edge(List<int> []adj,
int v, int w)
{
adj[v].Add(w);
adj[w].Add(v);
}
static void BFS(List<int> []adj, int s,
int v)
{
bool []visited = new bool[v];
List<int> q = new List<int>();
q.Add(s);
visited[s] = true;
while (q.Count != 0) {
int p = q[0];
q.RemoveAt(0);
foreach (int it in adj[p]) {
if (!visited[it]) {
visited[it] = true;
q.Add(it);
}
}
}
for (int i = 0; i < v; i++) {
if (!visited[i]) {
Console.Write(i + " ");
}
}
Console.Write("\n");
}
public static void Main(String[] args)
{
List<int> []adj = new List<int>[8];
for (int i = 0; i < 8; i++)
adj[i] = new List<int>();
add_edge(adj, 0, 1);
add_edge(adj, 0, 2);
add_edge(adj, 1, 2);
add_edge(adj, 3, 4);
add_edge(adj, 4, 5);
add_edge(adj, 6, 7);
BFS(adj, 0, 8);
}
}
|
Javascript
<script>
function add_edge(adj, v, w){
adj[v].push(w)
adj[w].push(v)
}
function BFS(adj, s, v){
let visited = new Array(v).fill(false)
let q = []
q.push(s)
visited[s] = true
while (q.length != 0){
let p = q.shift()
for(let it of adj[p]){
if (!visited[it]){
visited[it] = true
q.push(it)
}
}
}
for(let i=0;i<v;i++){
if (!visited[i]){
document.write(i,' ')
}
}
document.write("</br>")
}
let adj = new Array(8)
for(let i=0;i<8;i++){
adj[i] = new Array()
}
add_edge(adj, 0, 1)
add_edge(adj, 0, 2)
add_edge(adj, 1, 2)
add_edge(adj, 3, 4)
add_edge(adj, 4, 5)
add_edge(adj, 6, 7)
BFS(adj, 0, 8)
</script>
|