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

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

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

方法思路

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

    #include 
    #include
    #include
    #include
    #define MAXN 10005
    typedef 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/

    你可能感兴趣的文章
    Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
    查看>>
    Objective-C实现hornerMethod霍纳法算法(附完整源码)
    查看>>
    Objective-C实现Http Post请求(附完整源码)
    查看>>
    Objective-C实现Http协议下载文件(附完整源码)
    查看>>
    Objective-C实现IIR 滤波器算法(附完整源码)
    查看>>
    Objective-C实现IIR数字滤波器(附完整源码)
    查看>>
    Objective-C实现insertion sort插入排序算法(附完整源码)
    查看>>
    Objective-C实现integer partition整数分区算法(附完整源码)
    查看>>
    Objective-C实现integerPartition整数划分算法(附完整源码)
    查看>>
    Objective-C实现interpolation search插值搜索算法(附完整源码)
    查看>>
    Objective-C实现Interpolation search插值查找算法(附完整源码)
    查看>>
    Objective-C实现intersection交集算法(附完整源码)
    查看>>
    Objective-C实现intro sort内省排序算法(附完整源码)
    查看>>
    Objective-C实现inversions倒置算法(附完整源码)
    查看>>
    Objective-C实现isalpha函数功能(附完整源码)
    查看>>
    Objective-C实现islower函数功能(附完整源码)
    查看>>
    Objective-C实现isPowerOfTwo算法(附完整源码)
    查看>>
    Objective-C实现isupper函数功能(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>