Be the first user to complete this post

  • 0
Add to List
Medium

276. Graph – Find Number of non reachable vertices from a given vertex

Objective: Given undirected graph write an algorithm to count the number of non reachable vertices from a given vertex

Example:

Approach:

Use DFS

  1. Do the DFS (Depth-First Search) from the given vertex A, Recursively do the DFS from all the adjacent vertices of given vertex A.
  2. Use the visited array to keep track of visited vertices.
  3. Once the DFS is completed, count the number of false in visited array. These are the vertices which did not get visit.

Output:

Number of non-reachable vertices from the vertex 0 are: 4
Number of non reachable vertices from the vertex 6 are: 5