[Graph Theory] C_calculation_social network (Floyd counts the number of shortest paths)

1. Title Description

In the research of social networks, we often use the concept of graph theory to explain some social phenomena.

Let's look at such a problem.

There are n people in a social circle, and there are different degrees of relationships between people.

We map this relationship network to an undirected graph of n nodes. If two different people know each other, connect an undirected edge between their corresponding nodes and attach a positive weight. The smaller c and c, the closer the relationship between two people.

We can use the shortest length between the corresponding nodes to measure the closeness of the relationship between the two people s and t. Note that the other nodes on the shortest path provide some convenience for the connection between s and t, that is, these nodes Point has a certain degree of importance for the connection between s and t.

We can measure the importance of a node in a social network by counting the number of shortest paths through a node v.

Consider that there may be multiple shortest paths between two nodes A and B.

We modify the definition of the importance level as follows: Let Cs, t represent the number of different shortest paths from s to t, and Cs, t (v) represent the number of shortest paths from s to t after v

It is defined
Insert picture description here
as the importance of node v in social networks.

In order to make I (v) and Cs, t (v) meaningful, we stipulate that the social networks to be processed are all connected undirected graphs, that is, any two nodes have a shortest path of finite length.

Now given such a weighted undirected graph describing social networks, please find out the importance of each node.

Input format

The first line of the input has two integers n and m, which represent the number of nodes and undirected edges in the social network.

In the undirected graph, we number all nodes from 1 to n.

In the next m lines, each line uses three integers a, b, and c to describe an undirected edge connecting nodes a and b with a weight of c.

Note that there is at most one undirected edge connected between any two nodes, and no self-loop will appear in the undirected graph (that is, there is no undirected edge whose two endpoints are the same node).

In all data, it is guaranteed that the given undirected graph is connected, and the number of shortest paths between any two nodes does not exceed 1010.

Output format

The output includes n lines, one real number per line, accurate to 3 decimal places.
The real number in line i indicates the importance of node i in the social network.

Data range
n≤100, m≤4500, 1≤c≤1000

输入样例:
4 4
1 2 1
2 3 1
3 4 1
4 1 1
输出样例:
1.000
1.000
1.000
1.000

Second, the solution

Method 1: Floyd

  • When d[i][j] > d[i][k] + d[k][j], we think from i to j does not pass through the shortest number of k is the shortest and after several pieces of the same k.
  • When d[i][j] = d[i][k] + d[k][j], we think from i to j does not pass through the shortest number of k should be added after several shortest strip k.

Q&A:

  • Q1: Why did the last big sample get the answer WA?
    A1: The number of shortest paths does not exceed 1 0 10 10^{10} , that is, if there is a multiplication ×, then the calculation result will become very large. If you define INF as 0x3f, then it is easy to exceed, so when multiplication × appears, you must open the constants that are usually defined.
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static int V, E;
	static double[][] d,c;
	static int INF = (int)1e20;	//注
	static int maxv = 100 + 50, maxe = 4500 + 50;
	static void floyd() {
		for (int k = 1; k <= V; k++)
		for (int i = 1; i <= V; i++)
		for (int j = 1; j <= V; j++) {
			if(k == i || i == j || k == j)
				continue;
			if (d[i][j] > d[i][k] + d[k][j]) {
				d[i][j] = d[i][k] + d[k][j];
				c[i][j] = c[i][k] * c[k][j];
			} else if (d[i][j] == d[i][k] + d[k][j]) {
				c[i][j] += c[i][k] * c[k][j];
			}
		}
	}
    public static void main(String[] args) throws IOException {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
		V = sc.nextInt();
		E = sc.nextInt();
        c = new double[maxv][maxv];		
        d = new double[maxv][maxv];	
		
        for (int i = 1; i <= V; i++)
		for (int j = 1; j <= V; j++) {
		    if (i != j) {
		        d[i][j] = INF;
		    }
		}
		
		for (int i = 0; i < E; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			double w = sc.nextDouble();
			d[a][b] = d[b][a] = w;
			c[a][b] = c[b][a] = 1;
		}
		floyd();
		double[] imp = new double[V+1];
		for (int i = 1; i <= V; i++)
		for (int j = 1; j <= V; j++)
		for (int v = 1; v <= V; v++) {
		    if (i == j || j == v || i == v)
				continue;
		    if (d[i][j] == d[i][v] + d[v][j])
				imp[v] += (c[i][v]*c[v][j]) / c[i][j];
		}
	    for (int i = 1; i <= V; i++) {
			System.out.printf("%.3f\n", imp[i]);
		}
    }
}

Complexity analysis

  • time complexity: O ( V 3 ) O (V ^ 3)
  • Space complexity: O ( V 3 ) O (V ^ 3)
Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105590700