用邻接矩阵和邻接表创建图


  1 #include <iostream>
  2 using namespace std;
  3 
  4 #define INFINITY 65536//无穷大
  5 #define    MAX_VERTEX_NUM 10//最大顶点个数
  6 typedef enum{DG,DN,UDG,UDN}GraphKind;//有向图,有向网,无向图,无向网
  7 struct Graph
  8 {
  9     char vexs[MAX_VERTEX_NUM];//储存顶点
 10     int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//邻接矩阵
 11     int vexnum, arcnum;//顶点数和边(弧)数
 12     GraphKind kind;//图的种类
 13 };
 14 //确定顶点序号
 15 int LocateVex(Graph *G, char ch)
 16 {
 17     for (int i = 0; i < G->vexnum; i++)
 18         if (G->vexs[i] == ch)
 19         {
 20             return i;//返回顶点序号
 21             break;
 22         }
 23     return -1;
 24 }
 25 void CreateUDN(Graph *G)//创建无向网
 26 {
 27     int i, j;
 28     char v1, v2;//记录顶点名称
 29     int w;//记录权值
 30     cout << "请输入顶点个数:" << endl;cin >> G->vexnum;
 31     cout << "请输入边的个数:" << endl;cin >> G->arcnum;
 32     cout << "构造顶点:" << endl;
 33     for (int i = 0; i < G->vexnum; i++)//构造顶点
 34         cin >> G->vexs[i];
 35     for (int i = 0; i < G->vexnum; i++)//初始化邻接矩阵
 36         for (int j = 0; j < G->vexnum; j++)
 37             G->arc[i][j] = 0;
 38     cout << "构造邻接矩阵:" << endl;
 39     for (int k = 0; k < G->arcnum; k++)
 40     {
 41         cin >> v1 >> v2 >> w;
 42         i = LocateVex(G, v1);
 43         j = LocateVex(G, v2);
 44         G->arc[j][i] = G->arc[i][j] = w;
 45     }
 46 }
 47 void CreateGraph(Graph *G)
 48 {
 49     switch (G->kind)
 50     {
 51     //case DG:return CreateDG(G);
 52     //case DN:return CreateDN(G);
 53     //case UDG:return CreateUDG(G);
 54     case UDN:return CreateUDN(G);
 55     }
 56 }
 57 //遍历邻接矩阵
 58 void PrintArc(Graph *G)
 59 {
 60     int i, j;
 61     for (i = 0; i < G->vexnum; i++)
 62     {
 63         for (j = 0; j < G->vexnum; j++)
 64             cout << G->arc[i][j] << " ";
 65         cout << endl;
 66     }
 67 }
 68 //深度优先遍历(邻接矩阵储存)
 69 int visited[MAX_VERTEX_NUM] = { 0 };//初始化元素为0
 70 void DFS_Arc(Graph *G,int v)//递归实现,v为某个顶点
 71 {
 72     if(visited[v]==0)
 73         cout << G->vexs[v] << " ";
 74     visited[v] = 1;
 75     for (int j = 0; j < G->vexnum; j++)
 76     {
 77         if (G->arc[v][j] != 0 && visited[j] == 0)
 78             DFS_Arc(G,j);
 79     }
 80 }
 81 
 82 
 83 //邻接表
 84 struct ENode//边结点
 85 {
 86     int index;//边所指向的点的序号
 87     int info;//记录权值
 88     struct ENode* next;//指向下一条边
 89 };
 90 struct VNode//顶点结点
 91 {
 92     char data;//顶点名称
 93     ENode* first;//指向顶点的第一个邻边
 94 };
 95 struct AGraph//记录图的信息
 96 {
 97     int vexnum, arcnum;//顶点数和边数
 98     VNode p[MAX_VERTEX_NUM];//顶点数组
 99     GraphKind kind;//图的种类
100 };
101 
102 int LocateVex(AGraph *G,char ch)
103 {
104     for (int i = 0; i < G->vexnum; i++)
105         if (ch == G->p[i].data)
106         {
107             return i;
108             break;
109         }
110     return -1;
111 }
112 //创建邻接表
113 void CreateAGraph(AGraph *G)
114 {
115     int i, j, info;
116     char v1, v2;
117     cout << "请输入顶点个数:" << endl; cin >> G->vexnum;
118     cout << "请输入边的个数:" << endl; cin >> G->arcnum;
119     cout << "构造顶点:" << endl;
120     for (int i = 0; i < G->vexnum; i++)//构造顶点数组
121     {
122         cin >> G->p[i].data;
123         G->p[i].first = NULL;
124     }
125     cout << "构造邻接表:" << endl;
126     for (int k = 0; k < G->arcnum; k++)
127     {
128         cin >> v1 >> v2 >> info;
129         i = LocateVex(G, v1);
130         j = LocateVex(G, v2);
131         ENode *E = (ENode*)malloc(sizeof(ENode));
132         E->index = j;
133         E->info = info;
134         E->next = G->p[i].first;//头插法
135         G->p[i].first = E;
136     }
137 }
138 //打印邻接表
139 void PrintAGraph(AGraph *G)
140 {
141     for (int i = 0; i < G->vexnum; i++)
142     {
143         cout << G->p[i].data;
144         if (G->p[i].first != NULL)
145         {
146             ENode *p = G->p[i].first;
147             while (p->next != NULL)
148             {
149                 cout << "->" << p->index;
150                 p = p->next;
151             }
152             cout << "->" << p->index;
153         }
154         cout << endl;
155     }
156 }
157 //深度优先遍历(邻接表储存)
158 int visited1[MAX_VERTEX_NUM] = { 0 };
159 void DFS_List(AGraph *G, int v)
160 {
161     if (visited1[v] == 0)
162         cout << G->p[v].data << " ";
163     visited1[v] = 1;
164     if (G->p[v].first != NULL)
165     {
166         ENode *p = G->p[v].first;
167         DFS_List(G, p->index);
168         while (p->next != NULL)
169         {
170             p = p->next;
171             DFS_List(G, p->index);
172         }
173     }
174 }
175 int main()
176 {
177     Graph *G=(Graph*)malloc(sizeof(Graph));
178     G->kind = UDN;
179     CreateGraph(G);
180     PrintArc(G);
181     cout << endl << "深度优先遍历:" << endl;
182     DFS_Arc(G, 0);
183     cout << endl;
184     cout << "-------------------------------------" << endl;
185     cout << "邻接表:" << endl;
186     AGraph *AG=(AGraph*)malloc(sizeof(AGraph));
187     CreateAGraph(AG);
188     PrintAGraph(AG);
189     cout << "深度优先遍历:" << endl;
190     DFS_List(AG, 0);
191     system("pause");
192     return 0;
193 }

运行结果:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM