connected components of an undirected graph

i have written a program to find the connected components of an undirected graph with the concept of depth-first search..but there seems to be some problem as the program keeps hanging and then does not respond..can anyone help debugging it? its urgent. thanx in advance!

#include <iostream.h>
struct node
{
int vertex_no;
int adj[20];
};
node V[20];
int v;
char color[20];

void dfs_visit(int i)
{
color[i]='g';
for (int j=0; V[i].adj[j]!=0;j++)
{
if(color[j]=='w')
dfs_visit(V[i].adj[j]);
}
color[i]='b';
}
int main()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
V[i].adj[j]=0;
cout<<"Enter the number of vertices in the graph : ";
cin>>v;
cout<<"Graph contains following vertices : ";
for(int i=1;i<=v;i++)
{
cout<<i;
if(i<v)
cout<<", ";
}
cout<<"\n";
for(int i=1;i<=v;i++)
{
cout<<"\nEnter adjacents of vertex "<<i<<" one by one. \n( Press 0 when no more adjacents have to be added )\n";
int n=100;
int mark=0,j=0;
while(n!=0)
{
cin>>n;
if((n>v)||(n==i))
{
cout<<"Please enter a valid vertex";
continue;
}
V[i].adj[mark]=n;
mark++;
}
}
cout<<"The undirected graph you entered is as follows in adjanceny list representation:\n";
for(int i=1;i<=v;i++)
{
cout<<i;
for(int j=0;j<20;j++)
{
if(V[i].adj[j]==0)
break;
cout<<"-->"<<V[i].adj[j];
}
cout<<"\n";
}

//Using DFS to find out the connected components of the graph

for(int i=0;i<=v;i++)
{
color[i]='w';
}

void dfs_visit(int i);

for(int i=1;i<=v;i++)
{
if(i>1)
{
cout<<"Connected component "<<i-1<<" :";
for(int i=1;i<=v;i++)
{
if(color[v]=='b')
cout<<i<<" ";
color[v]='z';
}
cout<<"\n";
}
if(color[i]=='w')
dfs_visit(i);
}


cout<<"\n";
system("pause");
}
Topic archived. No new replies allowed.