Calculation of the "importance" of nodes in the social network graph (30 points) (Floyd)

In social networks, individuals or units (nodes) are connected through certain relationships (edges). They are affected by these relationships, which can be understood as a kind of interaction spreading between interconnected nodes in the network, which can be strengthened or weakened. The importance of nodes in the network varies according to their location.

"Tightness 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 (on average Bottom) 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 "tightness centrality" Cc(v​i​​) of node v​i​​ is mathematically defined as v​i​​ to all other nodes v​j​ The reciprocal of the average value of the shortest distance d(v​i​​ ,v​j​​) of ​ (j≠i):
Insert picture description here
For a disconnected graph, the tightness centrality of all nodes is 0.

Given an unweighted undirected graph and a group of nodes in it, calculate the tightness centrality of each node in this group of nodes.

Input format: The
first line of input gives two positive integers N and M, where N (≤10​4​​) is the number of nodes in the figure. By the way, assume that the nodes are numbered from 1 to N; M (≤10 5​​) is the number of edges. In the subsequent M lines, each line gives the information of an edge, that is, the number of the two nodes connected by the edge, separated by a space. The last line gives the number of nodes K (≤100) and K node numbers of this group of nodes that need to be calculated for tightness centrality, separated by spaces.

Output format: Output the tightness centrality of K given nodes
according to the format of Cc(i)=x.xx , each output occupies one line, and the result is kept to 2 decimal places.

Insert picture description here

Question idea:
1. The shortest path from each vertex to all other points can be calculated using the Floyf algorithm. This is the key point.
2. Note that when initializing the graph, using fill( Graph[0],Graph[0]+MAXN MAXN, INF) will cause the memory to exceed the limit, so a double loop is used, and only the size of n n is initialized.
3. Finally, we need to pay attention to how to judge whether a graph is a non-connected graph after adopting the Floyd algorithm. Through a for loop, check whether there is an INF value in any line. (If connected, after Floyd, each point must be able to reach all other points)

#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 10005;
const int INF = 0x3f3f3f3f;
int Graph[MAXN][MAXN];
void Floyd();
int n, m, k;
int main()
{
    
    

	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
    
    
			if (i == j)
				Graph[i][j] = 0;
			else
				Graph[i][j] = INF;
		}

	int a, b, t;
	while (m--)
	{
    
    
		cin >> a >> b;
		Graph[a][b] = Graph[b][a] = 1;
	}

	Floyd();
	int flag = 1;
	for (int i = 1; i <= n; i++)
		if (Graph[1][i] == INF)
		{
    
    
			flag = 0;
			break;
		}
	cin >> k;
	while (k--)
	{
    
    
		cin >> t;
		double sum = 0;
		for (int i = 1; i <= n; i++)
		{
    
    
			if (Graph[t][i] != INF)
				sum += Graph[t][i];
		}
		double average = (1.0 * (n - 1)) / sum;
		printf("Cc(%d)=%.2f\n", t, flag ? average : 0);
	}

	return 0;
}
void Floyd()
{
    
    
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (Graph[i][k] + Graph[k][j] < Graph[i][j])
					Graph[i][j] = Graph[i][k] + Graph[k][j];
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/114331133