一个无向图 V(G1)={0,1,2,3,4,5,6,7,8,9,10} 边集合 E(G1)={(0,1),(0,2),(0,3),(1,4),(1,5),(2,5),(2,6),(3,7),(6,8),(6,9),(7,10)}请用c语言写出它邻接矩阵
时间: 2023-06-19 19:06:04 浏览: 40
以下是用 C 语言写出该无向图邻接矩阵的代码:
```c
#include <stdio.h>
#define N 11
int main() {
int matrix[N][N] = {0}; // 初始化邻接矩阵为 0
// 添加边
matrix[0][1] = matrix[1][0] = 1;
matrix[0][2] = matrix[2][0] = 1;
matrix[0][3] = matrix[3][0] = 1;
matrix[1][4] = matrix[4][1] = 1;
matrix[1][5] = matrix[5][1] = 1;
matrix[2][5] = matrix[5][2] = 1;
matrix[2][6] = matrix[6][2] = 1;
matrix[3][7] = matrix[7][3] = 1;
matrix[6][8] = matrix[8][6] = 1;
matrix[6][9] = matrix[9][6] = 1;
matrix[7][10] = matrix[10][7] = 1;
// 输出邻接矩阵
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
0 1 1 1 0 0 0 0 0 0 0
1 0 0 0 1 1 0 0 0 0 0
1 0 0 0 0 1 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 1
0 1 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)