邻接矩阵、邻接表存储结构 遍历算法和图的广度优先搜索遍历
一、 实验内容
1)掌握图的邻接矩阵、邻接表存储结构表示及其创建算法
2)掌握图的深度优先搜索遍历算法和图的广度优先搜索遍历算法;
4)按照实验题目要求独立正确地完成实验内容(提交程序清单及相关实验数据与运行结果);
二、 源程序
1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 #define OK 1 6 #define OVERFLOW -1 7 #define ERROR 0 8 #define MAXSIZE 100 9 int typedef Status; 10 //邻接矩阵存储 11 #define MaxInt 32767 12 #define MVNum 100 13 typedef char VerTexType; 14 typedef int ArcType; 15 typedef struct//邻接矩阵存储定义 16 { 17 VerTexType vexs[MVNum];//char 18 ArcType arcs[MVNum][MVNum]; 19 int vexnum, arcnum; 20 }AMGraph; 21 typedef struct 22 { 23 int data[MAXSIZE]; 24 int front; /* 头指针 */ 25 int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */ 26 }Queue; 27 Status DeQueue(Queue &Q, int &e){ 28 if (Q.front == Q.rear)return ERROR; 29 e = Q.data[Q.front]; 30 Q.front = (Q.front + 1) % MAXSIZE; 31 return OK; 32 } 33 int LocateVex(const AMGraph &g, const VerTexType &v) 34 { 35 int i = 0; 36 for (i = 0; i < g.vexnum; i++) 37 { 38 if (g.vexs[i] == v) 39 break; 40 } if (i > g.vexnum) 41 { 42 fprintf(stderr, "no such vertex.\n"); 43 return -1; 44 } 45 return i; 46 } 47 Status InitQueue(Queue Q) 48 { 49 Q.front = 0; 50 Q.rear = 0; 51 return OK; 52 } 53 Status QueueEmpty(Queue Q) 54 { 55 if (Q.front == Q.rear) /* 队列空的标志 */ 56 return true; 57 else 58 return false; 59 } 60 Status EnQueue(Queue Q, int e) 61 { 62 if ((Q.rear + 1) % MAXSIZE == Q.front) 63 return ERROR; Q.data[Q.rear] = e; 64 Q.rear = (Q.rear + 1) % MAXSIZE; 65 return OK; 66 } 67 68 int FirstAdjVex(AMGraph &G, int &v) 69 { 70 int i, j = 0, k; 71 k = LocateVex(G, v);//k为顶点v在图G中的序号 72 for (i = 0; i<G.vexnum; ++i) 73 { 74 if (G.arcs[k][i]!= j) 75 { 76 return i; 77 } 78 } 79 return -1; 80 } 81 int NextAdjVex(AMGraph &G, int &v, int &w) 82 { 83 int i, j = 0, k1, k2; 84 k1 = LocateVex(G, v);//k1为顶点v在图G中的序号 85 k2 = LocateVex(G, w);//k2为顶点w在图G中的序号 86 87 for (i = k2 + 1; i<G.vexnum; ++i) 88 { 89 if (G.arcs[k1][i] != j) 90 { 91 return i; 92 } 93 } 94 return -1; 95 } 96 97 Status CreateUDN(AMGraph &G)//邻接矩阵创建无向图 98 { 99 int i = 0, j = 0,k=0,v1=0,v2=0,w=0; 100 cout << "请输入总顶点数,总边数" << endl; 101 cin >> G.vexnum >> G.arcnum;//当前点数,边数 102 for ( i = 0; i < G.vexnum; ++i){ 103 cout << "输入点信息" << endl; 104 cin >> G.vexs[i];//名字 105 } 106 for (i = 0; i < G.vexnum; ++i) 107 { 108 for (j = 0; j < G.vexnum; ++j){ 109 G.arcs[i][j] = MaxInt; 110 } 111 } 112 for (k = 0; k < G.arcnum; ++k){ 113 cin >> v1 >> v2 >> w; 114 i = LocateVex(G, v1); j = LocateVex(G, v2); 115 G.arcs[i][j] = w; 116 G.arcs[j][i] = G.arcs[i][j]; 117 } 118 return OK; 119 } 120 121 bool visited[MVNum]; 122 void DFS_AM(AMGraph G, int v)//邻接矩阵深度优先搜索遍历 123 {//从第v个顶点出发深度优先搜索遍历 124 int w = 0; 125 cout << v << endl; visited[v] = true; 126 for (w = 0; w < G.vexnum; w++){ 127 if ((G.arcs[v][w] != 0) && (!visited[w])) 128 DFS_AM(G, w); 129 } 130 } 131 132 133 //广度优先 134 void BFS(AMGraph &G, int v){ 135 cout << v; visited[v] = true; 136 Queue Q; 137 Q.front = 0; 138 int u, w; 139 InitQueue(Q); 140 EnQueue(Q, v); 141 while (!QueueEmpty(Q)){ 142 DeQueue(Q, u); 143 for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G,u,w)){ 144 if (!visited[w]){ 145 cout << w; 146 visited[w] = true; 147 EnQueue(Q, w); 148 } 149 } 150 151 } 152 153 } 154 155 156 157 158 159 //邻接表 160 #define MVNum 100 161 //邻接表定义 162 typedef struct ArcNode{ 163 int adjvex; 164 struct ArcNode *nextarc; 165 int info; 166 }ArcNode; 167 typedef struct VNode{ 168 VerTexType data; 169 ArcNode *firstarc; 170 }VNode,AdjList[MVNum]; 171 typedef struct{ 172 AdjList vertices; 173 int vexnum, arcnum; 174 }ALGraph; 175 int LocateVex(const ALGraph &g, const int &v) 176 { 177 int i = 0; 178 for (i = 0; i < g.vexnum; i++) 179 { 180 if (g.vertices[i].data == v) 181 break; 182 } if (i > g.vexnum) 183 { 184 fprintf(stderr, "no such vertex.\n"); 185 return -1; 186 } 187 return i; 188 } 189 190 int FirstAdjVex(ALGraph &G, int &v) 191 { 192 ArcNode * p; 193 int v1; 194 v1 = LocateVex(G, v);//v1为顶点v在图G中的序号 195 p = G.vertices[v1].firstarc; 196 if (p) 197 { 198 return p->adjvex; 199 } 200 else 201 { 202 return -1; 203 } 204 } 205 206 207 //邻接表创建无向图 208 Status CreateUDG(ALGraph &G){ 209 int i = 0, j = 0, k = 0,v1,v2; 210 cout << "输入总顶点数,总边数" << endl; 211 for (i = 0; i < G.vexnum; ++i){ 212 cout << "输入顶点值" << endl; 213 cin >> G.vertices[i].data; 214 G.vertices[i].firstarc = NULL; 215 216 } 217 for (k = 0; k < G.arcnum; ++k){ 218 cout << "输入一条边依附的两个顶点" << endl; 219 cin >> v1 >> v2; 220 i = LocateVex(G, v1); 221 j = LocateVex(G, v2); 222 ArcNode *p1,*p2; 223 p1 = new ArcNode; 224 p2 = new ArcNode; 225 p1->adjvex = j; 226 p1->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc = p1; 227 p2->adjvex = i; 228 p2->nextarc = G.vertices[j].firstarc; G.vertices[j].firstarc = p2; 229 230 } 231 return OK; 232 } 233 void DFS_AL(ALGraph G, int v){ 234 cout << v; visited[v] = true; 235 ArcNode *p; 236 int w; 237 p = G.vertices[v].firstarc; 238 while (p != NULL){ 239 w = p->adjvex; 240 if (!visited[w])DFS_AL(G, w); 241 p = p->nextarc; 242 } 243 } 244 int main(){ 245 AMGraph G1; 246 CreateUDN(G1); 247 DFS_AM(G1, 1); 248 ALGraph G2; 249 CreateUDG(G2); 250 DFS_AL(G2, 1); 251 }