




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1.给定无向图,请用邻接矩阵表示法表示该图v4v5v3v2v1 #include#includeusing namespace std;#define MAX 20typedef int AdjMAXMAX;typedef structstring vexsMAX; /顶点表Adj arcs; /邻接矩阵int vexnum,arcnum; /图的顶点和弧数MGraph;int LocateVex(MGraph &G,string u);int CreateUDN(MGraph &G) int i,k,j;string v1,v2;coutG.vexnumG.arcnum;cout输入顶点:;for(i=0;iG.vexsi; /构造顶点数for(i=0;iG.vexnum;i+) /构造邻接矩阵for(j=0;jG.vexnum;j+)G.arcsij=0;for(k=0;kG.arcnum;k+)cout输入第k+1v1v2;i=LocateVex(G,v1); j=LocateVex(G,v2);G.arcsij=1;G.arcsji=1; /置的对称弧return 0;int LocateVex(MGraph &G,string u) /确定u在G中序号 int i;for (i=0;iG.vexnum;i+)if (u=G.vexsi) return i;if (i=G.vexnum) coutError u!endl; exit(1); return 0;void ShowG(MGraph &G)int i,j;for(i=0;iG.vexnum;i+)coutG.vexsi ;coutendl;for(i=0;iG.vexnum;i+)for(j=0;jG.vexnum;j+)coutG.arcsij ;coutendl;main()MGraph A;int a;a=CreateUDN(A);ShowG(A);2.分别使用邻接矩阵表示法和邻接表表示法,用深度优先搜索法遍历该图。#include# include # include # include using namespace std;int visited30;# define MAX_VERTEX_NUM 30# define OK 1/typedef int VertexType;typedef int InfoType;typedef struct ArcNode /弧 int adjvex; struct ArcNode *nextarc;ArcNode;typedef struct VNode/表头 int data; ArcNode *firstarc;VNode,AdjListMAX_VERTEX_NUM;typedef struct/图 AdjList vertices; int vexnum,arcnum; int kind;ALGraph;void CreateDG(ALGraph &G) int k,i,v1; coutendlG.vexnum; coutG.arcnum; for(i=1;i=G.vexnum;i+)/初使化表头 G.verticesi.data=i; G.verticesi.firstarc=NULL; for(k=1;k=G.vexnum;k+) /输入边 int v2; cout请输入与结点kv2; cout请输入与第kv1; ArcNode *p; p=(ArcNode*)malloc(sizeof(ArcNode); if(!p) exit(-1); p-adjvex=v1; p-nextarc=NULL; G.verticesk.firstarc=p; for(int i=1;iv2;i+) int m; cout请输入与第km; ArcNode *q; q=(ArcNode *)malloc(sizeof(ArcNode);/动态指针 if(!q) exit(-1); q-adjvex=m; /顶点给P q-nextarc=NULL; p-nextarc=q; p=q; /free(q); /free(p); void DFS (ALGraph G,int v )/深度搜索 visitedv=1; coutG.verticesv.datanextarc) w=x-adjvex; if(visitedw=0) DFS(G,w); void DFSB (ALGraph G,int v)/深度搜索的边集 visitedv=1; ArcNode *y; y=(ArcNode*)malloc(sizeof(ArcNode); if(!y) exit(-1); y=G.verticesv.firstarc; int u=G.verticesv.data; int w; for(;y;y=y-nextarc) w=y-adjvex; if(visitedw=0) coutuwnext=NULL;void EnQueue (LinkQueue &Q,int e)/进队 QNode *p; p=(QNode*)malloc(sizeof(QNode); if(!p) exit(-1); p-data=e; p-next=NULL; Q.rear-next=p; Q.rear=p; /free(p);int DeQueue (LinkQueue &Q,int &e)/出队 if(Q.front=Q.rear) return -1; QNode *p; p=(QNode*)malloc(sizeof(QNode); if(!p) exit(-1); p=Q.front-next; e=p-data; Q.front-next=p-next; if(Q.rear=p) Q.rear=Q.front; free(p); return e;int QueueEmpty (LinkQueue Q)/判断队列是否为空 if(Q.front=Q.rear) return 1; return 0; void BFS(ALGraph G,int v)/广度搜索 int u; LinkQueue Q; InitQueue(Q); if(visitedv=0) visitedv=1; coutG.verticesv.dataadjvex;w=0;w=z-nextarc-adjvex) if(visitedw=0) visitedw=1; coutwnextarc) w=z-adjvex; if(visitedw=0) visitedw=1; coutwnextarc) w=r-adjvex; if(visitedw=0) visitedw=1; coutuwendl; EnQueue(Q,w); int main() int i; ALGraph G; CreateDG(G); int x; coutx; cout邻接表为:endl; for(int j=1;j=x;j+) coutG.verticesj.data ; ArcNode *p; p=(ArcNode*)malloc(sizeof(ArcNode); if(!p) exit(-1); p=G.verticesj.firstarc; while(p) coutadjvexnextarc; coutendl; cout请输入第一个要访问的结点序号:n; for( i=0;i30;i+) visitedi=0; cout广度搜索:endl; BFS(G,n); for( i=0;i30;i+) visitedi=0; coutendl; cout边集:endl; BFSB(G,n); for( i=0;i30;i+) visitedi=0; cout深度搜索:endl; DFS(G,n); for( i=0;i30;i+) visitedi=0; coutendl; cout边集:endl; DFSB(G,n); /system(pause); return 0;3.对学生选课工程图进行拓扑排序.#include#include#define MAX_VEXTEX_NUM 20#define M 20#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef int ElemType;typedef struct ArcNodeint adjvex;struct ArcNode *nextarc;ArcNode;typedef struct VNodeint data;ArcNode *firstarc;VNode,AdjListMAX_VEXTEX_NUM;typedef structAdjList vertices;int vexnum, arcnum;ALGraph;typedef struct /构件栈ElemType *base;ElemType *top;int stacksize;SqStack;void InitStack(SqStack *); /函数声明int Pop(SqStack *, ElemType *);void Push(SqStack *,ElemType );int StackEmpty(SqStack *);void CreatGraph(ALGraph *);void FindInDegree(ALGraph , int * );void TopologicalSort(ALGraph );void InitStack(SqStack *S)/初始化栈S-base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType);if(!S-base)printf(memory allocation failed, goodbye);exit(1);S-top=S-base;S-stacksize=STACK_INIT_SIZE;int Pop(SqStack *S,ElemType *e)/出栈操作if(S-top=S-base)return ERROR;*e=*-S-top;/printf(%dn,e);/ return e;return 0;void Push(SqStack *S,ElemType e)/进栈操作if(S-top-S-base=S-stacksize)S-base = (ElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT)*sizeof(ElemType);if(!S-base)printf(memory allocation failed, goodbye);exit(1);S-top = S-base+S-stacksize;S-stacksize+=STACKINCREMENT;*S-top+=e;int StackEmpty(SqStack *S)/判断栈是否为空if(S-top=S-base)return OK;elsereturn ERROR;void CreatGraph(ALGraph *G)/构件图int m, n, i;ArcNode *p;printf(请输入顶点数和边数:);scanf(%d%d,&G-vexnum,&G-arcnum);for (i = 1; i vexnum; i+)G-verticesi.data = i;G-verticesi.firstarc = NULL;for (i = 1; i arcnum; i+) /输入存在边的点集合printf(n请输入存在边的两个顶点的序号:);scanf(%d%d,&n,&m);while (n G-vexnum | m G-vexnum)printf(输入的顶点序号不正确 请重新输入:);scanf(%d%d,&n,&m);p = (ArcNode*)malloc(sizeof(ArcNode);if (p = NULL)printf(memory allocation failed,goodbey);exit(1);p-adjvex = m;p-nextarc = G-verticesn.firstarc;G-verticesn.firstarc = p;printf(建立的邻接表为:n); /输出建立好的邻接表for(i = 1; i vexnum; i+)printf(%d,G-verticesi.data);for(p = G-verticesi.firstarc; p; p = p-nextarc)printf(%3d,p-adjvex);printf(n);void FindInDegree(ALGraph G, int indegree)/求入度操作int i;for (i = 1; i = G.vexnum; i+)indegreei = 0;for (i = 1; i adjvex+;G.verticesi.firstarc = G.verticesi.firstarc-nextarc;void TopologicalSort(ALGraph G) /进行拓扑排序int indegreeM;int i, k, n;int count = 0;ArcNode *p;SqStack S;FindInDegree(G, indegree);Init
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中华传统文化(浙江金融职业学院)知到智慧树答案
- 中外建筑史知到智慧树答案
- 中外美术32讲知到智慧树答案
- 铁路工程施工组织设计及概算考试试题及答案
- 2025年度道路桥梁工程单项劳务分包合同示范
- 2025房产及院落租赁权附带使用权买卖合同
- 2025年软件开发工具集居间服务合同
- 2025版移动应用开发与推广服务合同
- 2025版国有企业内部员工绩效评估外包协议
- 2025版收养协议书家庭伦理与社会责任
- 公司政治监督工作方案
- 医院培训课件:《中医病历书写基本规范及要点》
- 大中型企业安全生产标准化管理体系要求解读2025
- 2024届高三特尖班及尖子班语文教学经验交流与反思
- ISO9001内审检查表格
- 包装印刷行业安全生产培训
- 《非物质文化遗产》课件
- 互联网加护理服务护理管理
- 《护理纠纷及防范》课件
- 《CT检查技术》课件
- 2024版标准性二手车贷款合同模板(含车况鉴定)3篇
评论
0/150
提交评论