Check if a directed graph is connected or not

Given a directed graph. The task is to check if the given graph is connected or not.

Examples:

Input:

Output: Yes



Input:

Output: No

Approach:

  1. Take two bool arrays vis1 and vis2 of size N (number of nodes of a graph) and keep false in all indexes.
  2. Start at a random vertex v of the graph G, and run a DFS(G, v).
  3. Make all visited vertices v as vis1[v] = true.
  4. Now reverse the direction of all the edges.
  5. Start DFS at the vertex which was chosen at step 2.
  6. Make all visited vertices v as vis2[v] = true.
  7. 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define N 100000
  
// To keep correct and reverse direction
vector<int> gr1[N], gr2[N];
  
bool vis1[N], vis2[N];
  
// Function to add edges
void Add_edge(int u, int v)
{
    gr1[u].push_back(v);
    gr2[v].push_back(u);
}
  
// DFS function
void dfs1(int x)
{
    vis1[x] = true;
  
    for (auto i : gr1[x])
        if (!vis1[i])
            dfs1(i);
}
  
// DFS function
void dfs2(int x)
{
    vis2[x] = true;
  
    for (auto i : gr2[x])
        if (!vis2[i])
            dfs2(i);
}
  
bool Is_Connected(int n)
{
    // Call for correct direction
    memset(vis1, false, sizeof vis1);
    dfs1(1);
  
    // Call for reverse direction
    memset(vis2, false, sizeof vis2);
    dfs2(1);
  
    for (int i = 1; i <= n; i++) {
  
        // If any vertex it not visited in any direction
        // Then graph is not connected
        if (!vis1[i] and !vis2[i])
            return false;
    }
  
    // If graph is connected
    return true;
}
  
// Driver code
int main()
{
    int n = 4;
  
    // Add edges
    Add_edge(1, 2);
    Add_edge(1, 3);
    Add_edge(2, 3);
    Add_edge(3, 4);
  
    // Function call
    if (Is_Connected(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
    static int N = 100000;
  
    // To keep correct and reverse direction
    @SuppressWarnings("unchecked")
    static Vector<Integer>[] gr1 = new Vector[N];
    @SuppressWarnings("unchecked")
    static Vector<Integer>[] gr2 = new Vector[N];
  
    static boolean[] vis1 = new boolean[N];
    static boolean[] vis2 = new boolean[N];
  
    static {
        for (int i = 0; i < N; i++)
        {
            gr1[i] = new Vector<>();
            gr2[i] = new Vector<>();
        }
    }
  
    // Function to add edges
    static void Add_edge(int u, int v)
    {
        gr1[u].add(v);
        gr2[v].add(u);
    }
  
    // DFS function
    static void dfs1(int x)
    {
        vis1[x] = true;
        for (int i : gr1[x])
            if (!vis1[i])
                dfs1(i);
    }
  
    // DFS function
    static void dfs2(int x) 
    {
        vis2[x] = true;
        for (int i : gr2[x])
            if (!vis2[i])
                dfs2(i);
    }
  
    static boolean Is_connected(int n)
    {
  
        // Call for correct direction
        Arrays.fill(vis1, false);
        dfs1(1);
  
        // Call for reverse direction
        Arrays.fill(vis2, false);
        dfs2(1);
  
        for (int i = 1; i <= n; i++)
        {
  
            // If any vertex it not visited in any direction
            // Then graph is not connected
            if (!vis1[i] && !vis2[i])
                return false;
        }
  
        // If graph is connected
        return true;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
  
        // Add edges
        Add_edge(1, 2);
        Add_edge(1, 3);
        Add_edge(2, 3);
        Add_edge(3, 4);
  
        // Function call
        if (Is_connected(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by
// sanjeev2552

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.



Improved By : sanjeev2552



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.