Ejemplo n.º 1
0
    def dft(self, starting_vert):

        stack = Stack()
        stack.add_to_tail(starting_vert)
        visited = set()

        while stack.len() > 0:
            cur_vert = stack.remove_from_tail()
            if cur_vert in visited:
                pass
            else:
                visited.add(cur_vert)
                print(f'cur_vert: {cur_vert}')

                for vert in self.vertices[cur_vert]:
                    if vert is not None:
                        if not vert in visited:
                            stack.add_to_tail(vert)
Ejemplo n.º 2
0
    def dfs(self, starting_vert, target_vert):
        paths = Stack()
        paths.add_to_tail([starting_vert])
        visited = set()

        while paths.len() > 0:
            cur_path = paths.remove_from_tail()
            cur_vert = cur_path[-1]
            visited.add(cur_vert)

            if cur_vert is target_vert:
                return cur_path
            else:
                for vert in self.vertices[cur_vert]:
                    if vert is not None:
                        if not vert in visited:
                            new_path = list(cur_path)
                            new_path.append(vert)
                            paths.add_to_tail(new_path)

        return None