问答题 已知无向图G=(V,E)的邻接表,给出求图G的连通分量个数的算法。
【正确答案】struct ArcNode { int adjvex; ArcNode*nextarc; InfoType*info; }; typedef struct { VertexType data; ArcNode*firstarc; }VNode,AdjList[MAX_VERTEX_NUM] struct ALGraph { AdjList adjlist; int n,e; }ALGraph; static int visited En]; void dfs(ALGraph g,int V); int dfscount(ALGraph g) { int i,j;j=0; for(i=0;i<g—>n;i++) if(visited==0) j++; dfs(g,i); } } viod dfs(ALGraph g,int V) { ArcNode*P;: visited [v]=1: p=g->adjlist[v].firstarc; while(P!=Null){ if(visited[p->adjvex]==0) dfs(g,p—>adjvex); P=p—>nextarc;) }
【答案解析】