Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-138

Expansion of a Set

Problem

Compute the edge expansion of a vertex subset S of an undirected graph: the number of edges crossing from S to its complement, divided by |S|.

Return 0.0 when S is empty or contains every node. edges is a list of [u, v] pairs and subset lists the nodes in S.

Hint

Small sets with many boundary edges are poor clusters.

Test cases

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

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