PTA Title Set data structures and algorithms (Chinese) 7-36 social network diagram nodes "importance" is calculated (30)

PTA Title Set data structures and algorithms (Chinese) 7-36 social network diagram nodes "importance" is calculated (30)

7-36 node in a social network map "importance" is calculated (30)
 

In the social network, or between the individual units (nodes) are related by some relationships (edges). They affected these relationships, this effect will be appreciated that the spread between the node An interactive network of interconnected, can be reduced can be enhanced. According to their position and the node, the levels which reflect the importance in the network are not the same.

"Closeness centrality" is a measure of a node reaches the "speed" of indicators other nodes, a node that is higher than the center of a lower central nodes more quickly (average sense under) reaches the other nodes in the network, and therefore a more important value in the propagation network. There are in N-node network, the node V I of the "closeness of the center" (mathematically defined as V I , all other nodes V J ( J I) is the shortest distance (the average of the reciprocal:

For non-communicating FIG tightness central nodes are all 0.

Given a powerless undirected graph nodes and a group wherein the calculating the set of closeness central node in each node.

Input formats:

Input of the first line gives two positive integers N and M, where N ( ≤) is the number of nodes in FIG, assuming way junction from 1 to N number; M ( ≤) is the number of edges. Subsequent M rows, each row one side is given information, i.e., number of the two nodes connected by edges, separated by spaces. The last line gives tightness need to calculate the center of the set number of nodes K ( ≤) and K th node numbers, separated by spaces.

Output formats:

According to Cc(i)=x.xxthe output format of the K given node degree centrality tight, each output per line, to retain the result to one decimal place.

Sample input:

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

Sample output:

Cc(3)=0.47
Cc(4)=0.62
Cc(9)=0.35
Topic Analysis: This is a multi-source shortest path problem a map, use Floyd algorithm to solve
  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<malloc.h>
  5 #define INIFITY 65535
  6 typedef struct ENode* Edge;
  7 struct ENode
  8 {
  9     int V1, V2;
 10 };
 11 typedef struct GNode* Graph;
 12 struct  GNode
 13 {
 14     int G[10000][10000];
 15     float Nv;
 16     int Ne;
 17 };
 18 
 19 int IsEdge(Graph Gra, int V1, int V2)
 20 {
 21     return Gra->G[V1][V2]!=INIFITY;
 22 }
 23 
 24 void Insert(Graph Gra, Edge E)
 25 {
 26     Gra->G[E->V1][E->V2] = 1;
 27     Gra->G[E->V2][E->V1] = 1;
 28 }
 29 
 30 Graph CreateGraph(int Nv)
 31 {
 32     Graph Gra = (Graph)malloc(sizeof(struct GNode));
 33     Gra->Nv = Nv;
 34     Gra->Ne = 0;
 35     for (int i = 1; i <=Gra->Nv; i++)
 36         for (int j = 1; j <= Gra->Nv; j++)
 37         {
 38             Gra->G[i][j] = INIFITY;
 39             if (i == j)
 40                 Gra->G[i][j] = 0;
 41         }
 42     return Gra;
 43 }
 44 
 45 int Dist[10000][10000];
 46 int Flag=1;
 47 void Floyd(Graph Gra)
 48 {
 49     int i, j, k;
 50     for (i = 1; i <= Gra->Nv; i++)
 51         for (j = 1; j <= Gra->Nv; j++)
 52             Dist[i][j] = Gra->G[i][j];
 53     for(int k=1;k<=Gra->Nv;k++)
 54         for(int i=1;i<=Gra->Nv;i++)
 55             for (int j = 1; j <= Gra->Nv; j++)
 56             {
 57                 if (Dist[i][k] + Dist[k][j] < Dist[i][j])
 58                     Dist[i][j] = Dist[i][k] + Dist[k][j];
 59             }
 60     for (i = 1; i <= Gra->Nv; i++)
 61         for (j = 1; j <= Gra->Nv; j++)
 62             if (Dist[i][j] == INIFITY)
 63             {
 64                 Flag = 0;
 65                 break;
 66             }
 67 }
 68 
 69 int main()
 70 {
 71     int N, M;
 72     scanf("%d%d", &N, &M);
 73     Graph Gra = CreateGraph(N);
 74     Gra->Ne = M;
 75     Edge E = (Edge)malloc(sizeof(struct ENode));
 76     for (int i = 0; i < Gra->Ne; i++)
 77     {
 78         scanf("%d%d", &(E->V1), &(E->V2));
 79         Insert(Gra, E);
 80     }
 81     Floyd(Gra);
 82     float num=0;
 83     int K;
 84     scanf("%d", &K);
 85     while (K--)
 86     {
 87         num = 0;
 88         int n;
 89         scanf("%d", &n);
 90         if (Flag)
 91         {
 92             for (int i = 1; i <= Gra->Nv; i++)
 93                 num += Dist[n][i];
 94             printf("Cc(%d)=%.2f\n", n, (Gra->Nv - 1) / num);
 95         }
 96         else
 97             printf("Cc(%d)=0.00\n", n);
 98     }
 99     return 0;
100 }
View Code

 

Guess you like

Origin www.cnblogs.com/57one/p/11666384.html