前言
数据结构中图的C++语言描述实现模板,有详细的步骤解析及使用示例。
代码仓库
图的邻接矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #include <iostream>
using namespace std;
#define MAX_VERTEX_NUM 5
typedef int VertexType;
typedef int EdgeType;
typedef struct { int vexNum; int edgeNum; VertexType vexs[MAX_VERTEX_NUM]; EdgeType edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; } Graph;
bool g_visited[MAX_VERTEX_NUM];
void use_example(); void createGraph(Graph &graph); void dfsTraverse(Graph graph); void dfs(Graph graph, int i); void bfsTraverse(Graph graph); void bfs(Graph graph, int i);
void use_example() { Graph graph; createGraph(graph);
dfsTraverse(graph); cout << endl;
bfsTraverse(graph); cout << endl;
return; }
void createGraph(Graph &graph) {
graph.vexNum = 5;
graph.edgeNum = 4;
for (int i = 0; i < graph.vexNum; ++i) { graph.vexs[i] = i; }
graph.edges[0][1] = 1; graph.edges[1][0] = 1;
graph.edges[0][2] = 1; graph.edges[2][0] = 1;
graph.edges[1][3] = 1; graph.edges[3][1] = 1;
graph.edges[2][3] = 1; graph.edges[3][2] = 1;
return; }
void dfsTraverse(Graph graph) { for (int i = 0; i < graph.vexNum; ++i) {
g_visited[i] = false; }
for (int i = 0; i < graph.vexNum; ++i) { if (g_visited[i] == false) { dfs(graph, i); } }
return; }
void dfs(Graph graph, int i) { cout << graph.vexs[i] << " "; g_visited[i] = true;
for (int j = 0; j < graph.vexNum; ++j) { if (graph.edges[i][j] == 1 && g_visited[j] == false) { dfs(graph, j); } }
return; }
void bfsTraverse(Graph graph) { for (int i = 0; i < graph.vexNum; ++i) { g_visited[i] = false; }
for (int i = 0; i < graph.vexNum; ++i) { if (g_visited[i] == false) { bfs(graph, i); } }
return; }
void bfs(Graph graph, int i) { int queue[MAX_VERTEX_NUM]; int front = 0; int rear = 0;
cout << graph.vexs[i] << " "; g_visited[i] = true;
queue[rear] = i; rear = (rear + 1) % MAX_VERTEX_NUM;
while (front != rear) { i = queue[front]; front = (front + 1) % MAX_VERTEX_NUM;
for (int j = 0; j < graph.vexNum; ++j) { if (graph.edges[i][j] == 1 && g_visited[j] == false) { cout << graph.vexs[j] << " "; g_visited[j] = true;
queue[rear] = j; rear = (rear + 1) % MAX_VERTEX_NUM; } } }
return; }
int main() { use_example();
return 0; }
|
图的邻接表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
| #include <iostream>
using namespace std;
#define MAX_VERTEX_NUM 5
typedef struct EdgeNode { int adjVex; struct EdgeNode *next; } EdgeNode;
typedef struct { int data; struct EdgeNode *firstEdge; } VertexNode;
typedef struct { int vexNum; int edgeNum; VertexNode adjList[MAX_VERTEX_NUM]; } Graph;
bool g_visited[MAX_VERTEX_NUM];
void use_example(); void createGraph(Graph &graph); void dfsTraverse(Graph graph); void dfs(Graph graph, int i); void bfsTraverse(Graph graph); void bfs(Graph graph, int i);
void use_example() { Graph graph; createGraph(graph);
dfsTraverse(graph); cout << endl;
bfsTraverse(graph); cout << endl;
return; }
void createGraph(Graph &graph) {
graph.vexNum = 5;
graph.edgeNum = 4;
for (int i = 0; i < graph.vexNum; ++i) { graph.adjList[i].data = i; graph.adjList[i].firstEdge = nullptr; }
struct EdgeNode *node = nullptr;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 2; node->next = graph.adjList[0].firstEdge; graph.adjList[0].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 1; node->next = graph.adjList[0].firstEdge; graph.adjList[0].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 3; node->next = graph.adjList[1].firstEdge; graph.adjList[1].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 0; node->next = graph.adjList[1].firstEdge; graph.adjList[1].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 3; node->next = graph.adjList[2].firstEdge; graph.adjList[2].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 0; node->next = graph.adjList[2].firstEdge; graph.adjList[2].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 2; node->next = graph.adjList[3].firstEdge; graph.adjList[3].firstEdge = node;
node = (struct EdgeNode *)malloc(sizeof(struct EdgeNode) * 1); node->adjVex = 1; node->next = graph.adjList[3].firstEdge; graph.adjList[3].firstEdge = node;
return; }
void dfsTraverse(Graph graph) { for (int i = 0; i < graph.vexNum; ++i) { g_visited[i] = false; }
for (int i = 0; i < graph.vexNum; ++i) { if (g_visited[i] == false) { dfs(graph, i); } }
return; }
void dfs(Graph graph, int i) { cout << graph.adjList[i].data << " "; g_visited[i] = true;
struct EdgeNode *node = graph.adjList[i].firstEdge;
while (node != nullptr) { if (g_visited[node->adjVex] == false) { dfs(graph, node->adjVex); }
node = node->next; }
return; }
void bfsTraverse(Graph graph) { for (int i = 0; i < graph.vexNum; ++i) { g_visited[i] = false; }
for (int i = 0; i < graph.vexNum; ++i) { if (g_visited[i] == false) { bfs(graph, i); } }
return; }
void bfs(Graph graph, int i) { int queue[MAX_VERTEX_NUM]; int front = 0; int rear = 0;
cout << graph.adjList[i].data << " "; g_visited[i] = true;
queue[rear] = i; rear = (rear + 1) % MAX_VERTEX_NUM;
while (front != rear) { i = queue[front]; front = (front + 1) % MAX_VERTEX_NUM;
struct EdgeNode *node = graph.adjList[i].firstEdge;
while (node != nullptr) { if (g_visited[node->adjVex] == false) { cout << node->adjVex << " "; g_visited[node->adjVex] = true;
queue[rear] = node->adjVex; rear = (rear + 1) % MAX_VERTEX_NUM; }
node = node->next; } }
return; }
int main() { use_example();
return 0; }
|
总结
不同参考资料中,图的描述实现各不相同,但基本思想是一致的。作者使用规范的变量命名、提供详细的步骤解析及使用示例,应用C++语言将其整合成模板,以帮助理解记忆。
参考资料
- 《2023版数据结构高分笔记》主编:率辉
- 《2023年计算机组成原理考研复习指导》组编:王道论坛
- 《大话数据结构》作者:程杰
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获