示例#1
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        print(starting_vertex)
        #creating a set to store visited nodes
        visited = set()


        queue = Queue()

        #load the starting vertex into the que
        queue.enqueue(starting_vertex)
        #while the queue has a length
        while queue.size() > 0:
            #remove the first node put in the list and set it to current node
            #print(f'queue before pop: {queue.queue}')
            currNode = queue.popleft()
            #m,print(f'queue after pop: {queue.queue}')
            #if the node has not been visited
            if currNode not in visited:
                #add the node to visited

                print(currNode)
                visited.add(currNode)
                #check the neighbors of the current node
                for neighbor in self.get_neighbors(currNode):
                    #if they are havent been visited add them to the path, and put them in the queue
                    if neighbor not in visited:
                        queue.enqueue(neighbor)
示例#2
0
def earliest_ancestor(ancestors, starting_node):
    # create a dictionary
    struct = {}

    # Loop through each pair in the ancestor list
    for pair in ancestors:
        # Parent is the first value of pair
        parent = pair[0]
        # Child is the second value of pair
        child = pair[1]

        # If Child is not in 'struct', add it
        if child not in struct:
            struct[child] = []

        # Then add the parents of each child to their list in the dict
        struct[child].append(parent)

    # Create a queue
    q = Queue()

    # Append the starting node
    q.enqueue([starting_node])

    # Set a default longest node to store the largest distance
    # index 0 and the last node - as it corresponds with the longest distance
    longest_node = [0, -1]

    # Check if the queue is empty
    while len(q) > 0:
        # pop the first path from the queue
        curr_path = q.popleft()

        # get the last node in the path
        last_node = curr_path[-1]

        # Check if the last node is in the dictionary
        if last_node not in struct:
            # if the curr_path is longer than the previous_path, that will be the new longest path
            if len(curr_path
                   ) > longest_node[0] and last_node > longest_node[1]:
                # store the new longest_node
                longest_node = [len(curr_path), last_node]

        # else, if the last_node does have parents
        else:
            # loop through each parent and queue up
            # a path from the current path to each parent
            for x in struct[last_node]:
                q.append(curr_path + [x])

        # if the longest_node is = starting_node, return -1 as the starting_node has no parents
        if longest_node[1] == starting_node:
            return -1

        # Else return the node furthest away, which is longest_node[1]
        else:
            return longest_node[1]