0

I have written the code to find the number of Connected Components of Directed Graph.When I use the Diagram below as my Adjacency Matrix.It gives the number of Connected Components as 2(First DFS: 0->1->2,Second DFS: 3).

enter image description here

But when I use the Diagram below as my Adjacency Matrix

enter image description here

It gives the number of Connected Components as 1(DFS: 0->2->3->1).So what I want to ask is calculating the number of Connected Components will depend on how we represent nodes in Adjacency Matrix if we use DFS to find the number of Connected Components?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct Graph
{
    int V;
    int E;
    int **Adj;
};


void AdjacencyMatrixOfGraph(struct Graph *G)
{
    int u,v,i,j,w;
    scanf("%d %d",&G->E,&G->V);
    G->Adj = (int**)malloc(G->V*sizeof(int *));
    for(i=0;i<G->V;i++)
    {
        G->Adj[i] = (int*)malloc(G->V*sizeof(int));
    }
    for(i=0;i<G->V;i++)
    {
        for(j=0;j<G->V;j++)
        {
            G->Adj[i][j] = 0;
        }

    }
    for(i=0;i<G->E;i++)
    {
        scanf("%d %d",&u,&v);
        G->Adj[u][v] = 1;
        //G->Adj[v][u] = 1;
    }
}
int Visited[1000];
void DFS(struct Graph *G,int u,int Visited[])
{
    Visited[u]=1;
    int v,w,i;
    for(v=0;v<G->V;v++)
    {
        if(G->Adj[u][v] !=0 && Visited[v] == 0)
        {
            //printf("U is %d and V is %d\n",u,v);
            Visited[v] = 1;
            DFS(G,v,Visited);
        }
    }

}

void DFSTraversal(struct Graph *G)
{
    //int Visited[G->V];
    int i;
    int counter = 0;
    for(i=0;i<G->V;i++)
    {
        Visited[i] = 0;
    }
    for(i=0;i<G->V;i++)
    {
        if(!Visited[i])
        {
            DFS(G,i,Visited);
            counter++;
        }
    }
    printf("The Number of Connected Components is %d\n",counter);
}
int main()
{

    struct Graph *graph = (struct Graph *)malloc(sizeof(struct Graph));
    AdjacencyMatrixOfGraph(graph);
    DFSTraversal(graph);
    return 0;

}
Dominique Fortin
  • 2,212
  • 15
  • 20
Abhi Jain
  • 35
  • 1
  • 7

1 Answers1

0

There are no non-trivial Strongly Connected Components (SCCs) in your graph. (There is no path from any vertex to itself.) So both of the answers "one" and "two" are wrong; the correct answer is four.

Your algorithm does not find SCCs. The algorithm could find Connected Components in an undirected graph, but your adjacency list would need to be modified to make the graph undirected, since you need to find the edge from either end.

rici
  • 234,347
  • 28
  • 237
  • 341
  • My mistake.I wrote Strongly Connected Components instead of Connected Components. Yes the algorithm is for number of Connected Components in directed graph not Strongly Connected Components.But how four? – Abhi Jain May 30 '16 at 20:57
  • I'm not sure how much sense it makes to speak of a "connected component" of a directed graph. How would you define that? Whatever it might mean, your algorithm does not compute it Your algorithm would compute the connected components of an undirected graph, which is a well-defined concept. – rici May 30 '16 at 23:00
  • Thanks for the explanation. – Abhi Jain May 31 '16 at 05:24