Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-319

Handshake Edge Count

Problem

Recover the number of undirected edges from a degree sequence using the handshake lemma.

Each undirected edge contributes 2 to the degree sum, so return total // 2. If the sum is odd the sequence cannot come from any graph, so return -1 instead.

Hint

Sum the degrees, reject odd sums, then divide by two.

Test cases

case 1
in: [[2,2,2]]
expected: 3
case 2
in: [[1,1]]
expected: 1
case 3
in: [[]]
expected: 0
case 4
in: [[3]]
expected: -1
case 5
in: [[0,0,0]]
expected: 0
Starter codePython
def edge_count_from_degrees(degrees):
    # Your code here
    pass

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