数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx
- 文档编号:5302714
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:20
- 大小:136.75KB
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx
《数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx》由会员分享,可在线阅读,更多相关《数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx(20页珍藏版)》请在冰豆网上搜索。
数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历
1.给定无向图,请用邻接矩阵表示法表示该图
#include
#include
usingnamespacestd;
#defineMAX20
typedefintAdj[MAX][MAX];
typedefstruct{
stringvexs[MAX];//顶点表
Adjarcs;//邻接矩阵
intvexnum,arcnum;//图的顶点和弧数
}MGraph;
intLocateVex(MGraph&G,stringu);
intCreateUDN(MGraph&G){
inti,k,j;stringv1,v2;
cout<<"请输入顶点数、弧数:
";
cin>>G.vexnum>>G.arcnum;
cout<<"输入顶点:
";
for(i=0;i cin>>G.vexs[i];//构造顶点数 } for(i=0;i for(j=0;j G.arcs[i][j]=0; } } for(k=0;k cout<<"输入第"< "; cin>>v1>>v2; i=LocateVex(G,v1);j=LocateVex(G,v2); G.arcs[i][j]=1; G.arcs[j][i]=1;//置 } return0; } intLocateVex(MGraph&G,stringu){//确定u在G中序号 inti; for(i=0;i if(u==G.vexs[i]) returni; } if(i==G.vexnum){ cout<<"Erroru! "< exit (1); } return0; } voidShowG(MGraph&G){ inti,j; for(i=0;i cout< } cout< for(i=0;i for(j=0;j cout< } cout< } } main(){ MGraphA; inta; a=CreateUDN(A); ShowG(A); } 2.分别使用邻接矩阵表示法和邻接表表示法,用深度优先搜索法遍历该图。 #include #include #include #include usingnamespacestd; intvisited[30]; #defineMAX_VERTEX_NUM30 #defineOK1 //typedefintVertexType; typedefintInfoType; typedefstructArcNode//弧 { intadjvex; structArcNode*nextarc; }ArcNode; typedefstructVNode//表头 { intdata; ArcNode*firstarc; }VNode,AdjList[MAX_VERTEX_NUM]; typedefstruct//图 { AdjListvertices; intvexnum,arcnum; intkind; }ALGraph; voidCreateDG(ALGraph&G) { intk,i,v1; cout< "; cin>>G.vexnum; cout<<"请输入弧的个数: "; cin>>G.arcnum; for(i=1;i<=G.vexnum;i++)//初使化表头 { G.vertices[i].data=i; G.vertices[i].firstarc=NULL; } for(k=1;k<=G.vexnum;k++)//输入边 { intv2; cout<<"请输入与结点"< "; cin>>v2; cout<<"请输入与第"< "; cin>>v1; ArcNode*p; p=(ArcNode*)malloc(sizeof(ArcNode)); if(! p)exit(-1); p->adjvex=v1; p->nextarc=NULL; G.vertices[k].firstarc=p; for(inti=1;i { intm; cout<<"请输入与第"< "; cin>>m; 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); } } voidDFS(ALGraphG,intv)//深度搜索 { visited[v]=1; cout< ArcNode*x; x=(ArcNode*)malloc(sizeof(ArcNode)); if(! x)exit(-1); x=G.vertices[v].firstarc; intw; for(;x;x=x->nextarc) {w=x->adjvex; if(visited[w]==0) DFS(G,w); } } voidDFSB(ALGraphG,intv)//深度搜索的边集 { visited[v]=1; ArcNode*y; y=(ArcNode*)malloc(sizeof(ArcNode)); if(! y)exit(-1); y=G.vertices[v].firstarc; intu=G.vertices[v].data; intw; for(;y;y=y->nextarc) {w=y->adjvex; if(visited[w]==0) { cout<"< DFSB(G,w); } } } typedefstructQNode { intdata; QNode*next; }QNode,*QueuePtr; typedefstruct { QueuePtrfront; QueuePtrrear; }LinkQueue; voidInitQueue(LinkQueue&Q)//建立一个空队列 { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(! Q.front)exit(-1); Q.front->next=NULL; } voidEnQueue(LinkQueue&Q,inte)//进队 { 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); } intDeQueue(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); returne; } intQueueEmpty(LinkQueueQ)//判断队列是否为空 { if(Q.front==Q.rear) return1; return0; } voidBFS(ALGraphG,intv)//广度搜索 { intu; LinkQueueQ; InitQueue(Q); if(visited[v]==0) { visited[v]=1; cout< EnQueue(Q,v); while(QueueEmpty(Q)! =1) { DeQueue(Q,u); ArcNode*z; z=(ArcNode*)malloc(sizeof(ArcNode)); if(! z)exit(-1); z=G.vertices[u].firstarc; /* for(intw=z->adjvex;w>=0;w=z->nextarc->adjvex) { if(visited[w]==0) { visited[w]=1; cout< EnQueue(Q,w); } }*/ intw; for(;z;z=z->nextarc) {w=z->adjvex; if(visited[w]==0) { visited[w]=1; cout< EnQueue(Q,w); } } } } } voidBFSB(ALGraphG,intv)//广度搜索的边集 { intu; LinkQueueQ; InitQueue(Q); if(visited[v]==0) { visited[v]=1; EnQueue(Q,v); while(QueueEmpty(Q)! =1) { DeQueue(Q,u); ArcNode*r; r=(ArcNode*)malloc(sizeof(ArcNode)); if(! r)exit(-1); r=G.vertices[u].firstarc; intw; for(;r! =NULL;r=r->nextarc) {w=r->adjvex; if(visited[w]==0) { visited[w]=1; cout<"< EnQueue(Q,w); } } } } } intmain() { inti; ALGraphG; CreateDG(G); intx; cout<<"请输入结点数: "; cin>>x; cout<<"邻接表为: "< for(intj=1;j<=x;j++) { cout< ArcNode*p; p=(ArcNode*)malloc(sizeof(ArcNode)); if(! p)exit(-1); p=G.vertices[j].firstarc; while(p) { cout< p=p->nextarc; } cout< } cout<<"请输入第一个要访问的结点序号: "< intn; cin>>n; for(i=0;i<30;i++) visited[i]=0; cout<<"广度搜索: "< BFS(G,n); for(i=0;i<30;i++) visited[i]=0; cout< cout<<"边集: "< BFSB(G,n); for(i=0;i<30;i++) visited[i]=0; cout<<"深度搜索: "< DFS(G,n); for(i=0;i<30;i++) visited[i]=0; cout< cout<<"边集: "< DFSB(G,n); //system("pause"); return0; } 3.对学生选课工程图进行拓扑排序. #include #include #defineMAX_VEXTEX_NUM20 #defineM20 #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 #defineOK1 #defineERROR0 typedefintElemType; typedefstructArcNode { intadjvex; structArcNode*nextarc; }ArcNode; typedefstructVNode { intdata; ArcNode*firstarc; }VNode,AdjList[MAX_VEXTEX_NUM]; typedefstruct { AdjListvertices; intvexnum,arcnum; }ALGraph; typedefstruct//构件栈 { ElemType*base; ElemType*top; intstacksize; }SqStack; voidInitStack(SqStack*);//函数声明 intPop(SqStack*,ElemType*); voidPush(SqStack*,ElemType); intStackEmpty(SqStack*); voidCreatGraph(ALGraph*); voidFindInDegree(ALGraph,int*); voidTopologicalSort(ALGraph); voidInitStack(SqStack*S)//初始化栈 { S->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(! S->base) { printf("memoryallocationfailed,goodbye"); exit (1); } S->top=S->base; S->stacksize=STACK_INIT_SIZE; } intPop(SqStack*S,ElemType*e)//出栈操作 { if(S->top==S->base) {returnERROR;} *e=*--S->top; //printf("%d\n",e); //returne; return0; } voidPush(SqStack*S,ElemTypee)//进栈操作 {if(S->top-S->base>=S->stacksize) { S->base=(ElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); if(! S->base) { printf("memoryallocationfailed,goodbye"); exit (1); } S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; }*S->top++=e; } intStackEmpty(SqStack*S)//判断栈是否为空 { if(S->top==S->base) returnOK; else returnERROR;} voidCreatGraph(ALGraph*G)//构件图 {intm,n,i; ArcNode*p; printf("请输入顶点数和边数: "); scanf("%d%d",&G->vexnum,&G->arcnum); for(i=1;i<=G->vexnum;i++) {G->vertices[i].data=i; G->vertices[i].firstarc=NULL; } for(i=1;i<=G->arcnum;i++)//输入存在边的点集合 { printf("\n请输入存在边的两个顶点的序号: "); scanf("%d%d",&n,&m); while(n<0||n>G->vexnum||m<0||m>G->vexnum) {printf("输入的顶点序号不正确请重新输入: "); scanf("%d%d",&n,&m); } p=(ArcNode*)malloc(sizeof(ArcNode)); if(p==NULL) {printf("memoryallocationfailed,goodbey"); exit (1); } p->adjvex=m; p->nextarc=G->vertices[n].firstarc; G->vertices[n].firstarc=p; } printf("建立的邻接表为: \n");//输出建立好的邻接表 for(i=1;i<=G->vexnum;i++) { printf("%d",G->vertices[i].data); for(p=G->vertices[i].firstarc;p;p=p->nextarc) printf("%3d",p->adjvex); printf("\n"); }} voidFindInDegree(ALGraphG,intindegree[])//求入度操作 { inti; for(i=1;i<=G.vexnum;i++) { indegree[i]=0; } for(i=1;i<=G.vexnum;i++) {while(G.vertices[i].firstarc) {indegree[G.vertices[i].firstarc->adjvex]++; G.vertices[i].firstarc=G.vertices[i].firstarc->nextarc; } } } voidTopologicalSort(ALGraphG)//进行拓扑排序 { intindegree[M]; inti,k,n; intcount=0; ArcNode*p; SqStackS; FindInDegree(G,indegree); InitStack(&S); for(i=1;i<=G.vexnum;i++) { printf("第%d个点的入度为%d\n",i,indegree[i]); } printf("\n"); for(i=1;i<=G.vexnum;i++) { if(! indegree[i]) Push(&S,i); } printf("进行拓扑排序输出顺序为: ");//输出结果 while(! StackEmpty(&S)) { Pop(&S,&n); printf("%4d",G.vertices[n].data); count++; for(p=G.vertices[n].firstarc;p! =NULL;p=p->nextarc) { k=p->adjvex; if(! (--indegree[k])) { Push(&S,k); } } }printf("\n"); if(count { printf("出现错误\n"); } else { printf("排序成功\n"); } } intmain(void)//主函数 { ALGraphG; CreatGraph(&G); TopologicalSort(G); system("pause"); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 visualc 邻接矩阵 表示 给定 进行 深度 遍历