数据结构图的两种存储结构(邻接矩阵和邻接表)

数据结构中图有多种存储结构,这里我就来说其中较简单的两种,邻接矩阵以及邻接表。

邻接矩阵:用一个n*n的矩阵来表示一张图,矩阵的横纵坐标均表示图的点,例如矩阵第i行第j列数字为1,在无向图中表示点i与点j之间有一条连线,而在有向图中表示在图中有一条由i指向j的边。这个点数字为几,代表有几条边。

邻接表:用链表的形式来表示图,这个表头结点所对应的顶点存在相邻顶点,则把相邻顶点依次存放于表头结点所指向的单向链表中。

下面分别给出了有向图的邻接矩阵以及无向图的邻接表实现方法。

 

邻接矩阵:

 

  1 //数据结构链接矩阵
  2 #include<iostream>
  3 #define MAXN 100
  4 using namespace std;
  5 typedef struct{
  6     char vexs[MAXN];
  7     int map[MAXN][MAXN],visit[MAXN],degree[MAXN];
  8     int vexnum,arcnum;
  9 }AMGraph;
 10 
 11 int LocateVex(AMGraph &G,char e)
 12 {
 13     for(int i=0; i<G.vexnum; ++i)
 14     {
 15         if(G.vexs[i] == e)
 16         {
 17             return i;
 18         }
 19     }
 20 }
 21 
 22 void InitMap(AMGraph &G)//建立邻接矩阵
 23 {
 24     cout<<"请输入点数和边数:";
 25     cin>>G.vexnum>>G.arcnum;
 26     cout<<"请输入各点的名称:"; 
 27     for(int i=0; i<G.vexnum; ++i)
 28     {
 29         cin>>G.vexs[i];
 30         G.visit[i] = 0; //初始化标记为0 
 31         G.degree[i] = 0; //初始化度为 0 
 32     }
 33     
 34     for(int i=0; i<G.vexnum; ++i)
 35         for(int j=0; j<G.vexnum; ++j)
 36             G.map[i][j] = 0;
 37 
 38     char v1,v2;
 39     cout<<"请输入每一条边:"<<endl;
 40     for(int i=0; i<G.arcnum; ++i)
 41     {
 42         cin>>v1>>v2;
 43         int x,y;
 44         x = LocateVex(G,v1);//找到 v1的位置
 45         y = LocateVex(G,v2);//找到 v2的位置
 46         G.degree[x]++; //v1的度++
 47         G.degree[y]++; //v2的度++
 48         G.map[x][y] = 1;
 49     }
 50 }
 51 
 52 void DFS(AMGraph &G,int e)
 53 {
 54     cout<<G.vexs[e]<<' ';
 55     for(int i=0; i<G.vexnum; ++i)
 56     {
 57         if(!G.visit[i] && G.map[e][i])
 58         {
 59             G.visit[i] = 1;
 60             DFS(G,i);
 61         }
 62     }
 63 }
 64 
 65 void BFS(AMGraph &G,int e)
 66 {
 67     for(int i=0; i<G.vexnum; ++i)//初始化标记为0 
 68         G.visit[i] = 0;
 69     G.visit[e] = 1;//起点标记走过 
 70     
 71     int que[MAXN];
 72     int rear=0,front=0,t;
 73     que[rear++] = e;
 74 
 75     while(rear != front)
 76     {
 77         t = que[front++];
 78         front %= MAXN;
 79         cout<<G.vexs[t]<<' ';
 80         for(int i=0; i<G.vexnum; ++i)
 81         {
 82             if(!G.visit[i] && G.map[t][i])
 83             {
 84                 que[rear++] = i;
 85                 rear %= MAXN;
 86                 G.visit[i] = 1;
 87             }
 88         }
 89     }
 90 }
 91 
 92 int main()
 93 {
 94     AMGraph G;
 95     InitMap(G);
 96     cout<<"邻接矩阵为:"<<endl;
 97     for(int i=0; i<G.vexnum; ++i)
 98     {
 99         for(int j=0; j<G.vexnum; ++j)
100         {
101             cout<<G.map[i][j]<<' ';
102         }
103         cout<<endl;
104     }
105     char a;
106     bool flag = false;
107     cout<<"请输入搜索的起点:"<<endl;
108     cin>>a;
109     for(int i=0; i<G.vexnum; ++i)
110     {
111         if(G.vexs[i] == a)
112         {
113             flag = true;
114             G.visit[i] = 1;
115             cout<<"深度优先遍历结果:";
116             DFS(G,i);//深搜
117             cout<<endl;
118             
119             cout<<"广度优先遍历结果:";
120             BFS(G,i);//广搜
121             cout<<endl;
122             cout<<"每个点的度分别为:";
123             for(int j=0; j<G.vexnum; ++j)
124                 cout<<G.degree[j]<<' ';
125             break;
126         }
127     }
128     if(!flag)
129         cout<<"无名字为"<<a<<"的点"<<endl;
130     return 0;
131 }
132  
133 /*
134 0 1
135 0 2
136 0 3
137 1 4
138 2 4
139 2 5
140 3 5
141 4 6
142 5 6
143 */

 

 

 

