Given an **undirected graph** and two **vertices X and Y**, our task is to check whether the vertex X lies in the subgraph of the vertex Y.**Examples:**

Input:X = 2, Y = 3Output:NoExplanation:

Subgraph of a vertex Y = 3 is set of all the vertex which lies below Y and are reachable by Y. Here subgraph of 3 contains {6} not 2.Input:X = 6, Y = 1Output:YesExplanation:

Subgraph of 1 contain {2, 3, 4, 5, 6} so 6 lies in subgraph of 1.

**Approach:** The idea is to use Depth First Search(DFS). Initialize two arrays *in* and *out* for maintaining the start time of traversing a vertex and end time to mark until the vertex traversed. If the starting time of the second vertex is less than the starting time of the first vertex and the ending time of the first vertex is less than that of the second vertex then return true else return false.

Below is the implementation of the above approach:

## C++

`// C++ implementation to check if vertex X ` `// lies in subgraph of vertex Y ` `// for the given graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `int` `cnt = 1; ` ` ` `// Function ot perform dfs ` `void` `dfs(vector<` `int` `> v[], ` `int` `in[], ` ` ` `int` `out[], ` `int` `visited[], ` `int` `i) ` `{ ` ` ` `// Mark visited of vertex i ` ` ` `visited[i] = 1; ` ` ` ` ` `// Update starting time ` ` ` `// of vertex i ` ` ` `in[i] = cnt; ` ` ` ` ` `// Increment the cnt ` ` ` `cnt++; ` ` ` ` ` `for` `(` `auto` `x : v[i]) { ` ` ` `// Check if not visited ` ` ` `// call dfs from x ` ` ` `if` `(!visited[x]) ` ` ` `dfs(v, in, out, visited, x); ` ` ` `} ` ` ` ` ` `// Update ending time ` ` ` `// of vertex i ` ` ` `out[i] = cnt; ` ` ` ` ` `// Increment the cnt ` ` ` `cnt++; ` `} ` ` ` `// Function to add edges in graph ` `void` `addedge(vector<` `int` `> v[], ` `int` `x, ` `int` `y) ` `{ ` ` ` `v[x].push_back(y); ` ` ` `v[y].push_back(x); ` `} ` ` ` `// Function to check if vertex X ` `// lies in subgraph of vertex Y ` `// for the given graph ` `bool` `is_subtree(vector<` `int` `> v[], ` `int` `n, ` ` ` `int` `m, ` `int` `x, ` `int` `y) ` `{ ` ` ` `// Arrays for starting time, ` ` ` `// ending time and to check ` ` ` `// for visited respectively ` ` ` `int` `in[n + 1], out[n + 1], visited[n + 1]; ` ` ` ` ` `// Mark all vertices starting time, ` ` ` `// ending time and visited as zero ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `in[i] = 0; ` ` ` `out[i] = 0; ` ` ` `visited[i] = 0; ` ` ` `} ` ` ` ` ` `// Check if y comes before x ` ` ` `// and leaves after x then x lies ` ` ` `// in the subgraph of y ` ` ` `// call dfs from any vertex, ` ` ` `// here we have called from 1 ` ` ` `dfs(v, in, out, visited, 1); ` ` ` `if` `(in[y] < in[x] && out[y] > out[x]) ` ` ` `return` `true` `; ` ` ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// n number of vertices ` ` ` `// m number of edges ` ` ` `int` `n = 6, m = 5; ` ` ` ` ` `// Create a graph given ` ` ` `// in the above diagram ` ` ` `vector<` `int` `> v[n + 1]; ` ` ` `addedge(v, 1, 2); ` ` ` `addedge(v, 1, 3); ` ` ` `addedge(v, 2, 4); ` ` ` `addedge(v, 1, 5); ` ` ` `addedge(v, 3, 6); ` ` ` ` ` `int` `x = 6, y = 1; ` ` ` `if` `(is_subtree(v, n, m, x, y)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to check if ` `# vertex X lies in subgraph of ` `# vertex Y for the given graph ` `cnt ` `=` `1` ` ` `# Function to perform dfs ` `def` `dfs(v, in_, out, visited, i): ` ` ` ` ` `global` `cnt ` ` ` ` ` `# Mark visited of vertex i ` ` ` `visited[i] ` `=` `1` ` ` ` ` `# Update starting time ` ` ` `# of vertex i ` ` ` `in_[i] ` `=` `cnt ` ` ` ` ` `# Increment the cnt ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `# Check if not visited ` ` ` `# call dfs from x ` ` ` `for` `x ` `in` `v[i]: ` ` ` `if` `not` `visited[x]: ` ` ` `dfs(v, in_, out, visited, x) ` ` ` ` ` `# Update ending time ` ` ` `# of vertex i ` ` ` `out[i] ` `=` `cnt ` ` ` ` ` `# Increment the cnt ` ` ` `cnt ` `+` `=` `1` ` ` `# Function to add edges in graph ` `def` `addedge(v, x, y): ` ` ` ` ` `v[x].append(y) ` ` ` `v[y].append(x) ` ` ` `# Function to check if vertex X ` `# lies in subgraph of vertex Y ` `# for the given graph ` `def` `is_subtree(v, n, m, x, y): ` ` ` ` ` `# Arrays for starting time, ` ` ` `# ending time and to check ` ` ` `# for visited respectively ` ` ` ` ` `# Mark all vertices starting time, ` ` ` `# ending time and visited as zero ` ` ` `in_ ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` `out ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` `visited ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` ` ` `# Check if y comes before x ` ` ` `# and leaves after x then x lies ` ` ` `# in the subgraph of y ` ` ` `# call dfs from any vertex, ` ` ` `# here we have called from 1 ` ` ` `dfs(v, in_, out, visited, ` `1` `) ` ` ` `if` `in_[y] < in_[x] ` `and` `out[y] > out[x]: ` ` ` `return` `True` ` ` `else` `: ` ` ` `return` `False` ` ` `# Driver code ` ` ` `# n number of vertices ` `# m number of edges ` `n, m ` `=` `6` `, ` `5` ` ` `# Create a graph given ` `# in the above diagram ` `v ` `=` `[] ` `for` `i ` `in` `range` `(n ` `+` `1` `): ` ` ` `v.append([]) ` ` ` `addedge(v, ` `1` `, ` `2` `) ` `addedge(v, ` `1` `, ` `3` `) ` `addedge(v, ` `2` `, ` `4` `) ` `addedge(v, ` `1` `, ` `5` `) ` `addedge(v, ` `3` `, ` `6` `) ` ` ` `x, y ` `=` `6` `, ` `1` ` ` `if` `is_subtree(v, n, m, x, y): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Stuti Pathak ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(V + E) **Space Complexity:** O(3*N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Find the Degree of a Particular vertex in a Graph
- Find a Mother Vertex in a Graph
- All vertex pairs connected with exactly k edges in a graph
- Find a Mother vertex in a Graph using Bit Masking
- Find dependencies of each Vertex in a Directed Graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Topological Sort of a graph using departure time of vertex
- k'th heaviest adjacent node in a graph where each vertex has weight
- Add and Remove vertex in Adjacency List representation of Graph
- Finding minimum vertex cover size of a graph using binary search
- Print all possible paths in a DAG from vertex whose indegree is 0
- Finding the path from one vertex to rest using BFS
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Sum of shortest distance on source to destination and back having at least a common vertex
- Maximum difference of count of black and white vertices in a path containing vertex V
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices

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.