C++ version of "importance" calculation of nodes in PTA social network graph——Shandong University of Science and Technology

Title:
In a social network, individuals or units (nodes) are connected by certain relationships (edges). They are influenced by these relationships, which can be understood as an interaction that spreads between interconnected nodes in the network, which can be strengthened or weakened. The importance of nodes in the network varies according to their locations.

"Centrality centrality" is an index used to measure the "speed" of a node reaching other nodes, that is, a node with a higher centrality can be faster than a node with a lower centrality (average meaning Bottom) to reach other nodes in the network, so it has more important value in the propagation process of the network. In a network with N nodes, the "closeness centrality" Cc(vi​i​​) of node vi​is mathematically defined as vi​​to all other nodes v​j​​ (j≠i) The reciprocal of the average value of the shortest distance d(vi​i​,v​j​):

For a disconnected graph, the closeness centrality of all nodes is 0.

Given an unweighted undirected graph and a set of nodes in it, compute the closeness centrality of each node in the set.
Input format:

Enter the first line to give two positive integers N and M, where N (≤10​4​​) is the number of nodes in the graph, and by the way assume that the nodes are numbered from 1 to N; M (≤10​5​​ ) is the number of edges. In the following M lines, each line gives the information of an edge, that is, the numbers of the two nodes connected by the edge, separated by spaces. The last line gives the number K (≤100) of the group of nodes that need to calculate the closeness centrality and the number of K nodes, separated by spaces.
Output format:
Output format:
Output the compactness centrality of K given nodes in the format of Cc(i)=x.xx, each output occupies one line, and the result is kept to 2 decimal places.
Input sample:

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
#include<bits/stdc++.h>
using namespace std;
#define MAX 10001
#define MAXCOST 0x7fffffff
struct NODE
{
    
    
    int to[MAX];
    int num;
    int cost;
}node[MAX];
int len[MAX];
bool vis[MAX];
int m,n;
int solve(int x)
{
    
    
    int sum=0,cnt=x;
    queue<int> q;
    q.push(x);
    fill(len,len+MAX,MAXCOST);
    memset(vis,false,sizeof(vis));
    vis[x]=true;
    len[x]=0;
    while(!q.empty())//广搜取相邻节点
    {
    
    
        x=q.front();
        q.pop();
        for(int i=0;i<node[x].num;i++)
        {
    
    
            if(!vis[node[x].to[i]])
            {
    
    
                vis[node[x].to[i]]=true;
                len[node[x].to[i]]=min(len[node[x].to[i]],len[x]+1);//取最短距离
                q.push(node[x].to[i]);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
    
    
        if(i!=cnt&&len[i]==MAXCOST)//有一个点没找到,说明图不连通
            return 0;
        sum+=len[i];
    }
    return sum;
}
int main()
{
    
    
    //freopen("in.txt","r",stdin);
    cin>>n>>m;
    int r,c,cost=1;
    for(int i=0;i<m;i++)
    {
    
    
        cin>>r>>c;
        node[r].to[node[r].num++]=c;
        node[r].cost=cost;
        node[c].to[node[c].num++]=r;
        node[c].cost=cost;
    }
    int k,x;
    bool flag=true;//是否连通
    cin>>k;
    for(int i=0;i<k;i++)
    {
    
    
        cin>>x;
        int sum=solve(x);
        if(sum==0)
            flag=false;
        if(flag)
            printf("Cc(%d)=%.2f\n",x,1.0*(n-1)/sum);
        else
            printf("Cc(%d)=%.2f\n",x,0.00);
    }
}

For more PTA codes, please refer to my blog

ps: The code is for reference only, please do not plagiarize

Guess you like

Origin blog.csdn.net/scorpion_legend/article/details/109540307