zoukankan      html  css  js  c++  java
  • 06-图3 六度空间(30 分)

    “六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。


    图1 六度空间示意图

    “六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。

    假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。

    输入格式:

    输入第1行给出两个正整数,分别表示社交网络图的结点数N(1,表示人数)、边数M(≤,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。

    输出格式:

    对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。

    输入样例:

    10 9
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    

    输出样例:

    1: 70.00%
    2: 80.00%
    3: 90.00%
    4: 100.00%
    5: 100.00%
    6: 100.00%
    7: 100.00%
    8: 90.00%
    9: 80.00%
    10: 70.00%

    我的答案
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 
      5 #define MaxVertexMax 10000    /* 最大顶点数设为100 */
      6 #define INFINITY     65535  /* 设为双字节无符号证书的最大值 */
      7 #define MaxSize      330000
      8 
      9 typedef int Vertex;
     10 typedef int WeightType;
     11 typedef char DataType;
     12 
     13 typedef struct ENode *PtrToENode;
     14 struct ENode {
     15     Vertex V1, V2;
     16     WeightType Weight;
     17 };
     18 typedef PtrToENode Edge;
     19 
     20 typedef struct GNode *PtrToGNode;
     21 struct GNode {
     22     int Nv;
     23     int Ne;
     24     WeightType G[MaxVertexMax][MaxVertexMax];
     25     DataType Data[MaxVertexMax];
     26 };
     27 typedef PtrToGNode MGraph;
     28 
     29 struct QNode {
     30     Vertex Data[MaxSize];
     31     int rear;
     32     int front;
     33 };
     34 typedef struct QNode *Queue;
     35 
     36 MGraph CreateGraph(int VertexNum)
     37 {
     38     Vertex V, W;
     39     MGraph Graph;
     40 
     41     Graph = (MGraph)malloc(sizeof(struct GNode));
     42     Graph->Nv = VertexNum;
     43     Graph->Ne = 0;
     44 
     45     for(V=0;V<=Graph->Nv;V++)
     46         for(W=0;W<=Graph->Nv;W++)
     47             Graph->G[V][W] = INFINITY;
     48 
     49     return Graph;
     50 }
     51 
     52 void InsertEdge(MGraph Graph, Edge E)
     53 {
     54     Graph->G[E->V1][E->V2] = E->Weight;
     55     Graph->G[E->V2][E->V1] = E->Weight;
     56 }
     57 
     58 MGraph BuildGraph()
     59 {
     60     MGraph Graph;
     61     Edge E;
     62     Vertex V;
     63     int Nv, i;
     64 
     65     scanf("%d", &Nv);
     66     Graph = CreateGraph(Nv);
     67     scanf("%d
    ", &(Graph->Ne));
     68     if(Graph->Ne != 0) {
     69         E = (Edge)malloc(sizeof(struct ENode));
     70         E->Weight = 1;
     71         for(i=0;i<Graph->Ne;i++) {
     72             scanf("%d %d
    ", &E->V1, &E->V2);
     73             InsertEdge(Graph, E);
     74         }
     75     }
     76 
     77     for(V=0;V<Graph->Nv;V++)
     78         scanf(" %c", &(Graph->Data[V]));
     79 
     80     return Graph;
     81 }
     82 
     83 void PrintGraph(MGraph Graph)
     84 {
     85     Vertex V, W;
     86     printf("Graph:
    ");
     87     for(V=1;V<=Graph->Nv;V++) {
     88         for(W=1;W<Graph->Nv;W++) 
     89             printf("%5d " ,Graph->G[V][W]);
     90         printf("
    ");
     91     }
     92     printf("-----------------------
    ");
     93 }
     94 
     95 //广度优先
     96 int IsEmpty(Queue Q)
     97 {
     98     return (Q->rear == Q->front);   //1:empty 0:not empty
     99 }
    100 
    101 void AddQ(Queue PtrQ, Vertex item)
    102 {
    103     if((PtrQ->rear+1)%MaxSize == PtrQ->front) {
    104         printf("Queue full");
    105         return;
    106     }
    107     PtrQ->rear = (PtrQ->rear+1)%MaxSize;
    108     PtrQ->Data[PtrQ->rear] = item;
    109 }
    110 
    111 Vertex DeleteQ(Queue PtrQ)
    112 {
    113     if(PtrQ->front == PtrQ->rear) {
    114         printf("Queue empty");
    115         return -1;
    116     } else {
    117         PtrQ->front = (PtrQ->front+1)%MaxSize;
    118         return PtrQ->Data[PtrQ->front];
    119     } 
    120 }
    121 
    122 void Visit(Vertex V)
    123 {
    124     printf(" %d", V);
    125 }
    126 
    127 int IsEdge(MGraph Graph, Vertex V, Vertex W)
    128 {
    129     return Graph->G[V][W]<INFINITY?1:0;
    130 }
    131 
    132 int BFS(MGraph Graph, Vertex S, int *Visited)
    133 {
    134     Queue Q;
    135     Vertex V, W;
    136     int count = 0, level = 0, last = S, tail = 0;
    137     Q = (Queue)malloc(sizeof(struct QNode));
    138 
    139     // Visit(S);
    140     Visited[S] = 1;
    141     count++;
    142     AddQ(Q, S);
    143 
    144     while(!IsEmpty(Q)) {
    145         V = DeleteQ(Q);
    146         for(W=1;W<=Graph->Nv;W++) {
    147             if(!Visited[W] && IsEdge(Graph, V, W)) {
    148                 // Visit(W);
    149                 Visited[W] = 1;
    150                 AddQ(Q, W);
    151                 count++;
    152                 tail = W;       //下一层最后一个
    153             }
    154         }
    155         if(V == last) {
    156             level++;
    157             last = tail;
    158         }
    159         if(level == 6) break;
    160     }
    161 
    162     return count;
    163 }
    164 
    165 int main()
    166 {
    167     MGraph Graph;
    168     int *Visited, i, j;
    169     int count = 0;
    170     Graph = BuildGraph();
    171     Visited = (int *)malloc(sizeof(int)*(Graph->Nv+1));
    172     for(i=0;i<=Graph->Nv;i++)
    173         Visited[i] = 0;
    174     
    175     // PrintGraph(Graph);
    176 
    177     for(i=1;i<=Graph->Nv;i++) {
    178         if(!Visited[i]) {
    179             count = BFS(Graph, i, Visited);
    180             // printf(" count %d
    ", count);
    181             printf("%d: %2.2f%
    ", i, (float)count/Graph->Nv*100);
    182             for(j=0;j<=Graph->Nv;j++)
    183                 Visited[j] = 0;
    184         }
    185     }
    186     return 0;
    187 }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    Windows Service 2016 DatacenterStandEmbedded激活方法
    批处理文件设置IP以及DNS
    C#类的一些基础知识(静态方法可以不用实例化调用)
    Dynamics Crm Plugin插件注册的问题及解决方案(持续更新。。。。。。)
    【转载】C# get 与set的一些说明
    C#补位函数PadLeft和PadRight
    Kubernetes集群调度之Scheduler
    Kubernetes集群控制之ControllerManager
    Kubernetes集群大脑之apiserver
    Kubernetes集群存储之etcd
  • 原文地址:https://www.cnblogs.com/ch122633/p/8971199.html
Copyright © 2011-2022 走看看