Social network diagram nodes "importance" is calculated (30) Java Solution

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 N nodes in the network, the node v i of "closeness centrality" Cc (v i ) is mathematically defined as v i to rest all nodes v to J (J ≠ i) the shortest distance D (V I , V J reciprocal of the mean) of:
Here Insert Picture Description
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 (≦ 10 . 4 ) is the number of nodes in the FIG., The way is assumed that the node number from 1 to N; M (≦ 10 . 5 ) is a side several. 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 of nodes K number (≦ 100), and K th node numbers, separated by spaces.

Output formats:

Cc according to the output (i) = x.xx format of K given node degree centrality tight, each output per line, the retention result decimal two.

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 The
cc (4) = 0.62
cc (9) = 0:35

import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int vertex = sc.nextInt();
        int edge = sc.nextInt();
        Graph graph = new Graph(vertex, edge);
        for (int i = 0; i < edge; i++) {
            int temp1 = sc.nextInt();
            int temp2 = sc.nextInt();
            graph.Insert(temp1 - 1, temp2 - 1);
        }
        int num = sc.nextInt();
        for (int i = 0; i < num; i++) {
            int n = sc.nextInt();
            System.out.format("Cc(%d)=%.2f\n",n,graph.Importance(n-1));
        }
    }

}

class Graph {
    private int Vertex;
    private int Edge;
    private int[][] G;

    Graph(int v, int e) {
        Vertex = v;
        Edge = e;
        G = new int[v][v];
    }

    void Insert(int v1, int v2) {
        G[v1][v2] = 1;
        G[v2][v1] = 1;
    }

    double Importance(int v) {
        int[] distance = new int[Vertex];
        for (int i = 0; i < Vertex; i++) {
            distance[i] = Integer.MAX_VALUE;
        }
        distance[v] = 0;
        int[][] Visited = new int[Vertex][Vertex];
        LinkedBlockingDeque<Integer> q = new LinkedBlockingDeque<Integer>();
        q.push(v);
        while(!q.isEmpty()){
            int temp = q.poll();
            for(int i=0;i<Vertex;i++){
                if(G[temp][i]==1&&Visited[temp][i]==0){
                    if(distance[i]>distance[temp]+1)
                        distance[i] = distance[temp]+1;
                    q.add(i);
                    Visited[temp][i] = 1;
                    Visited[i][temp] = 1;
                }
            }
        }
        double sum = 0;
        for (int i = 0; i < Vertex; i++) {
            if (distance[i] != Integer.MAX_VALUE) {
                sum += distance[i];
            }
        }
        return (Vertex - 1.0) / sum;
    }
}

Guess you like

Origin www.cnblogs.com/nonlinearthink/p/10956625.html