コード例 #1
0
def earliest_ancestor(ancestors, starting_node):
    ancestorTree = Graph()
    # Iterate through ancestors to find vertices
    # for (parent,child) in ancestors
    for ancestor in ancestors:
        for vert in ancestor:
            ancestorTree.add_vertex(vert)
    # print("Tree", ancestorTree.vertices)

    for ancestor in ancestors:
        ancestorTree.add_edge(ancestor[1], ancestor[0])
    # print("Tree", ancestorTree.vertices)

    # default length to check the length of path list against
    longest_path = 1
    # counter for storing storing the last node
    last_node = 0
    # passing the vertices by reference
    ancestor_vert = ancestorTree.vertices

    # Iterate through the vertices of the ancestorTree
    for i in ancestor_vert:
        # i = individual nodes/vertices added using add_vert()
        # returns a list of nodes and sets the list to the variable path
        path = ancestorTree.dfs(starting_node, i)
        # print("path list loop", path)

        # If path is not = to None and the length of the path list in greater that longest_path which defaults to the value integer 1
        if path is not None and len(path) > longest_path:
            # set longest_path = length of the path
            longest_path = len(path)
            print("longest_path", longest_path)
            # last node is = to last node/vertice of the longest_path
            last_node = i
            print("last_node", last_node)
        # I was missing that longest_path defaults to 1.
        # If path list is empty and longest_path is set to default of 1
        elif not path and longest_path == 1:
            # print("empty", path)
            # print("elif path", longest_path)
            # set last_node to -1
            last_node = -1

    # print("Out of for loop", last_node)
    return last_node
コード例 #2
0
def play():
    # create an array of visited rooms to keep track of
    visited = set()
    graph = Graph()
    # the path we take in that one run of the game
    trav_path = []
    # using dfs to find the rooms - find all rooms on possible branch
    d_rooms = graph.dfs(player.current_room)
    # returns and array of the rooms in d_rooms
    rooms = [d_room for d_room in d_rooms]
    # while total visited is less than the total rooms in graph of world - 1
    while (len(visited) < len(room_graph) - 1):
        # current room is the first room in the rooms array
        curr_room = rooms[0]
        # the next traverse will be to the next item in the rooms array
        next_room = rooms[1]

        # use a bfs to find the shortest path to destination - explores all the neighbour nodes
        shortest = graph.bfs(curr_room, next_room)
        # loop through shortest until nothing left
        while len(shortest) > 1:
            # find the neighbours
            curr_room_nays = d_rooms[shortest[0]]
            # next traverse will be to the next item in shortest array
            next_room = shortest[1]
            # if the next room (in the shortest path) exists in the current neighbours of curr room
            if next_room in curr_room_nays:
                trav_path.append(curr_room_nays[next_room])
            # remove the first room from the queue
            shortest.remove(shortest[0])

        # remove the current room from the total rooms
        rooms.remove(curr_room)
        # pop it on to the visited rooms array
        visited.add(curr_room)

    return trav_path
コード例 #3
0
ファイル: ancestor.py プロジェクト: taniamichelle/Graphs
def earliest_ancestor(ancestors, starting_node):
    ancestor_tree = Graph()
    # Iterate through ancestors
    for (parent, child) in ancestors:
        # Add vertices to ancestor_tree
        ancestor_tree.add_vertex(parent)
        ancestor_tree.add_vertex(child)
    # print("ancestor tree", ancestor_tree.vertices)

    for (parent, child) in ancestors:
        # Add edges
        ancestor_tree.add_edge(child, parent)
    # print("neighbors", ancestor_tree.get_neighbors(5))
    # print("ancestor tree", ancestor_tree.vertices)

    longest_path = 1  # Keep track of # ancestors; highest means most ancestors
    earliest_ancestor = 0  # Store last node (as an integer)
    for i in ancestor_tree.vertices:
        # print("i", i)  # Print vertices
        # Call dfs function from Graph class
        path = ancestor_tree.dfs(
            i, starting_node)  # i is each vertex/node in graph
        # print("ancestor dfs", ancestor_tree.dfs(starting_node, i))
        print('path', path)
        if path:  # If there are items in list
            if len(
                    path
            ) > longest_path:  # If list length is greater than longest path
                longest_path = len(
                    path)  # Set longest path equal to list length
                earliest_ancestor = i  # Set earliest_ancestor equal to current node/vertex
        elif not path and longest_path == 1:  # If path is 'None' and 'longest_path' is our default of 1
            earliest_ancestor = -1

    print("earliest ancestor", earliest_ancestor)
    return earliest_ancestor
コード例 #4
0
data = get(end['init'])

gr = Graph()
gr.add_vertex(data)
# print(gr.rooms)
# map_data = {}

# map_data[data['room_id']] = data

# print("map_data", map_data)
"""
{room_id: {title: "foo", terrain: "bar"}}
"""
visited = set()
while True:
    dfs = gr.dfs(data)
    curr_room = gr.rooms[dfs[-1]]
    for room_id in dfs:
        visited.add(room_id)
    if len(visited) >= 500:
        break
    print('visited rooms ---->', visited)
    unexplored_dirs = gr.get_unexplored_dir(curr_room)
    data = curr_room
    if not len(unexplored_dirs):
        data = gr.backtrack_to_unex(curr_room)
    print("current room ---->", data)
"""
After traversal
"""