void Top_Sort(VexNode g[], int n,VexNode *temp) //用有入度域的aov网进行拓扑排序,输出并存到数组temp中 { int i, j, k, top, m = 0; EdgeNode* p; top = -1; //链栈初始化,-1为栈尾 for(i = 0; i < n; i ++) //将入度为0的顶点链接成链栈 if (g[i].InDegree == 0) { g[i].InDegree = top; top = i; } printf("aov拓扑排序结果为:"); while (top != -1) //当链栈非空时 { j = top; //将栈顶顶点记为j top = g[top].InDegree; //栈顶指针指向弹出栈后下一个入度为0的顶点 printf("%s ", g[j].Date);//输出顶点信息 temp[m] = g[j]; //将顶点信息有序保存到数组 m++; //记录已输出的顶点个数 p = g[j].FirstEdge; while (p != NULL) //删除顶点j的所有出边 { k = p->AdjVex; g[k].InDegree--; //将顶点j的邻接边节点k入度减1 if (g[k].InDegree == 0) //若顶点k入度为零则入链栈 { g[k].InDegree = top; top = k; } p = p->Next; //查找下一个邻接边节点 } } if (m < n) printf("AOV 网有回路!!!!!"); }代码实现思路
时间: 2024-02-14 13:05:52 浏览: 57
这段代码实现了有入度域的AOV网的拓扑排序,并将排序结果输出和保存到数组temp中。具体实现思路如下:
1. 将所有入度为0的顶点加入到链栈中。
2. 当链栈非空时,弹出栈顶顶点j,输出该顶点的信息,并将该顶点信息保存到数组temp中。
3. 遍历顶点j的所有出边,将其邻接顶点k的入度减1。如果邻接顶点k的入度为0,则将其加入到链栈中。
4. 重复第2、3步,直到链栈为空。
5. 如果输出的顶点个数m小于图中的顶点个数n,则说明图中存在环,输出"AOV网有回路!!!!!"的提示信息。
拓扑排序的核心思想是将有向无环图(DAG)中的顶点按照一定的顺序进行排列,使得对于每一条有向边(u, v),其起点u都排在终点v的前面。对于有入度域的AOV网,入度为0的顶点是没有前驱顶点的,可以最先加入到拓扑序列中;而每次删除一个顶点时,需要将该顶点的所有出边的邻接顶点的入度减1,如果邻接顶点的入度变为0,则该顶点可以加入到拓扑序列中。
相关问题
现在你扮演一名学生,已知typedef int ElemType; #define N 10 //用于存储图的邻接矩阵的数组 struct MGraph{ ElemType vertex[N];//用于存储顶点信息的数组 int arc[N][N]; //邻接矩阵,用于存储边信息的数组 int degree_out[N]; //顶点的出度 int degree_in[N]; //顶点的入度 }mGraph;为C语言函数定义部分,请你写出一个以DFS为函数名的深度优先遍历算法
好的,我来给你写一个深度优先遍历算法。如下所示:
```
void DFS(int v, bool visited[]) {
visited[v] = true; // 标记当前顶点已访问
printf("%d ", mGraph.vertex[v]); // 访问当前顶点
// 遍历当前顶点的邻接点
for (int i = 0; i < N; i++) {
if (mGraph.arc[v][i] == 1 && !visited[i]) {
DFS(i, visited); // 递归访问邻接点
}
}
}
void DFSTraverse() {
bool visited[N] = { false }; // 初始化为所有顶点未访问状态
for (int i = 0; i < N; i++) {
if (!visited[i]) {
DFS(i, visited); // 对未访问过的顶点进行深度优先遍历
}
}
}
```
其中,`DFS`函数是深度优先遍历的核心操作,参数`v`表示当前访问的顶点在`mGraph.vertex`数组中的下标,`visited`数组用于记录每个顶点是否已被访问过。该函数首先将当前顶点标记为已访问,并输出当前顶点的值,然后遍历当前顶点的所有邻接点,对未访问过的邻接点进行递归访问。
`DFSTraverse`函数是对整个图进行深度优先遍历的入口函数,它首先将所有顶点初始化为未访问状态,然后对未访问过的顶点依次进行深度优先遍历。
补全代码:#include<stdio.h> #include<malloc.h> #define N 20 //图的邻接表-边或弧存储 typedef struct EdgeNode { int adjvex; struct EdgeNode *next; } EdgeNode; //图的邻接表-顶点存储(增加了入度域) typedef struct VNode { char data; int ind; //顶点入度 struct EdgeNode *link; } VNode; //图的邻接表表示 typedef struct ALgraph { int vexnum,arcnum; VNode adjlist[N]; }ALGraph; void createGraph_list(ALGraph *g); int topSort(ALGraph *g); //读入数据创建带入度的邻接表 void createGraph_list(ALGraph *g) { } //拓扑排序,判断图中是否存在环,存在返回0 int topSort(ALGraph *g) { } int main() { ALGraph g; int i; EdgeNode *s; createGraph_list(&g); for(i=0; i<g.vexnum; i++) { printf("%c,%d:",g.adjlist[i].data,g.adjlist[i].ind); s=g.adjlist[i].link; while(s!=NULL) { printf("->%d",s->adjvex); s=s->next; } printf("\n"); } i=topSort(&g); printf("%d\n",i); if(i==1){ printf("no ring\n"); }else{ printf("has ring\n"); } return 0; }
下面是完整的代码:
```
#include<stdio.h>
#include<malloc.h>
#define N 20
//图的邻接表-边或弧存储
typedef struct EdgeNode {
int adjvex;
struct EdgeNode *next;
} EdgeNode;
//图的邻接表-顶点存储(增加了入度域)
typedef struct VNode {
char data;
int ind; //顶点入度
struct EdgeNode *link;
} VNode;
//图的邻接表表示
typedef struct ALgraph {
int vexnum,arcnum;
VNode adjlist[N];
}ALGraph;
//读入数据创建带入度的邻接表
void createGraph_list(ALGraph *g) {
int i,j,k;
char v1,v2;
EdgeNode *p;
printf("请输入顶点数和边数(用空格隔开):");
scanf("%d%d",&g->vexnum,&g->arcnum);
printf("请输入%d个顶点:\n",g->vexnum);
for(i=0; i<g->vexnum; i++)
{
getchar();
scanf("%c",&g->adjlist[i].data);
g->adjlist[i].ind=0;
g->adjlist[i].link=NULL;
}
printf("请输入%d条边(用空格隔开):\n",g->arcnum);
for(i=0; i<g->arcnum; i++)
{
getchar();
scanf("%c%c",&v1,&v2);
for(j=0; j<g->vexnum; j++)
{
if(g->adjlist[j].data==v1)
break;
}
for(k=0; k<g->vexnum; k++)
{
if(g->adjlist[k].data==v2)
break;
}
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex=k;
p->next=g->adjlist[j].link;
g->adjlist[j].link=p;
g->adjlist[k].ind++;
}
}
//拓扑排序,判断图中是否存在环,存在返回0
int topSort(ALGraph *g) {
int i,k;
int top=0;
int count=0;
EdgeNode *p;
int stack[N];
for(i=0; i<g->vexnum; i++)
{
if(g->adjlist[i].ind==0)
stack[++top]=i;
}
while(top!=0)
{
k=stack[top--];
count++;
p=g->adjlist[k].link;
while(p!=NULL)
{
g->adjlist[p->adjvex].ind--;
if(g->adjlist[p->adjvex].ind==0)
stack[++top]=p->adjvex;
p=p->next;
}
}
if(count<g->vexnum)
return 0;
else
return 1;
}
int main() {
ALGraph g;
int i;
EdgeNode *s;
createGraph_list(&g);
for(i=0; i<g.vexnum; i++)
{
printf("%c,%d:",g.adjlist[i].data,g.adjlist[i].ind);
s=g.adjlist[i].link;
while(s!=NULL)
{
printf("->%d",s->adjvex);
s=s->next;
}
printf("\n");
}
i=topSort(&g);
printf("%d\n",i);
if(i==1){
printf("no ring\n");
}else{
printf("has ring\n");
}
return 0;
}
```
这段代码实现了有向图的拓扑排序,并判断图中是否存在环。在主函数中,调用`createGraph_list`函数读入数据创建带入度的邻接表,然后遍历邻接表输出各个顶点的入度和出边,最后调用`topSort`函数进行拓扑排序,并根据排序结果判断图中是否存在环。
阅读全文
相关推荐

















