2-3 Importance calculation for nodes in social network diagrams (25 points)

2-3 Importance calculation for nodes in social network diagrams (25 points)

In social networks, individuals or units (nodes) are connected by certain relationships (edges). They are affected by these relationships, which can be interpreted as an interaction that spreads between interconnected nodes in the network and can be enhanced or weakened. Nodes have different importance in the network depending on their location.

Compactness Centrality is a measure of how fast a node arrives at other nodes, that is, a node with a higher centrality can reach other nodes in the network more quickly (on average) than a node with a lower centrality, and thus has more value in the propagation of the network. In a network with N nodes, the "compactness centrality" C**c(v**i) of node v**i is mathematically defined as the inverse of the average value of the shortest distance d(v**i,v**j) from v**i to all other nodes v**j(j=i):

For disconnected graphs, the compactness center of all nodes is 0.

Given an unweighted undirected graph and a set of nodes, the compactness center of each node in the set is calculated.

Input format:

The first line of input gives two positive integers N and M, where N (< 104) is the number of nodes in the graph, which is conveniently assumed to be numbered from 1 to N. M (< 105) is the number of edges. In subsequent M lines, each line gives information about an edge, the two node numbers that the edge connects, separated by a space. The last line gives the number of K (< 100) and the number of K nodes for which compactness centrality needs to be calculated, separated by spaces.

Output format:

Outputs the compactness centrality of K given nodes in the format Cc(i)=x.x x, one line for each output, leaving the result 2 digits after the decimal point.

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
 No empty line at end

Output sample:

Cc(3)=0.47
Cc(4)=0.62
Cc(9)=0.35
 No empty line at end

It involves:

AC Code

#include <stdio.h>
#include <stdlib.h>
#define MAX 10001
#define inf 10000

typedef struct node
{
	int V;
	int Edge[MAX][MAX];
}*Graph;

Graph G;

Graph Create(int V,int M)
{
	int i, j, u, v;
    G = (Graph)malloc(sizeof(struct node));
	G->V = V;
	for (i = 0; i < G->V; i++)
		for (j = 0; j < G->V; j++)
			G->Edge[i][j] = 0;
	for (i = 0; i < M; i++)
	{
		scanf("%d", &u);
		scanf("%d", &v);
		G->Edge[u - 1][v - 1] = 1;
		G->Edge[v - 1][u - 1] = 1;
	}
	return G;
}
void Cc(int n)
{
	n--;
	int i, k, path = 0;
	int queue[MAX], dis[MAX];
	int front = -1, rear = 0;
	for (i = 0; i < G->V; i++)
		dis[i] = inf;
	queue[rear] = n;//Queue n
	dis[n] = 0;//Set n to 0
	while (front!=rear)
	{
		k = queue[++front];
		for (i = 0; i < G->V; i++)
		{
			if (G->Edge[i][k] && dis[i] > dis[k] + 1)
			{
				dis[i] = dis[k] + 1;
				queue[++rear] = i;
			}
		}
	}
	for (i = 0; i < G->V; i++)
	{
		if (dis[i] != inf)
			path += dis[i];
		else
		{
			printf("Cc(%d)=0.00\n", n + 1);
			return;
		}
	}
	printf("Cc(%d)=%.2f\n", n + 1, (double)(G->V - 1) / path);
};

int main()
{
	int N, M, K, i, n;
	scanf("%d", &N);
	scanf("%d", &M);
	G = Create(N, M);
	scanf("%d", &K);
	for (i = 0; i < K; i++)
	{
		scanf("%d", &n);
		Cc(n);
	}
	return 0;
}

scanf("%d", &M);
G = Create(N, M);
scanf("%d", &K);
for (i = 0; i < K; i++)
{
scanf("%d", &n);
Cc(n);
}
return 0;
}

Tags: Algorithm Binary tree Graph Theory

Posted on Tue, 30 Nov 2021 12:53:52 -0500 by vB3Dev.Com