BESSER icon indicating copy to clipboard operation
BESSER copied to clipboard

Better sorting algorithm for classes_sorted_by_inheritance

Open FChikh opened this issue 2 years ago • 0 comments

As I've noticed, now the average time complexity for DomainModel.classes_sorted_by_inheritance() is O(|classes|^2), as there are two nested loops which iterates over classes' set. It can be improved with implementation of Topological Sorting algorithm with asymptotic of O(|classes| + |generalizations|), so it can be assumed as linear time, if the number of generalizations is proportional to the number of classes. I haven't tried yet to debug it, but here is how it approximately should look like:

def classes_sorted_by_inheritance(classes):
    # Set up a dependency graph
    child_map = {cl: set() for cl in classes}

    # Populating the child_map based on generalizations (edges in top-sort graph)
    for cl in classes:
        for generalization in cl.generalizations:
            child_map[generalization.general].add(cl)

    # Helper function for DFS
    def dfs(cl, visited, sorted_list):
        visited.add(cl)
        for child in child_map[cl]:
            if child not in visited:
                dfs(child, visited, sorted_list)
        sorted_list.append(cl)

    # Perform DFS from each node that hasn't been visited yet
    visited = set()
    sorted_list = []
    for cl in classes:
        if cl not in visited:
            dfs(cl, visited, sorted_list)

    # The classes are sorted from most derived to least derived, so reverse the list
    sorted_list.reverse()
    return sorted_list

As I told Ivan, I can contribute on this matter as well.

FChikh avatar Nov 22 '23 12:11 FChikh