Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-040

Betweenness Centrality (Brandes-lite)

Problem

Compute the (unnormalized) betweenness centrality of every node in an undirected unweighted graph.

For each ordered source, accumulate dependencies with Brandes' algorithm: when a shortest path has sigma[w] prefixes reaching w and sigma[v] via v, node v receives (sigma[v] / sigma[w]) * (1 + delta[w]). Sum over all sources and divide by 2 for the undirected pairs. Return floats in node order.

Hint

Accumulate dependency contributions while unwinding the BFS stack, deepest nodes first.

Test cases

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

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