Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-255

Contraction Hierarchy Order Lite

Problem

Pick the next node to contract in a contraction hierarchy by minimum edge difference.

The edge difference of a node is the number of neighbor pairs that are not yet connected minus its degree. Compute it for every node and return [node, difference] for the minimum (ties by smallest index). edges is a list of [u, v] pairs.

Hint

Cheap contractions add few shortcuts, keeping the hierarchy small.

Test cases

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

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