c++判断图连通性
答案:1 悬赏:0 手机版
解决时间 2021-01-28 13:51
- 提问者网友:骑士
- 2021-01-28 08:58
判断图的连通性的最优算法是什么啊,用c++怎么编码实现呢
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-01-28 10:04
#define NumberofVertices 20
struct Edge{
int dest;
struct Edge*link;
}
struct Vertex{
int data;
struct Edge* adj;
}
struct Vertex NodeTable[NumberofVertices];
int GetFirstNeighbor(int v)
{
if(v!=-1)
{
struct Edge* p=NodeTable[v].adj;
if(p)return p->dest;
}
return -1;
}
int GetNextNeighbor(int v,int w)
{
if(v!=-1)
{
struct Edge* p=NodeTable[v].adj;
while(p)
{
if(p->dest==w &&p->link)return p->link->dest;
else p=p->link;
}
}
return -1;
}
void DFS(int v,int visited[])
{
int w;
visited[v]=1;
w=GetFirstNeighbor(v);
while(w!=-1)
{
if(visited[w]==0)DFS(w,visited);
w=GetNextNeighbor(v,w);
}
}
int Isconect()
{
int i,x=0;
int n=NumberofVertices;
int* visited=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)visited[i]=0;
for(i=0;i<n;i++)
if(visited[i]==0)
{
DFS(i,visited);
x++;
}
free(visited);
if(x>1)
{printf("It is not connected!\n");return 1;}
else
printf("It is connected!\n");
return 0;
}
struct Edge{
int dest;
struct Edge*link;
}
struct Vertex{
int data;
struct Edge* adj;
}
struct Vertex NodeTable[NumberofVertices];
int GetFirstNeighbor(int v)
{
if(v!=-1)
{
struct Edge* p=NodeTable[v].adj;
if(p)return p->dest;
}
return -1;
}
int GetNextNeighbor(int v,int w)
{
if(v!=-1)
{
struct Edge* p=NodeTable[v].adj;
while(p)
{
if(p->dest==w &&p->link)return p->link->dest;
else p=p->link;
}
}
return -1;
}
void DFS(int v,int visited[])
{
int w;
visited[v]=1;
w=GetFirstNeighbor(v);
while(w!=-1)
{
if(visited[w]==0)DFS(w,visited);
w=GetNextNeighbor(v,w);
}
}
int Isconect()
{
int i,x=0;
int n=NumberofVertices;
int* visited=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)visited[i]=0;
for(i=0;i<n;i++)
if(visited[i]==0)
{
DFS(i,visited);
x++;
}
free(visited);
if(x>1)
{printf("It is not connected!\n");return 1;}
else
printf("It is connected!\n");
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