All problems
Degree Centrality
Problem
Compute the degree centrality of every node in an undirected graph: degree(v) / (n - 1).
Return a list of floats in node order. When n is less than 2, every node has centrality 0.0.
Hint
Count each node's incident edges, then divide by the maximum possible degree.
Test cases
case 1
in: [4,[[0,1],[0,2],[0,3]]]
expected: [1,0.3333333333333333,0.3333333333333333,0.3333333333333333]
case 2
in: [3,[[0,1],[1,2],[0,2]]]
expected: [1,1,1]
case 3
in: [1,[]]
expected: [0]
case 4
in: [3,[[0,1],[1,2]]]
expected: [0.5,1,0.5]
Starter codePython
def degree_centrality(n, edges):
# Your code here
pass4 test cases · run your code against them in the editor