コード例 #1
0
def earliest_ancestor(ancestors, starting_node):

    g = Graph()

    for pair in ancestors:
        parent = pair[0]
        child = pair[1]
        g.add_vertex(parent)
        g.add_vertex(child)
        g.add_edge(child, parent)

    s = Stack()
    s.push([starting_node])
    print("starting_node: ", starting_node)
    longest_path_length = 1
    earliest_ancestor = -1

    while s.size() > 0:
        path = s.pop()
        current_node = path[-1]
        print("path: ", path, "current_node: ", current_node)

        if len(path) > longest_path_length:
            longest_path_length = len(path)
            earliest_ancestor = current_node

        neighbors = g.get_neighbors(current_node)

        for ancestor in neighbors:
            path_copy = list(path)
            print("path_copy: ", path_copy)
            path_copy.append(ancestor)
            s.push(path_copy)

    return earliest_ancestor
コード例 #2
0
ファイル: ancestor.py プロジェクト: wvanorder/Graphs
def earliest_ancestor(ancestors, starting_node):
    graph = {}
    stack = Stack()

    stack.push([starting_node])

    longest_path = 1
    ancestor = None
    
    for parent, child in ancestors:
        graph[child] = graph.get(child, [])
        graph[child].append(parent)

    while stack.size() > 0:
        path = stack.pop()
        v = path[-1]
        if len(path) > longest_path:
            longest_path = len(path)
            ancestor = v
        if len(path) == longest_path and ancestor:
            ancestor = v if v < ancestor else ancestor
        for parent in get_parents(graph, v):
            path_copy = path.copy()
            path_copy.append(parent)
            stack.push(path_copy)
    
    return ancestor if ancestor else -1
コード例 #3
0
ファイル: island_count.py プロジェクト: ehickey08/Graphs
def dft(start_x, start_y, visited, matrix):
    s = Stack()
    s.push((start_x, start_y))

    while s.size() > 0:
        v = s.pop()
        x = v[0]
        y = v[1]

        if not visited[y][x]:
            visited[y][x] = True
            for neighbor in get_neighbors(x, y, matrix):
                s.push(neighbor)
    return visited
コード例 #4
0
 def dft(self, starting_vertex):
     """
     Print each vertex in depth-first order
     beginning from starting_vertex.
     """
     visited = set()
     s = Stack()
     s.push(starting_vertex)
     while s.size() > 0:
         v = s.pop()
         if v not in visited:
             visited.add(v)
             print(f'dft print {v}')
             for neighbor in self.vertices[v]:
                 s.push(neighbor)
コード例 #5
0
 def dft(self, starting_vertex):
     """
     Print each vertex in depth-first order
     beginning from starting_vertex.
     """
     stack = Stack()
     visited = set()
     stack.push(starting_vertex)
     while stack.size() > 0:
         node = stack.pop()
         if node not in visited:
             print(node)
             visited.add(node)
             neighbors = self.get_neighbors(node)
             for node in neighbors:
                 stack.push(node)
コード例 #6
0
 def get_dist_parent(self, starting_vertex):
     
     """
     Gets the distant parent
     """
     # Create a stack and push starting vertex
     ss = Stack()
     ss.push([starting_vertex])
     # Create a set of traversed vertices
     visited = set()
     paths = []
     
     dist_parent = -1
     # While stack is not empty:
     while ss.size() > 0:
         # pop the last added vertex
         path = ss.pop()
         # if not visited
         if path[-1] not in visited:
             # DO THE THING!!!!!!!
             paths.append(path)
             
             # mark as visited
             visited.add(path[-1])
             # push all neightbors
             for next_vert in self.get_parents(path[-1]):
                 new_path = list(path)
                 new_path.append(next_vert)
                 ss.push(new_path)
     len_path=[]
     parents=[]
     for path in paths:
         len_path.append(len(path))
     max_len = max(len_path)
     if max_len == 1:
         dist_parent = -1
     else:
         for path in paths:
             if(len(path) == max_len):
                 parents.append(path[-1])
         dist_parent = min(parents)
     print(paths)
     return dist_parent
