Given a directed graph. The task is to check if the given graph is connected or not.
- Take two bool arrays vis1 and vis2 of size N (number of nodes of a graph) and keep false in all indexes.
- Start at a random vertex v of the graph G, and run a DFS(G, v).
- Make all visited vertices v as vis1[v] = true.
- Now reverse the direction of all the edges.
- Start DFS at the vertex which was chosen at step 2.
- Make all visited vertices v as vis2[v] = true.
- If any vertex v has vis1[v] = false and vis2[v] = false then the graph is not connected.
Below is the implementation of the above approach:
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.
- Convert undirected connected graph to strongly connected directed graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Minimum edges required to make a Directed Graph Strongly Connected
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if a given Graph is 2-edge connected or not
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if there exists a connected graph that satisfies the given conditions
- Check if longest connected component forms a palindrome in undirected graph
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Find if there is a path between two vertices in a directed graph
- Detect Cycle in a Directed Graph
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph
- Euler Circuit in a Directed Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Assign directions to edges so that the directed graph remains acyclic
- Detect Cycle in a directed graph using colors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.