博客
关于我
(c语言详解)06-图3 六度空间 (30分)(详细解释)
阅读量:148 次
发布时间:2019-02-28

本文共 2271 字,大约阅读时间需要 7 分钟。

为了解决这个问题,我们需要验证“六度空间”理论,即在给定的社交网络图中,对于每个节点,计算其在六度空间内的节点占总节点数的百分比。六度空间理论指出,在一个社交网络中,任意两个陌生人之间最多有六个人相连,即最多通过五个人可以认识对方。

方法思路

  • 构建邻接表:使用邻接表存储社交网络图,这样可以高效地访问每个节点的邻居。
  • 广度优先搜索 (BFS):对于每个节点,使用BFS计算其在六度空间内的节点数。BFS从起点出发,层次遍历,直到达到六层或更深时停止。
  • 计算六度空间覆盖率:统计每个节点在六度空间内的节点数,计算其占总节点数的百分比,并按要求格式输出结果。
  • 解决代码

    #include 
    #include
    #include
    #include
    #define MAXN 10005typedef struct VNode { struct VNode* Next; int V;} Vertex;typedef struct LNode { struct VNode* FirstEdge;} List;typedef struct GNode { int Nv, Ne; List Head;} Graph;void Insert(int u, int v, Graph* G) { Vertex* newU = (Vertex*)malloc(sizeof(Vertex)); newU->V = u; newU->Next = G->Head[u].FirstEdge; G->Head[u].FirstEdge = newU; Vertex* newV = (Vertex*)malloc(sizeof(Vertex)); newV->V = v; newV->Next = G->Head[v].FirstEdge; G->Head[v].FirstEdge = newV;}void BFS(int s, int Nv, Graph* G, FILE* output) { int visited[Nv + 1] = {0}; int cnt = 0; std::queue
    > q; visited[s] = 1; cnt = 1; q.push({s, 0}); while (!q.empty()) { auto current = q.front(); q.pop(); int v = current.first; int d = current.second; if (d >= 5) { continue; } for (Vertex* neighbor = G->Head[v].FirstEdge; neighbor != NULL; neighbor = neighbor->Next) { int w = neighbor->V; if (!visited[w]) { visited[w] = 1; cnt++; q.push({w, d + 1}); } } } double perc = (double)cnt / (double)Nv * 100.0; perc = round(perc * 100) / 100.0; // 保留两位小数 fprintf(output, "%d: %.2f%%\n", s, perc);}int main() { char* input = NULL; size_t n = 0; FILE* input_file = fopen("input.txt", "r"); if (input_file == NULL) { printf("无法打开输入文件\n"); return 1; } Graph* G = (Graph*)malloc(sizeof(struct GNode)); G->Nv = 0; G->Ne = 0; while (fgets(input, sizeof(input), input_file) != NULL) { n = strlen(input); if (n < 2) { continue; } int u, v; sscanf(input, "%d %d", &u, &v); Insert(u, v, G); G->Ne++; } G->Nv = n; FILE* output = fopen("output.txt", "w"); for (int i = 1; i <= G->Nv; ++i) { BFS(i, G->Nv, G, output); } fclose(input_file); fclose(output); free(G); return 0;}

    代码解释

  • 数据结构定义:定义了邻接表和相关数据结构,包括顶点和列表。
  • 插入边Insert函数用于在邻接表中插入边,确保每条边是双向的。
  • BFS函数:从给定的起点开始,层次遍历,直到达到六层或更深时停止。统计访问节点数,计算百分比并输出。
  • 主函数:读取输入,构建邻接表,处理每个节点的BFS,输出结果。
  • 通过这种方法,我们可以高效地计算每个节点在六度空间内的节点覆盖率,验证“六度空间”理论。

    转载地址:http://pcrd.baihongyu.com/

    你可能感兴趣的文章
    Springboot基于Redisson实现Redis分布式可重入锁【案例到源码分析】
    查看>>
    PHP利用正则表达式实现手机号码中间4位用星号(*)替换显示
    查看>>
    PHP加密与安全的最佳实践
    查看>>
    PHP加速器eaccelerator导致php-fpm进程卡死原因分析
    查看>>
    PHP区分 企业微信浏览器 | 普通微信浏览器 | 其他浏览器
    查看>>
    php原生代码怎么连表查询,PHP tp5中使用原生sql查询代码实例
    查看>>
    PHP去掉转义符
    查看>>
    php去除字符串开头或末尾的字符(例如逗号)
    查看>>
    php反射api
    查看>>
    PHP反射ReflectionClass、ReflectionMethod 入门教程
    查看>>
    PHP反射机制
    查看>>
    php取当天的最后一秒_Docker快速搭建PHP开发环境详细教程
    查看>>
    php取绝对值
    查看>>
    PHP变量内容的获取
    查看>>
    php各种常用的算法
    查看>>
    php各种缓存策略对比
    查看>>
    RabbitMQ高级特性 - 消息分发(限流、负载均衡)
    查看>>
    php后台“爬虫”模拟登录第三方系统
    查看>>
    php后台的在控制器中就可以实现阅读数增加
    查看>>
    php命令行生成项目结构
    查看>>