数据结构->图的运算
来源:互联网 发布:mac os pc装机 编辑:程序博客网 时间:2024/05/29 08:10
1、键盘输入数据,建立一个有向图的邻接表。
2、输出该邻接表。
3、在有向图的邻接表的基础上计算各顶点的度,并输出。
4、以有向图的邻接表为基础实现输出它的拓扑排序序列。
5、采用邻接表存储实现有向图的深度优先递归遍历。
6、编写一个主函数,调试上述算法。
#include <stdio.h>#include <stdlib.h>#define MAXVNUM 100 //顶点最大个数#define VertexType inttypedef struct Node{ int adjvex; struct Node *nextarc; int weight;} ArcNode;typedef struct{ int degree,indegree; VertexType data; ArcNode *firstarc;} VNode;typedef struct{ VNode vertices[MAXVNUM]; int vexnum,arcnum;//顶点的实际数,边的实际数} ALGraph;void CreatAdjList(ALGraph *A){ int n,i; scanf("%d",&n); A->vexnum=n; for(i=1; i<=n; i++) { A->vertices[i].data=i; A->vertices[i].firstarc=NULL; } int a,b; scanf("%d,%d",&a,&b); ArcNode *p; A->arcnum=0; while(a!=0&&b!=0) { A->arcnum++; p=(ArcNode*)malloc(sizeof(ArcNode)); p->adjvex=b; p->nextarc=A->vertices[a].firstarc; A->vertices[a].firstarc=p; scanf("%d,%d",&a,&b); }}int visited[MAXVNUM];void DFSTraverse(ALGraph A){ printf("图的深度遍历 "); int v; for(v=1; v<=A.vexnum; v++) visited[v]=0; for(v=1; v<=A.vexnum; v++) { if(!visited[v]) DFS(A,v); }}VertexType GraphFirstAdj(ALGraph A,int v){ ArcNode *p=A.vertices[v].firstarc; if(p==NULL) return 0; else return p->adjvex;}VertexType GraphNextAdj(ALGraph A,int v,int w){ ArcNode *p=A.vertices[v].firstarc; if(p==NULL) return 0; else { while(p->adjvex!=w) { p=p->nextarc; } if(p->adjvex==w&&p->nextarc!=NULL) return p->nextarc->adjvex; else return 0; }}void DFS(ALGraph A,int v){ printf("%d ",A.vertices[v].data); visited[v]=1; VertexType w; for(w=GraphFirstAdj(A,v); w; w=GraphNextAdj(A,v,w)) { if(!visited[w]) DFS(A,w); }}void print(ALGraph A){ int v; ArcNode *p; for(v=1; v<=A.vexnum; v++) { printf("%d ",v); p=A.vertices[v].firstarc; while(p!=NULL) { printf("%d ",p->adjvex); p=p->nextarc; } printf("\n"); }}void degree(ALGraph *A){ int v; ArcNode *p; for(v=1; v<=A->vexnum; v++) { A->vertices[v].indegree=0; A->vertices[v].degree=0; } for(v=1; v<=A->vexnum; v++) { p=A->vertices[v].firstarc; while(p!=NULL) { A->vertices[v].degree++; A->vertices[p->adjvex].indegree++; p=p->nextarc; } } for(v=1; v<=A->vexnum; v++) { printf("顶点 <%d> 入度 %d 出度 %d\n",v,A->vertices[v].indegree, A->vertices[v].degree); }}void toposort(ALGraph A){ int top=0,k,v,stack[MAXVNUM]; for(v=1; v<=A.vexnum; v++) if(A.vertices[v].indegree==0) stack[top++]=v; ArcNode *p; printf("拓扑排序为 "); while(top!=0) { v=stack[--top]; printf("%d -> ",A.vertices[v].data); p=A.vertices[v].firstarc; while(p!=NULL) { k=p->adjvex; A.vertices[k].indegree--; if(A.vertices[k].indegree==0) stack[top++]=k; p=p->nextarc; } } puts("");}int main(){ ALGraph A; CreatAdjList(&A); printf("打印邻接表\n"); print(A); printf("计算出度入度\n"); degree(&A); toposort(A); DFSTraverse(A); return 0;}/*51,24,25,43,53,10,061,32,32,42,53,43,64,65,60,0*/
阅读全文
0 0
- 数据结构->图的运算
- 数据结构栈的运算
- 数据结构栈的运算
- 数据结构-位运算的使用
- 【数据结构】单链表上的基本运算
- 数据结构 线性链表的运算
- 队列的数据结构及基本运算
- 经典数据结构之矩阵的基本运算
- 数据结构 单链表的应用:多项式及其运算
- 数据结构实验:一元多项式的运算
- 【数据结构之顺序栈的基本运算】
- 【数据结构之链栈的基本运算】
- 【数据结构之链队列的基本运算】
- 数据结构 顺序串的各种基本运算
- 数据结构之顺序表的基本运算
- 数据结构之二叉树的各种运算
- 数据结构 - 串的基本运算实现
- 数据结构学习-二叉树的基本运算
- 51nod 1172 Partial Sums V2 任意模数FFT
- 高级用法
- 程序员偷偷深爱的9个不良编程习惯
- python随机数
- SQL Server 返回结果集的几种方式
- 数据结构->图的运算
- 补充:webpack入门
- Beautiful Soup 4.4.0 文档
- EL技术(三)
- 教你如何编写Linux启动或重启时的执行命令/脚本
- spring通过构造注入
- java web第一天
- web项目的构建流程及webpack3.10.0的使用
- python学习—Day45—消息队列