键盘输入数据,建立一个有向图的邻接表的代码
时间: 2024-02-11 17:08:33 浏览: 87
以下是一个简单的键盘输入数据,建立有向图邻接表的代码。该代码使用了 STL 中的 vector 和 unordered_map 容器。
```cpp
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
int n, m;
cin >> n >> m; // n 为节点数,m 为边数
unordered_map<int, vector<int>> graph; // 定义邻接表
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v; // u -> v 有一条有向边
graph[u].push_back(v); // 加入邻接表
}
return 0;
}
```
在这个代码中,我们首先读入节点数 n 和边数 m,然后定义一个 unordered_map 容器 graph,用于存储邻接表。接下来,我们通过循环读入每一条边,将其加入到邻接表中。最后,我们返回 0 表示程序正常结束。
相关问题
使用c语言解决以下问题需要是一个有完整主函数的代码(1) 键盘输入数据,建立一个有向图的邻接表。 (2) 输出该邻接表。 (3) 在有向图的邻接表的基础上计算各顶点的度,并输出。
以下是一个实现题目要求的完整C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义邻接表结构体
typedef struct AdjListNode {
int dest; // 目标顶点
struct AdjListNode* next; // 指向下一个邻接点的指针
} AdjListNode;
typedef struct AdjList {
AdjListNode* head; // 指向邻接点链表的头指针
} AdjList;
typedef struct Graph {
int V; // 顶点数
AdjList* array; // 邻接表数组
} Graph;
// 初始化邻接表
AdjListNode* newAdjListNode(int dest) {
AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// 创建有向图的邻接表
Graph* createGraph(int V) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->V = V;
graph->array = (AdjList*)malloc(V * sizeof(AdjList));
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
// 添加边到邻接表中
void addEdge(Graph* graph, int src, int dest) {
AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
// 打印邻接表
void printGraph(Graph* graph) {
for (int i = 0; i < graph->V; ++i) {
AdjListNode* pCrawl = graph->array[i].head;
printf("\n 顶点 %d 的邻接点有: ", i);
while (pCrawl) {
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
// 计算各顶点的出度和入度
void calculateDegree(Graph* graph) {
int inDegree[graph->V], outDegree[graph->V];
for (int i = 0; i < graph->V; ++i) {
inDegree[i] = 0;
outDegree[i] = 0;
}
for (int i = 0; i < graph->V; ++i) {
AdjListNode* pCrawl = graph->array[i].head;
while (pCrawl) {
++outDegree[i];
++inDegree[pCrawl->dest];
pCrawl = pCrawl->next;
}
}
printf("\n顶点 入度 出度\n");
for (int i = 0; i < graph->V; ++i) {
printf(" %d %d %d\n", i, inDegree[i], outDegree[i]);
}
}
int main() {
int V, E, src, dest;
printf("请输入顶点数和边数:");
scanf("%d%d", &V, &E);
// 创建邻接表
Graph* graph = createGraph(V);
// 添加边
printf("请输入边的起点和终点:\n");
for (int i = 0; i < E; ++i) {
scanf("%d%d", &src, &dest);
addEdge(graph, src, dest);
}
// 打印邻接表
printf("\n该有向图的邻接表如下:\n");
printGraph(graph);
// 计算顶点度
calculateDegree(graph);
return 0;
}
```
这个程序首先根据用户输入的顶点数和边数创建了邻接表,然后遍历所有边,将边添加到邻接表中。接着打印邻接表和计算各顶点的度。
) 键盘输入数据,建立一个有向图的邻接表。 (2) 输出该邻接表。 (3) 在有向图的邻接表的基础上计算各顶点的度,并输出
好的,让我来解答你的问题。
首先,让我们来看看如何建立有向图的邻接表。假设输入的数据格式为:
```
5 6
1 2
1 3
2 3
2 4
3 4
4 5
```
其中第一行的两个数字表示顶点数和边数,接下来的每一行表示一条边。
那么我们可以使用 Python 语言来实现建立邻接表的过程,代码如下:
```python
n, m = map(int, input().split())
# 初始化邻接表
adj = [[] for i in range(n)]
# 添加边
for i in range(m):
u, v = map(int, input().split())
adj[u-1].append(v)
# 输出邻接表
for i in range(n):
print(i+1, end=': ')
for j in adj[i]:
print(j, end=' ')
print()
```
接下来,我们来看看如何计算各顶点的度。有向图的度分为出度和入度,出度表示从该顶点出发的边的数目,入度表示指向该顶点的边的数目。
那么我们可以使用以下代码来计算各顶点的度:
```python
# 计算各顶点的出度
out_degree = [len(adj[i]) for i in range(n)]
# 计算各顶点的入度
in_degree = [0] * n
for i in range(n):
for j in adj[i]:
in_degree[j-1] += 1
# 输出各顶点的度
for i in range(n):
print("顶点 %d 的度为:出度 %d,入度 %d" % (i+1, out_degree[i], in_degree[i]))
```
这样,我们就完成了建立有向图的邻接表、输出邻接表和计算各顶点的度的过程。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)