def split_mapping_by_keys(mapping, key_lists):
    """Split up a mapping (dict) according to connected components

    Each connected component is a sequence of keys from mapping;
    returned is a corresponding sequence of head mappings, each with
    only the keys from that connected component.
    """
    mappings = []
    for seq in key_lists:
        mappings.append(dict((head_id, value) for head_id, value
                             in list(mapping.items()) if head_id in seq))
    return mappings