博客
关于我
(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类数组式访问(ArrayAccess接口)
    查看>>
    PHP系列:浅谈PHP中isset()和empty() 函数的区别
    查看>>
    PHP索引数组unset的坑-array_values解决方案
    查看>>
    PHP索引数组排序方法整理(冒泡、选择、插入、快速)
    查看>>
    PHP线程安全和非线程安全
    查看>>
    R3LIVE开源项目常见问题解决方案
    查看>>
    php缃戠珯,www.wfzwz.com
    查看>>
    php缓存查询函数
    查看>>
    php编写TCP服务端和客户端程序
    查看>>
    php编码规范
    查看>>
    PHP编码规范-PSR1、psr2 /psr3 psr4
    查看>>
    PHP编程效率的20个要点
    查看>>
    PHP网页缓存技术优点及代码
    查看>>
    PHP自动化测试(一)make test 和 phpt
    查看>>
    php自定义函数: 文件大小转换成智能形式
    查看>>
    php英语单词,php常用英语单词,快速学习php编程英语(6)
    查看>>
    R3.4.0安装包时报错“需要TRUE/FALSE值的地方不可以用缺少值”,需升级到R3.5.0
    查看>>
    PHP获取curl传输进度
    查看>>
    PHP获取IP所在地区(转)
    查看>>
    PHP获取IP的方法对比
    查看>>