第13周项目验证算法(5)
2015-12-18 08:56
363 查看
问题及代码:
运行结果:
知识点总结:
拓扑排序算法的验证
/* 文件名称:项目1.5.cpp 作者:孙洁 完成日期: 2015.12.18 问题描述: (5)拓扑排序算法验证(使用图4作为测试用例) 输入描述: 程序输出: 相关数据 */ #include <stdio.h> #include <malloc.h> #include "graph.h" void TopSort(ALGraph *G) { int i,j; int St[MAXV],top=-1; //栈St的指针为top ArcNode *p; for (i=0; i<G->n; i++) //入度置初值0 G->adjlist[i].count=0; for (i=0; i<G->n; i++) //求所有顶点的入度 { p=G->adjlist[i].firstarc; while (p!=NULL) { G->adjlist[p->adjvex].count++; p=p->nextarc; } } for (i=0; i<G->n; i++) if (G->adjlist[i].count==0) //入度为0的顶点进栈 { top++; St[top]=i; } while (top>-1) //栈不为空时循环 { i=St[top]; top--; //出栈 printf("%d ",i); //输出顶点 p=G->adjlist[i].firstarc; //找第一个相邻顶点 while (p!=NULL) { j=p->adjvex; G->adjlist[j].count--; if (G->adjlist[j].count==0)//入度为0的相邻顶点进栈 { top++; St[top]=j; } p=p->nextarc; //找下一个相邻顶点 } } } int main() { ALGraph *G; int A[7][7]= { {0,0,1,0,0,0,0}, {0,0,0,1,1,0,1}, {0,0,0,1,0,0,0}, {0,0,0,0,1,1,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,1,0} }; ArrayToList(A[0], 7, G); DispAdj(G); printf("\n"); printf("拓扑序列:"); TopSort(G); printf("\n"); return 0; }
运行结果:
知识点总结:
拓扑排序算法的验证
相关文章推荐
- ES6学习——新的语法:let
- Filter
- 第16周项目1 验证算法(7)归并排序
- 第八周项目1建立顺序串算法库
- 第15周实践项目一(3)---希尔排序
- 使用 Android 的日志工具LogCat
- 【解决】Word 在试图打开文件时遇到错误 请尝试下列方法:* xxx * xxx * xxx
- 关于java代码质量的问题
- 关于java代码质量的问题
- 详解Java的Hibernate框架中的List映射表与Bag映射
- 本学期数据结构学习总结
- Nim Game LeetCode
- 期末回回首~
- JS 操作日期
- 第16周项目1 验证算法(8)基数排序
- 关于java代码质量的问题
- php之smarty学习一
- 第十四周 是否是二叉树(补)
- 数据结构实践——验证算法
- 第四周 项目3-单链表的应用