标签:
1.定义:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <queue> using namespace std; #define MAX 0x1f1f1f1f #define N 200 bool visit[N]; typedef char VerterType[20]; typedef struct tu { VerterType vexs[N];//顶点数组 int arc[N][N];//邻接矩阵数组 int numpoint,numedge; //图的顶点数和边数 int kind;//图的类型 } graph; //1、确定顶点的位置 int locates(graph &head,VerterType a) { int i,j; for(i=0; i<head.numpoint; i++) { if(strcmp(head.vexs[i],a)==0) { j=i; break; } } return j; } //2、创建图 void graphcreate(graph &head) { int i,j,k; printf("输入顶点数和边数,图的类型:1.无向图,2.无向网,3.有向图,4.有向网\n"); cin>>head.numpoint>>head.numedge>>head.kind; printf("输入定点数组:\n"); for(i=0; i<head.numpoint; i++) { cin>>head.vexs[i]; } for(i=0; i<head.numpoint; i++) { for(j=0; j<head.numpoint; j++) { head.arc[i][j]=MAX; } } VerterType a,b; int quan; if(head.kind%2==1) printf("请输入弧尾、弧头:\n"); else printf("请输入弧尾、弧头和权值:\n"); if(head.kind==1) { for(i=0; i<head.numedge; i++) { cin>>a>>b; int s1=locates(head,a),s2=locates(head,b); head.arc[s1][s2]=1; head.arc[s2][s1]=1; } } else if(head.kind==2) { for(i=0; i<head.numedge; i++) { cin>>a>>b>>quan; int s1=locates(head,a),s2=locates(head,b); head.arc[s1][s2]=quan; head.arc[s2][s1]=quan; } } else if(head.kind==3) { for(i=0; i<head.numedge; i++) { cin>>a>>b; int s1=locates(head,a),s2=locates(head,b); head.arc[s1][s2]=1; } } else { for(i=0; i<head.numedge; i++) { cin>>a>>b>>quan; int s1=locates(head,a),s2=locates(head,b); head.arc[s1][s2]=quan; } } } //3.遍历整个邻接矩阵 void printGraph(graph &g) { int i, j; for(i = 0; i < g.numpoint; i++) { for(j = 0; j < g.numpoint; j++) { printf("%d", g.arc[i][j]); if(j!=g.numpoint-1) printf(" "); else printf("\n"); } } } //4、两个顶点是否存在边。 void find(VerterType a,VerterType b,graph &p) { int i,s1,s2; s1=locates(p,a); s2=locates(p,b); if(p.arc[s1][s2]<MAX&&p.arc[s1][s2]>0) { if(p.kind%2==1)cout<<"yes"<<endl; else { cout<<"yes"<<endl; cout<<a<<"--"<<b<<"的度为:"<<p.arc[s1][s2]<<endl; } } else cout<<"no"<<endl; } //5.查找一个定点的度。(有向图输出入度加出度之和) void findextent(graph &p,VerterType a) { int n,i,j,k,sum=0; if(p.kind<=2) { k=locates(p,a); for(i=0; i<p.numpoint; i++) { if(p.arc[k][i]!=MAX&&p.arc[k][i]!=0) sum++; } cout<<"度为:"<<sum<<endl; } else { k=locates(p,a); for(i=0; i<p.numpoint; i++) { if(p.arc[k][i]!=MAX&&p.arc[k][i]!=0) sum++; if(p.arc[i][k]!=MAX&&p.arc[i][k]!=0) sum++; } cout<<"度为:"<<sum<<endl; } } int maxx; void DFS(graph &p,int i) { maxx++; int j; visit[i]=true; cout<<p.vexs[i]; if(maxx!=p.numpoint) cout<<" "; else cout<<endl; for(j=0; j<p.numpoint; j++) { if(p.arc[i][j]>0&&p.arc[i][j]<MAX&&visit[j]==false) { DFS(p,j); } } } void DFSTraverse(graph g) { int i; memset(visit,false,sizeof(visit)); for(i = 0; i < g.numpoint; i++) { if(!visit[i]) { DFS(g,i); } } } void BFS(graph &p) { int i,j; int sum=0; queue<int>q; memset(visit,false,sizeof(visit)); for(i=0; i<p.numpoint; i++) { if(!visit[i]) { visit[i]=true; cout<<p.vexs[i]; sum++; if(sum!=p.numpoint) cout<<" "; else cout<<endl; q.push(i); while(!q.empty()) { int m; m=q.front(); q.pop(); for(j=0; j<p.numpoint; j++) { if(p.arc[m][j]>0&&p.arc[m][j]<MAX&&!visit[j]) { visit[j]=true; cout<<p.vexs[j]; sum++; if(sum!=p.numpoint) cout<<" "; else cout<<endl; q.push(j); } } } } } } int main() { graph head; VerterType a,b; int n; cin>>n; while(n--) { maxx=0; graphcreate(head); printf("输出链接矩阵:\n"); printGraph(head); printf("请输入两个顶点:\n"); cin>>a>>b; printf("判断两顶点之间是否存在边:\n"); find(a,b,head); printf("请输入一个顶点:\n"); cin>>a; printf("它的度为:\n"); findextent(head,a); printf("深度优先搜索遍历:\n"); DFSTraverse(head); printf("广度优先搜索遍历:\n"); BFS(head); } return 0; }
详细解读代码便可分析出相应的知识点了。
标签:
原文地址:http://www.cnblogs.com/famousli/p/4240774.html