图的基本操作

 1 1.邻接矩阵存图
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 #define maxn 32767
 5 #define maxv 100
 6 #define OK 1
 7 //邻接矩阵的定义
 8 typedef struct
 9 {
10     char vexs[maxv];
11     int arcs[maxv][maxv];
12     int vexnum;
13     int arcnum;
14 }amgraph;
15 //确定顶点表的位置
16 int Loated(amgraph G,char u)
17 {
18     for(int i=0;i<G.vexnum;i++)
19     {
20         if(u==G.vexs[i])
21         {
22             return i;
23         }
24 
25     }
26     return -1;
27 }
28 //创建邻接矩阵
29 int Createudn(amgraph &G)
30 {
31     char s,e;
32     int w;
33     cout<<"请输入点数和边数"<<endl;
34     cin>>G.vexnum>>G.arcnum;
35      cout<<"请输入每一个点"<<endl;
36     for(int i=0;i<G.vexnum;i++)
37     {
38         cin>>G.vexs[i];
39     }
40     for(int i=0;i<G.vexnum;i++)
41     {
42         for(int j=0;j<G.vexnum;j++)
43         {
44             G.arcs[i][j]=maxn;
45         }
46     }
47     cout<<"请输入每一条边"<<endl;
48     for(int i=0;i<G.arcnum;i++)
49     {
50         cin>>s>>e>>w;
51         int a=Loated(G,s);
52         int b=Loated(G,e);
53         G.arcs[a][b]=G.arcs[b][a]=w;
54     }
55     return OK;
56 }
57 int main()
58 {
59     amgraph G;
60     cout<<"创建邻接矩阵"<<endl;
61     Createudn(G);
62     cout<<"邻接矩阵创建完成"<<endl;
63 }

 

 

 

 1 2.邻接表存图
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 #define maxv 100
 5 #define OK 1
 6 //边节点
 7 typedef struct Arcnode
 8 {
 9     int adjvex;//上一条边
10     struct Arcnode *nextarc;//下一条边
11     int info;//权值
12 }Arcnode;
13 //顶点
14 typedef struct Vnode
15 {
16     char data;//名称
17     Arcnode *fiestarc;//第一个节点
18 }Vnode,Adjlist[maxv];
19 //定义邻接表
20 typedef struct
21 {
22     Adjlist vertices;//表头
23     int vexnum;//最大点数
24     int arcnum;//最大边数
25 }Algragh;
26 //确定表头位置
27 int Located(Algragh G,char u)
28 {
29     for(int i=0;i<G.vexnum;i++)
30     {
31         if(u==G.vertices[i].data)
32             return i;
33     }
34     return -1;
35 }
36 int Createuec(Algragh G)
37 {
38     char s,e;
39     int w;
40     cout<<"请输入最大点数和最大边数"<<endl;
41     cin>>G.vexnum>>G.arcnum;
42     cout<<"请输入每个点"<<endl;
43     for(int i=0;i<G.vexnum;i++)
44     {
45         cin>>G.vertices[i].data;
46         G.vertices[i].fiestarc=NULL;
47     }
48     cout<<"请输入每条边"<<endl;
49     for(int i=0;i<G.arcnum;i++)
50     {
51         cin>>s>>e>>w;
52         int a=Located(G,s);
53         int b=Located(G,e);
54         Arcnode *p1=new Arcnode;
55         p1->adjvex=b;
56         p1->info=w;
57         p1->nextarc=G.vertices[a].fiestarc;
58         G.vertices[a].fiestarc=p1;
59         Arcnode *p2=new Arcnode;
60         p2->adjvex=a;
61         p2->info=w;
62         p2->nextarc=G.vertices[a].fiestarc;
63         G.vertices[a].fiestarc=p2;
64     }
65     return OK;
66 }
67 int main()
68 {
69     Algragh G;
70     cout<<"创建邻接表"<<endl;
71     Createuec(G);
72     cout<<"邻接表创建完成"<<endl;
73 }

 

 

 

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define maxn 32767
  4 #define maxv 100
  5 #define OK 1
  6 #define ERROR -1 
  7 bool visited[1000];
  8 //邻接矩阵的定义
  9 typedef struct
 10 {
 11     char vexs[maxv];//存储顶点 
 12     int arcs[maxv][maxv];//存储边 
 13     int vexnum;//最大顶点数 
 14     int arcnum;//最大边数 
 15 }amgraph;
 16 //确定顶点表的位置
 17 int Loated(amgraph G,char u)
 18 {
 19     for(int i=0;i<G.vexnum;i++)
 20     {
 21         if(u==G.vexs[i])
 22         {
 23             return i;
 24         }
 25 
 26     }
 27     return -1;
 28 }
 29 //创建邻接矩阵
 30 int Createudn(amgraph &G)
 31 {
 32     char s,e;
 33     int w;
 34     cout<<"请输入点数和边数"<<endl;
 35     cin>>G.vexnum>>G.arcnum;
 36      cout<<"请输入每一个点"<<endl;
 37     for(int i=0;i<G.vexnum;i++)
 38     {
 39         cin>>G.vexs[i];
 40     }
 41     for(int i=0;i<G.vexnum;i++)
 42     {
 43         for(int j=0;j<G.vexnum;j++)
 44         {
 45             G.arcs[i][j]=maxn;
 46         }
 47     }
 48     cout<<"请输入每一条边"<<endl;
 49     for(int i=0;i<G.arcnum;i++)
 50     {
 51         cin>>s>>e>>w;
 52         int a=Loated(G,s);
 53         int b=Loated(G,e);
 54         G.arcs[a][b]=G.arcs[b][a]=w;
 55     }
 56     return OK;
 57 }
 58 //深度优先搜索遍历
 59 void dfs(amgraph G,int v)
 60 {
 61     cout<<G.vexs[v]<<endl;
 62     visited[v]=true;
 63     for(int w=0;w<G.vexnum;w++)
 64     {
 65         if(G.arcs[v][w]!=0&&(!visited[w]))
 66             dfs(G,w);
 67     }
 68 }
 69 //队列
 70 typedef struct
 71 {
 72     int *base;
 73     int front;
 74     int rear;
 75  }Squeue;
 76 //新建空队列
 77 int InitQueue(Squeue &Q)
 78 {
 79     Q.base=new int[maxv];
 80     if(!Q.base)
 81     exit(OVERFLOW);
 82     Q.front=Q.rear=0;
 83     return OK;
 84  } 
 85  //入队
 86  int Enqueue(Squeue &Q,int e)
 87  {
 88      if((Q.rear+1)%maxv==Q.front)
 89      return ERROR;
 90      Q.base[Q.rear]=e;
 91      Q.rear=(Q.rear+1)%maxv;
 92      return OK;
 93   } 
 94   //出队
 95   int Dequeue(Squeue &Q,int &e)
 96   {
 97       if(Q.front==Q.rear)
 98       return ERROR;
 99       e=Q.base[Q.front];
100       Q.front=(Q.front+1)%maxv;
101       return OK;
102    } 
103    //是否队空
104    int Queueempty(Squeue &Q)
105    {
106        if(Q.rear==Q.front)
107        return 1;
108        else
109        return 0;
110     } 
111 int First(amgraph G,int u)
112 {
113     for(int i=0;i<G.vexnum;i++)
114     {
115         if(G.arcs[u][i]!=32767)
116         return i;
117     }
118     return -1;
119 }
120 int Next(amgraph G,int u,int w)
121 {
122     for(int i=w+1;i<G.vexnum;i++)
123     {
124         if(G.arcs[u][i]!=32767)
125         return i;
126     }
127     return -1;
128 }
129 //bfs
130 void bfs(amgraph G,int v)
131 {
132     int u;
133     cout<<G.vexs[v]<<endl;
134     visited[v]=true;
135     Squeue Q;
136     InitQueue(Q);
137     Enqueue(Q,v);
138     while(!Queueempty(Q))
139     {
140         Dequeue(Q,u);
141         for(int w=First(G,u);w>=0;w=Next(G,u,w))
142         {
143             if(!visited[w])
144             {
145                 cout<<G.vexs[w]<<endl;
146                 visited[w]=true;
147                 Enqueue(Q,w);
148             }
149         }
150     }
151 }
152 int main()
153 {
154     amgraph G;
155     int a;
156     cout<<"1.创建邻接矩阵"<<endl;
157     cout<<"2.广度优先遍历邻接矩阵"<<endl;
158     cout<<"3.深度优先遍历邻接矩阵"<<endl;
159     cout<<"请选择您的操作(按0结束操作)"<<endl;
160     while(cin>>a,a)
161     {
162          switch(a)
163     {
164         case 1:cout<<"创建邻接矩阵"<<endl;
165              Createudn(G);
166              cout<<"邻接矩阵创建完成"<<endl;
167              break;
168         case 2:cout<<"广度优先遍历邻接矩阵"<<endl;
169             bfs(G,0); 
170             fill(visited,visited+1000,false);
171             break;
172         case 3:cout<<"深度优先遍历邻接矩阵"<<endl;
173             dfs(G,0);
174             fill(visited,visited+1000,false);
175             break;
176     }
177     cout<<"1.创建邻接矩阵"<<endl;
178     cout<<"2.广度优先遍历邻接矩阵"<<endl;
179     cout<<"3.深度优先遍历邻接矩阵"<<endl;
180     cout<<"请选择您的操作(按0结束操作)"<<endl;
181     }
182 }

 

 

 

posted @ 2022-05-16 21:34  格蕾  阅读(56)  评论(0)    收藏  举报