Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-012

Clustering Coefficient (Local)

Problem

Compute the local clustering coefficient of every node in an undirected simple graph.

For a node with degree k < 2 the coefficient is 0.0; otherwise it is 2 * L / (k * (k - 1)), where L is the number of edges among the node's neighbors. Return a list of floats in node order.

Hint

Count the edges inside the neighbor set, then normalize by the number of possible pairs.

Test cases

case 1
in: [4,[[0,1],[1,2],[2,0],[2,3]]]
expected: [1,1,0.3333333333333333,0]
case 2
in: [3,[[0,1],[1,2]]]
expected: [0,0,0]
case 3
in: [2,[[0,1]]]
expected: [0,0]
case 4
in: [1,[]]
expected: [0]
Starter codePython
def clustering_coefficients(n, edges):
    # Your code here
    pass

4 test cases · run your code against them in the editor