
function parent = canonize(parent, f)
    [_, s] = sort(f(:), "descend");
    [_, R] = intersect(s, [1:numel(f)]);

    for p = fliplr(s')
        q = parent(p);
        
        if f(parent(q)) == f(q)
            parent(p) = parent(q);
        end
    end
    
    parent = reshape(parent, size(f));
end
