Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-211

WL Kernel Similarity

Problem

Compute the normalized Weisfeiler-Lehman kernel similarity between two color histograms.

labels_a and labels_b are color lists produced by WL refinement. Build color-count dictionaries, take their dot product, and divide by the product of their L2 norms. Return 0.0 when either list is empty or has zero norm.

Hint

Normalized kernels let graphs of different sizes be compared fairly.

Test cases

case 1
in: [["a","b","a"],["a","b","a"]]
expected: 1
case 2
in: [["a","b"],["a","c"]]
expected: 0.5
case 3
in: [["a","a","b"],["a","b","b"]]
expected: 0.8
case 4
in: [[],[]]
expected: 0
Starter codePython
import math
def wl_kernel_similarity(labels_a, labels_b):
    # Your code here
    pass

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