コード例 #7
0
ファイル: ancestor.py プロジェクト: ehickey08/Graphs
def earliest_ancestor(ancestors, starting_node):
    s = Stack()
    s.push([starting_node])
    longest_path = 1
    ancestor = None
    graph = build_graph(ancestors)
    while s.size() > 0:
        path = s.pop()
        v = path[-1]
        if len(path) > longest_path:
            longest_path = len(path)
            ancestor = v
        if len(path) == longest_path and ancestor:
            ancestor = v if v < ancestor else ancestor
        for p in get_parents(graph, v):
            path_copy = path.copy()
            path_copy.append(p)
            s.push(path_copy)
    return ancestor if ancestor else -1
コード例 #8
0
ファイル: graph.py プロジェクト: qweliant/Graphs
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        stack = Stack()
        visited = set()
        stack.push(starting_vertex)
        while stack.size() > 0:  # pop off the first vertex

            vertex = stack.pop()  # if that vertex has not been visited...

            if vertex not in visited:  # mark it as visited..

                print(vertex)
                visited.add(
                    vertex
                )  # then add all of its neighbors to the top of the stack

                for neighbor in self.vertices[vertex]:
                    stack.push(neighbor)
コード例 #9
0
ファイル: graph.py プロジェクト: qweliant/Graphs
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     s = Stack()
     s.push([starting_vertex])
     visited = set()
     while s.size() > 0:
         path = s.pop()
         v = path[-1]
         if v not in visited:
             if v == destination_vertex:
                 return path
             visited.add(v)
             for next_vert in self.get_neighbors(v):
                 new_path = list(path)
                 new_path.append(next_vert)
                 s.push(new_path)
     return None
コード例 #10
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """
        stack = Stack()
        visited = set()
        stack.push([starting_vertex])

        while stack.size() > 0:
            path = stack.pop()
            last_node = path[-1]
            if last_node == destination_vertex:
                return path
            if last_node not in visited:
                visited.add(last_node)
                neighbors = self.get_neighbors(last_node)
                for node in neighbors:
                    new_path = [*path] + [node]
                    stack.push(new_path)
コード例 #11
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """
        visited = set()
        s = Stack()
        path = [starting_vertex]
        s.push(path)
        while s.size() > 0:
            path = s.pop()
            v = path[-1]
            if v == destination_vertex:
                return (f'DFS shortest path {path}')

            if v not in visited:
                visited.add(v)
                for neighbor in self.vertices[v]:
                    path_copy = path.copy()
                    path_copy.append(neighbor)
                    s.push(path_copy)
コード例 #12
0
    def dft(self, starting_node):
        final_path = [starting_node]
        # create an empty set to store visited nodes
        visited = set()
        # create a empty stack and push starting vertex path
        s = Stack()
        s.push([starting_node])
        # stack not empty, pop FIFO item
        while s.size() > 0:
            path = s.pop()
            v = path[-1]

            # if that vertex has not been visited, marked it as visited
            if v not in visited:
                visited.add(v)
                # add more ancestor to ancestor tree
                if len(final_path) < len(path):
                    final_path = path.copy()
                # child has two ancestors, choose smaller ancestor
                elif len(final_path) == len(
                        path
                ) and final_path > path:  # [8,11] > [8,4], final_path=[8,4]
                    final_path = path.copy()
                # then get all path leading its neighbor onto stack
                for neighbor in self.vertices[v]:
                    # create new path to its neighbor
                    path_copy = path.copy()  # list(path) equivalent
                    # attach vertex neighbor to end of path
                    path_copy.append(neighbor)
                    # push onto stack to be looped
                    s.push(path_copy)

        if final_path == [starting_node]:
            return (-1)
        else:
            return (final_path[-1])  # return earliest ancestor