邻接表:

  1 #include<iostream>
  2 #define MAXN 100
  3 using namespace std;
  4 typedef struct ArcNode{
  5     char adjvex;
  6     struct ArcNode *next;
  7 }ArcNode;
  8 typedef struct VNode{
  9     char data;
 10     ArcNode *frist;
 11 }VNode,AdjList[MAXN];
 12 typedef struct{
 13     AdjList vertices;
 14     int vexnum,arcnum;
 15     int visit[MAXN],degree[MAXN];
 16 }ALGraph;
 17 
 18 int LocateVex(ALGraph &G,char e)
 19 {
 20     for(int i=0; i<G.vexnum; ++i)
 21         if(G.vertices[i].data == e)
 22             return i;
 23 }
 24 
 25 void InitMap(ALGraph &G)
 26 {
 27     cout<<"请输入点数和边数:";
 28     cin>>G.vexnum>>G.arcnum;
 29     cout<<"请输入各点的名称:";
 30     for(int i=0; i<G.vexnum; ++i)
 31     {
 32         cin>>G.vertices[i].data;
 33         G.visit[i] = 0;
 34         G.degree[i] = 0;
 35         G.vertices[i].frist = NULL;
 36     }
 37     
 38     char v1,v2;
 39     cout<<"请输入每一条边:"<<endl;
 40     for(int i=0; i<G.arcnum; ++i)
 41     {
 42         cin>>v1>>v2;
 43         int x,y;
 44         x = LocateVex(G,v1);
 45         y = LocateVex(G,v2);
 46         
 47         G.degree[x]++;
 48         G.degree[y]++;
 49         
 50         ArcNode *p1 = new ArcNode;
 51         p1->adjvex = y;
 52         p1->next = G.vertices[x].frist;
 53         G.vertices[x].frist = p1;
 54         
 55         ArcNode *p2 = new ArcNode;
 56         p2->adjvex = x;
 57         p2->next = G.vertices[y].frist;
 58         G.vertices[y].frist = p2;
 59     }
 60 }
 61 
 62 void DFS(ALGraph &G,int e)
 63 {
 64     cout<<G.vertices[e].data<<' ';
 65     G.visit[e] = 1;
 66     ArcNode *p = new ArcNode;
 67     p = G.vertices[e].frist;
 68     while(p != NULL)
 69     {
 70         if(!G.visit[p->adjvex])
 71             DFS(G,p->adjvex);
 72         p = p->next;
 73     }
 74     delete p;
 75 }
 76 
 77 void BFS(ALGraph &G,int e)
 78 {
 79     for(int i=0; i<G.vexnum; ++i)
 80         G.visit[i] = 0;
 81     G.visit[e] = 1;
 82     
 83     int rear = 0, front = 0,t;
 84     int que[MAXN];
 85     que[rear++] = e;
 86     while(rear != front)
 87     {
 88         t = que[front++];
 89         front %= MAXN;
 90         cout<<G.vertices[t].data<<' ';
 91         ArcNode *p = new ArcNode;
 92         p = G.vertices[t].frist;
 93         while(p != NULL)
 94         {
 95             if(!G.visit[p->adjvex])
 96             {
 97                 G.visit[p->adjvex] = 1;
 98                 que[rear++] = p->adjvex;
 99                 rear %= MAXN;
100             }
101             p = p->next;
102         }
103     }
104 }
105 
106 int main()
107 {
108     ALGraph G;
109     InitMap(G);
110     char a;
111     cout<<"请输入搜索的起点:";
112     cin>>a;
113     for(int i=0; i<G.vexnum; ++i)
114     {
115         if(G.vertices[i].data == a)
116         {
117             cout<<"深度优先遍历结果:";
118             DFS(G,i);
119             cout<<endl;
120             
121             cout<<"广度优先遍历结果:";
122             BFS(G,i);
123             cout<<endl;
124             break;
125         }
126     }
127     cout<<"每个点的度分别为:";
128     for(int i=0; i<G.vexnum; ++i)
129         cout<<G.degree[i]<<' ';
130     cout<<endl;
131     return 0;
132 }
133 
134 /*
135 5 6
136 01234
137 0 1
138 0 3
139 1 2
140 1 4
141 2 3
142 2 4
143 */

 

 

 

  

 

posted @ 2018-06-05 17:37  特务依昂  阅读(5748)  评论(0编辑  收藏  举报