基于三国人物关系数据,运用c语言,构建三国人物关系图。图的顶点为三国人物。边为人物关系(如儿子、义弟、主公)。本题需要实现在图结构上做的各种人物关系查询。人物的属性包括:人名、所属国。 如刘备(刘备,蜀国) 【功能】 1.基于邻接表构建三国人物关系图,并能输出展示邻接表,保存在文件”TRKGraph.txt”中。文件格式如下: 刘备-->张飞--->关羽--->赵云 注意:图的构建,应该先整理图中的顶点数据,即要整理所有三国人物数据作为图顶点。 2.统计人物关系数量最多的前10个三国人物及其所属国。 3.输入2个人物,查找2个人物之间所有关系。基于图的深度遍历,展示关系路径。并能说明最短路径是哪条。答辩时候展示:有关系的人物。无关系的人物。 4.智能问答。输入荀彧的主公的儿子是谁?能输出关系路径,并给出答案。
时间: 2024-03-30 09:33:36 浏览: 61
好的,让我来回答你的问题。
对于第一个问题,需要使用邻接表来构建三国人物关系图,具体的实现可以使用C语言来完成。首先需要整理出所有的三国人物数据作为图顶点,然后根据人物之间的关系建立边,如儿子、义弟、主公等,最终构建出完整的图。可以通过遍历邻接表来输出展示三国人物关系图,同时将邻接表保存在文件"TRKGraph.txt"中。
对于第二个问题,需要统计人物关系数量最多的前10个三国人物及其所属国。可以通过遍历邻接表,统计每个人物的关系数量,并将结果按照关系数量降序排列,最终输出前10个人物及其所属国。
对于第三个问题,需要输入2个人物,查找2个人物之间所有关系。可以基于图的深度遍历,展示关系路径,并说明最短路径是哪条。首先需要判断输入的2个人物是否在图中存在,如果存在,则利用深度遍历算法来找到2个人物之间的所有关系路径,并输出最短路径。
对于第四个问题,需要实现智能问答功能。输入荀彧的主公的儿子是谁?程序需要能够根据输入的问题,找到荀彧、荀彧的主公、荀彧的主公的儿子之间的关系路径,并输出答案。
相关问题
用c语言代码建立三国人物关系图谱
好的,下面是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_RELATIONS 50
typedef struct {
char name[MAX_NAME_LEN];
int id;
} Person;
typedef struct {
int from_id;
int to_id;
} Relation;
int main() {
Person persons[MAX_RELATIONS * 2];
int num_persons = 0;
Relation relations[MAX_RELATIONS];
int num_relations = 0;
// 添加人物
strcpy(persons[num_persons].name, "刘备");
persons[num_persons].id = num_persons;
num_persons++;
strcpy(persons[num_persons].name, "关羽");
persons[num_persons].id = num_persons;
num_persons++;
strcpy(persons[num_persons].name, "张飞");
persons[num_persons].id = num_persons;
num_persons++;
strcpy(persons[num_persons].name, "诸葛亮");
persons[num_persons].id = num_persons;
num_persons++;
strcpy(persons[num_persons].name, "赵云");
persons[num_persons].id = num_persons;
num_persons++;
// 添加人物关系
relations[num_relations].from_id = 0; // 刘备
relations[num_relations].to_id = 1; // 关羽
num_relations++;
relations[num_relations].from_id = 0; // 刘备
relations[num_relations].to_id = 2; // 张飞
num_relations++;
relations[num_relations].from_id = 0; // 刘备
relations[num_relations].to_id = 3; // 诸葛亮
num_relations++;
relations[num_relations].from_id = 3; // 诸葛亮
relations[num_relations].to_id = 4; // 赵云
num_relations++;
// 打印人物关系图谱
printf("三国人物关系图谱:\n");
for (int i = 0; i < num_persons; i++) {
printf("%s: ", persons[i].name);
for (int j = 0; j < num_relations; j++) {
if (relations[j].from_id == i) {
printf("%s ", persons[relations[j].to_id].name);
} else if (relations[j].to_id == i) {
printf("与%s ", persons[relations[j].from_id].name);
}
}
printf("\n");
}
return 0;
}
```
这里我们定义了两个结构体:`Person` 用来存储人物的姓名和 ID,`Relation` 用来存储人物之间的关系(使用 ID 来表示人物)。然后我们添加了一些人物和人物之间的关系,最后打印出人物关系图谱。
用c语言,统计人物关系数量最多的前10个三国人物及其所属国。
这个问题需要一个三国人物关系的数据集,可以使用图论算法来解决,下面是一个简单的实现思路:
1. 定义一个结构体来存储人物的姓名和所属国家信息。
```
typedef struct character{
char name[30];
char country[10];
} Character;
```
2. 读取三国人物关系数据集,将人物信息存储到一个数组中。
```
Character characters[N]; // N为人物数量
// 读取数据集,并将人物信息存储到characters数组中
```
3. 构建一个邻接矩阵来表示人物之间的关系。
```
int graph[N][N]; // 邻接矩阵,N为人物数量
// 初始化邻接矩阵
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
graph[i][j] = 0;
}
}
// 读取人物之间的关系,更新邻接矩阵
```
4. 使用深度优先搜索算法遍历图,统计每个人物的关系数量。
```
int visited[N]; // 标记数组,记录每个人物是否已被访问
int count[N]; // 记录每个人物的关系数量
int max_count = 0; // 记录最大关系数量
// 初始化visited和count数组
for(int i=0; i<N; i++){
visited[i] = 0;
count[i] = 0;
}
// 深度优先搜索算法
void dfs(int u){
visited[u] = 1;
for(int v=0; v<N; v++){
if(graph[u][v] && !visited[v]){
count[v]++;
if(count[v] > max_count){
max_count = count[v];
}
dfs(v);
}
}
}
// 遍历每个人物,统计关系数量
for(int i=0; i<N; i++){
if(!visited[i]){
count[i] = 1;
dfs(i);
}
}
```
5. 找出关系数量最多的前10个人物及其所属国家。
```
// 定义一个结构体来存储人物的姓名、所属国家和关系数量
typedef struct character_info{
char name[30];
char country[10];
int count;
} CharacterInfo;
CharacterInfo infos[N]; // N为人物数量
// 初始化infos数组
for(int i=0; i<N; i++){
strcpy(infos[i].name, characters[i].name);
strcpy(infos[i].country, characters[i].country);
infos[i].count = count[i];
}
// 使用快速排序算法将infos数组按照关系数量从大到小排序
void quick_sort(CharacterInfo *infos, int left, int right){
if(left >= right){
return;
}
int i = left;
int j = right;
CharacterInfo pivot = infos[left];
while(i < j){
while(i < j && infos[j].count <= pivot.count){
j--;
}
if(i < j){
infos[i] = infos[j];
i++;
}
while(i < j && infos[i].count > pivot.count){
i++;
}
if(i < j){
infos[j] = infos[i];
j--;
}
}
infos[i] = pivot;
quick_sort(infos, left, i-1);
quick_sort(infos, i+1, right);
}
quick_sort(infos, 0, N-1);
// 输出关系数量最多的前10个人物及其所属国家
for(int i=0; i<10; i++){
printf("%s %s %d\n", infos[i].name, infos[i].country, infos[i].count);
}
```
这个实现思路只是一个简单的示例,具体实现过程可能需要根据数据集的不同进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)