C语言代码实现邻接矩阵创建无向图
时间: 2023-12-21 15:25:02 浏览: 82
好的,下面是 C 代码,用于创建邻接矩阵表示的无向图:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
int adj_matrix[MAX_VERTICES][MAX_VERTICES];
int visited[MAX_VERTICES];
void create_graph(int num_vertices, int num_edges) {
int i, j, v1, v2;
for (i = 0; i < num_vertices; i++) {
for (j = 0; j < num_vertices; j++) {
adj_matrix[i][j] = 0;
}
visited[i] = 0;
}
for (i = 0; i < num_edges; i++) {
printf("Enter edge %d: ", i+1);
scanf("%d%d", &v1, &v2);
adj_matrix[v1][v2] = 1;
adj_matrix[v2][v1] = 1;
}
}
void print_graph(int num_vertices) {
int i, j;
printf("\nAdjacency Matrix:\n");
for (i = 0; i < num_vertices; i++) {
for (j = 0; j < num_vertices; j++) {
printf("%d ", adj_matrix[i][j]);
}
printf("\n");
}
}
int main() {
int num_vertices, num_edges;
printf("Enter number of vertices: ");
scanf("%d", &num_vertices);
printf("Enter number of edges: ");
scanf("%d", &num_edges);
create_graph(num_vertices, num_edges);
print_graph(num_vertices);
return 0;
}
```
希望对您有帮助!请问您还需要了解哪些问题呢?
阅读全文