All problems
Assortativity Coefficient
Problem
Compute the degree assortativity coefficient of an undirected graph: the Pearson correlation of the degrees at the two ends of each edge.
With M edges, let a_e = (d_u + d_v) / 2 and p_e = d_u * d_v; then r = (mean(p) - mean(a)^2) / (mean((d_u^2 + d_v^2) / 2) - mean(a)^2). Return 0.0 when the denominator is 0 or the graph has no edges. edges is a list of [u, v] pairs.
Hint
A negative value means high-degree nodes prefer low-degree neighbors.
Test cases
case 1
in: [4,[[0,1],[0,2],[0,3]]]
expected: -1
case 2
in: [4,[[0,1],[1,2],[2,3]]]
expected: -0.5
case 3
in: [4,[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]]
expected: 0
case 4
in: [3,[[0,1],[1,2]]]
expected: -1
case 5
in: [5,[[0,1],[1,2],[2,3],[3,4],[4,0]]]
expected: 0
Starter codePython
def degree_assortativity(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor