Calculate the complexity of this code
class Digraph:
    def __init__(self):
        self.edges = {}

    def add_vertex(self, v):
        if v not in self.edges:
            self.edges[v] = set()

    def add_edge(self, v, w):
        self.add_vertex(v)
        self.add_vertex(w)
        self.edges[v].add(w)

    def adj(self, v):
        return self.edges.get(v, set())

    def vertices(self):
        return self.edges.keys()


class SAP:
    def __init__(self, graph):
        self.graph = graph

    def _bfs(self, sources_v, sources_w):
        dist_v = {v: float('inf') for v in self.graph.vertices()}
        dist_w = {v: float('inf') for v in self.graph.vertices()}
        queue = []

        for s in sources_v:
            if s in dist_v:
                dist_v[s] = 0
                queue.append((s, 'v'))
        for s in sources_w:
            if s in dist_w:
                dist_w[s] = 0
                queue.append((s, 'w'))

        shortest = float('inf')
        ancestor = -1

        while queue:
            v, source = queue.pop(0)
            current_dist = dist_v if source == 'v' else dist_w
            opposite_dist = dist_w if source == 'v' else dist_v

            if opposite_dist[v] != float('inf'):
                total_dist = current_dist[v] + opposite_dist[v]
                if total_dist < shortest:
                    shortest = total_dist
                    ancestor = v

            for w in self.graph.adj(v):
                if current_dist[w] == float('inf'):
                    current_dist[w] = current_dist[v] + 1
                    queue.append((w, source))

        return shortest, ancestor

    def length(self, v, w):
        shortest, _ = self._bfs([v], [w])
        return -1 if shortest == float('inf') else shortest

    def ancestor(self, v, w):
        _, ancestor = self._bfs([v], [w])
        return ancestor

    def length_in_list(self, v_list, w_list):
        shortest, _ = self._bfs(v_list, w_list)
        return -1 if shortest == float('inf') else shortest

    def ancestor_in_list(self, v_list, w_list):
        _, ancestor = self._bfs(v_list, w_list)
        return ancestor


class WordNet:
    def __init__(self, synsets_file, hypernyms_file):
        self.synsets = {}
        self.nouns_map = {}
        self.graph = Digraph()

        with open(synsets_file, 'r') as file:
            for line in file:
                fields = line.strip().split(',')
                synset_id = int(fields[0])
                synset = fields[1]
                # description = fields[2]

                self.synsets[synset_id] = synset
                self.graph.add_vertex(synset_id)

                for noun in synset.split():
                    if noun not in self.nouns_map:
                        self.nouns_map[noun] = set()
                    self.nouns_map[noun].add(synset_id)

        with open(hypernyms_file, 'r') as file:
            for line in file:
                fields = line.strip().split(',')
                synset_id = int(fields[0])
                hypernyms = map(int, fields[1:])
                for hypernym_id in hypernyms:
                    self.graph.add_edge(synset_id, hypernym_id)

        self.sap = SAP(self.graph)

    def nouns(self):
        return list(self.nouns_map.keys())

    def is_noun(self, word):
        return word in self.nouns_map

    def distance(self, nounA, nounB):
        if nounA not in self.nouns_map or nounB not in self.nouns_map:
            raise ValueError("One or both nouns are not in WordNet")

        synsetsA = self.nouns_map[nounA]
        synsetsB = self.nouns_map[nounB]

        return self.sap.length_in_list(list(synsetsA), list(synsetsB))

    def sap_f(self, nounA, nounB):
        if nounA not in self.nouns_map or nounB not in self.nouns_map:
            raise ValueError("One or both nouns are not in WordNet")

        synsetsA = self.nouns_map[nounA]
        synsetsB = self.nouns_map[nounB]

        ancestor_id = self.sap.ancestor_in_list(list(synsetsA), list(synsetsB))
        return self.synsets[ancestor_id] if ancestor_id != -1 else None


class Outcast:
    def __init__(self, wordnet):
        self.wordnet = wordnet

    def outcast(self, nouns):
        max_distance = -1
        outcast = None

        for noun in nouns:
            if not self.wordnet.is_noun(noun):
                raise ValueError(f"{noun} is not in WordNet")

            distance_sum = sum(self.wordnet.distance(noun, other) for other in nouns if other != noun)

            if distance_sum > max_distance:
                max_distance = distance_sum
                outcast = noun

        return outcast


def main():
    wordnet = WordNet("synsets.txt", "hypernyms.txt")
    outcast = Outcast(wordnet)
    nouns = ["hood", "1530s", "neighborhood", "decongestant"]
    print(f"Outcast: {outcast.outcast(nouns)}")

    print(f"Nouns: {wordnet.nouns}")

    check = wordnet.is_noun("23531542352352")
    print(f"Is noun: {check}")

    similarity = wordnet.distance("car", "vehicle")
    print(f"Similarity: {similarity}")

    common_ancestor = wordnet.sap_f("dog", "wolf")
    print(f"Common ancestor: {common_ancestor}")


main()