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

    你可能感兴趣的文章
    poj2546
    查看>>
    POJ2728 Desert King
    查看>>
    POJ2794 Double Patience[离散概率 状压DP]
    查看>>
    poj2828(线段树查找序列第k小的值)
    查看>>
    POJ2891:Strange Way to Express Integers——题解
    查看>>
    poj3045 Cow Acrobats(二分最大化最小值)
    查看>>
    poj3061 Subsequence(尺取法)
    查看>>
    poj3074 DLX精确覆盖
    查看>>
    poj3252(组合数)
    查看>>
    Qt笔记——QToolBox开发简易QQ聊天界面
    查看>>
    poj3307
    查看>>
    Qt笔记——QString与隐式共享、MVC架构
    查看>>
    Qt笔记——QSemaphore处理生产者/消费者模式
    查看>>
    Qt笔记——QMutex&QWaitCondition处理生产者消费者模式
    查看>>
    Qt笔记——QLable+QPixmap图片缩放踩坑
    查看>>
    Qt笔记——foreach与forever
    查看>>
    QT程序怎么挪到Linux下,linux+Qt程序如何打包发布
    查看>>
    Qt知识:视图框架QGraphicsWidget详解
    查看>>
    SpringBoot中项目启动及定时任务缓存数据库常用数据至内存变量并转换后高频调用
    查看>>
    Qt知识: 画刷风格
    查看>>