博客
关于我
(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/

    你可能感兴趣的文章
    php微信公众号开发access_token获取
    查看>>
    php微信公众号开发用户基本信息
    查看>>
    php怎么将对象变成数组,php怎么将对象转换成数组
    查看>>
    php怎样比较两数大小,jquery如何判断两个数值的大小
    查看>>
    PHP性能监控 - 开启xhprof(一)
    查看>>
    PHP性能监控 - 怎么看xhprof报告(二)
    查看>>
    php手冊,php手冊之變量范圍
    查看>>
    PHP手机号码归属地查询API接口
    查看>>
    php把get参数放入数组_php怎么将数组转为url参数?
    查看>>
    php操作mysql用select_php如何操作mysql获取select 结果
    查看>>
    PHP操作符与控制结构
    查看>>
    php支付宝手机网页支付类实例
    查看>>
    php教程之php空白页的原因及解决方法
    查看>>
    PHP数据库操作
    查看>>
    PHP数据文件过大,导致PHP加速器eaccelerator在PHP5.2版本下崩溃
    查看>>
    PHP数据访问的多重查询(租房子查询)
    查看>>
    RabbitMQ - 基于 SpringAMQP 带你实现五种消息队列模型
    查看>>
    php数组函数分析--array_column
    查看>>
    php数组实现:哈希 +双向链表
    查看>>
    PHP数组排序函数array_multisort()函数详解(二)
    查看>>