用c语言实现:假设图G采用邻接表存储,分别设计实现以下要求的算法并测试: (1)求出图G中每个顶点的出度; (2)判断图G中是否存在边<ij>; (3)将图的邻接表存储结构转换为邻接矩阵
时间: 2024-04-22 19:22:16 浏览: 141
好的,以下是C语言的实现:
1. 求出图G中每个顶点的出度
```c
#include <stdio.h>
#include <stdlib.h>
// 邻接表节点结构体
struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// 邻接表头结构体
struct AdjList {
struct AdjListNode* head;
};
// 图结构体
struct Graph {
int V;
struct AdjList* array;
};
// 创建节点
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// 创建图
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// 添加边
void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// 求出图G中每个顶点的出度
void calculate_out_degree(struct Graph* graph) {
int out_degree[graph->V];
struct AdjListNode* temp;
for (int i = 0; i < graph->V; i++) {
out_degree[i] = 0;
temp = graph->array[i].head;
while (temp != NULL) {
out_degree[i]++;
temp = temp->next;
}
printf("顶点%d的出度为%d\n", i, out_degree[i]);
}
}
int main() {
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
calculate_out_degree(graph);
return 0;
}
```
2. 判断图G中是否存在边<ij>
```c
#include <stdio.h>
#include <stdlib.h>
// 邻接表节点结构体
struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// 邻接表头结构体
struct AdjList {
struct AdjListNode* head;
};
// 图结构体
struct Graph {
int V;
struct AdjList* array;
};
// 创建节点
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// 创建图
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// 添加边
void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// 判断图G中是否存在边<ij>
int is_edge_exist(struct Graph* graph, int i, int j) {
struct AdjListNode* temp = graph->array[i].head;
while (temp != NULL) {
if (temp->dest == j) {
return 1;
}
temp = temp->next;
}
return 0;
}
int main() {
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
int i = 1, j = 4;
if (is_edge_exist(graph, i, j)) {
printf("存在边<%d,%d>\n", i, j);
} else {
printf("不存在边<%d,%d>\n", i, j);
}
return 0;
}
```
3. 将图的邻接表存储结构转换为邻接矩阵
```c
#include <stdio.h>
#include <stdlib.h>
// 邻接表节点结构体
struct AdjListNode {
int dest;
struct AdjListNode* next;
};
// 邻接表头结构体
struct AdjList {
struct AdjListNode* head;
};
// 图结构体
struct Graph {
int V;
struct AdjList* array;
};
// 创建节点
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// 创建图
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// 添加边
void addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// 将图的邻接表存储结构转换为邻接矩阵
void convert_to_adj_matrix(struct Graph* graph) {
int adj_matrix[graph->V][graph->V];
for (int i = 0; i < graph->V; i++) {
for (int j = 0; j < graph->V; j++) {
adj_matrix[i][j] = 0;
}
}
struct AdjListNode* temp;
for (int i = 0; i < graph->V; i++) {
temp = graph->array[i].head;
while (temp != NULL) {
adj_matrix[i][temp->dest] = 1;
temp = temp->next;
}
}
printf("邻接矩阵为:\n");
for (int i = 0; i < graph->V; i++) {
for (int j = 0; j < graph->V; j++) {
printf("%d ", adj_matrix[i][j]);
}
printf("\n");
}
}
int main() {
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
convert_to_adj_matrix(graph);
return 0;
}
```
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.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)
![](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)