Given an undirected graph with **N** vertices and **K** edges, the task is to check if for every combination of three vertices in the graph, there exists two vertices which are connected to third vertex. In other words, for every vertex triplet **(a, b, c)**, if there exists a path between **a** and **c**, then there should also exist a path between **b** and **c**.

**Examples:**

Input:N= 4, K = 3

Edges: 1 -> 2, 2 -> 3, 3 -> 4Output:YESExplanation:

Since the whole graph is connected, the above condition will always be valid.

Input:N = 5 and K = 3

Edges: 1 -> 3, 3 -> 4, 2 -> 5.Output:NOExplanation:

If we consider the triplet (1, 2, 3) then there is a path between vertices 1 and 3 but there is no path between vertices 2 and 3.

**Approach:** Follow the steps below to solve the problem –

- Traverse the graph by DFS Traversal technique from any component and maintain two variables to store the component minimum and component maximum.
- Store every component maximum and minimum in a vector.
- Now, if any two components have an intersection in their minimum and maximum values interval, then there will exist a valid (a < b < c) triplet. Hence, both of the components should be connected. Otherwise, the graph is not valid.

Below is the implementation of the above approach

## C++

`// C++ program of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to add edge into ` `// the graph ` `void` `addEdge(vector<` `int` `> adj[], ` ` ` `int` `u, ` `int` `v) ` `{ ` ` ` `adj[u].push_back(v); ` ` ` `adj[v].push_back(u); ` `} ` ` ` `void` `DFSUtil(` `int` `u, vector<` `int` `> adj[], ` ` ` `vector<` `bool` `>& visited, ` ` ` `int` `& componentMin, ` ` ` `int` `& componentMax) ` `{ ` ` ` `visited[u] = ` `true` `; ` ` ` ` ` `// Finding the maximum and ` ` ` `// minimum values in each component ` ` ` `componentMax = max(componentMax, u); ` ` ` `componentMin = min(componentMin, u); ` ` ` ` ` `for` `(` `int` `i = 0; i < adj[u].size(); i++) ` ` ` `if` `(visited[adj[u][i]] == ` `false` `) ` ` ` `DFSUtil(adj[u][i], adj, visited, ` ` ` `componentMin, componentMax); ` `} ` ` ` `// Function for checking whether ` `// the given graph is valid or not ` `bool` `isValid(vector<pair<` `int` `, ` `int` `> >& v) ` `{ ` ` ` `int` `MAX = -1; ` ` ` `bool` `ans = 0; ` ` ` `// Checking for intersecting intervals ` ` ` `for` `(` `auto` `i : v) { ` ` ` `// If intersection is found ` ` ` `if` `(i.first <= MAX) { ` ` ` ` ` `// Graph is not valid ` ` ` `ans = 1; ` ` ` `} ` ` ` ` ` `MAX = max(MAX, i.second); ` ` ` `} ` ` ` ` ` `return` `(ans == 0 ? 1 : 0); ` `} ` ` ` `// Function for the DFS Traversal ` `void` `DFS(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `std::vector<pair<` `int` `, ` `int` `> > v; ` ` ` `// Traversing for every vertex ` ` ` `vector<` `bool` `> visited(V, ` `false` `); ` ` ` `for` `(` `int` `u = 1; u <= V; u++) { ` ` ` `if` `(visited[u] == ` `false` `) { ` ` ` `int` `componentMax = u; ` ` ` `int` `componentMin = u; ` ` ` ` ` `DFSUtil(u, adj, visited, ` ` ` `componentMin, componentMax); ` ` ` ` ` `// Storing maximum and minimum ` ` ` `// values of each component ` ` ` `v.push_back({ componentMin, ` ` ` `componentMax }); ` ` ` `} ` ` ` `} ` ` ` ` ` `bool` `check = isValid(v); ` ` ` ` ` `if` `(check) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 4, K = 3; ` ` ` ` ` `vector<` `int` `> adj[N + 1]; ` ` ` ` ` `addEdge(adj, 1, 2); ` ` ` `addEdge(adj, 2, 3); ` ` ` `addEdge(adj, 3, 4); ` ` ` ` ` `DFS(adj, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(N + E)**Auxiliary Space: **O(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:

- Find K vertices in the graph which are connected to at least one of remaining vertices
- Convert undirected connected graph to strongly connected directed graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Find a triplet such that sum of two equals to third element
- All vertex pairs connected with exactly k edges in a graph
- Find the remaining vertices of a square from two given vertices
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Queries to count connected components after removal of a vertex from a Tree
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Make a tree with n vertices , d diameter and at most vertex degree k
- Maximum difference of count of black and white vertices in a path containing vertex V
- Find the vertex diagonally opposite to the vertex M from an N-sided polygon
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Check if a directed graph is connected or not
- Check if there exists a connected graph that satisfies the given conditions
- Check if longest connected component forms a palindrome in undirected graph

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.