Beispiel #1
0
def basic_dfs(graph, startNode, goalNode):
    """
    Performs a depth-first search on a graph from a specified start
    node to a specified goal node, returning a path-to-goal if it
    exists, otherwise returning None.
    Uses backtracking, but does not use an extended set.
    """
    return generic_search()(graph, startNode, goalNode)
    visited, stack = set(), [startNode]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            if vertex is goalNode:
                return make_generic_search(extensions,
                                           has_loops)()(graph, startNode,
                                                        goalNode)
            visited.add(vertex)
            for n in graph.get_neighbors(vertex):
                if n not in visited:
                    stack.append(n)
    return None
Beispiel #2
0
Datei: lab2.py Projekt: cookt/mit
    ext=[]
    neighbors=graph.get_neighbors(path[len(path)-1])
    for n in neighbors:
        if not(n in path):
            copy=path[:]
            copy.append(n)
            ext.append(copy)
    return ext
        
def sort_by_heuristic(graph, goalNode, nodes):
    return sorted(sorted(nodes),key=lambda n: graph.get_heuristic_value(n, goalNode))


# You can ignore the following line.  It allows generic_search (PART 3) to 
# access the extensions and has_loops functions that you defined in PART 1.
generic_search = make_generic_search(extensions, has_loops)  # DO NOT CHANGE

#### PART 2: Search Algorithms #########################################

# Note: Optionally, you may skip to Part 3: Generic Search,
# then complete Part 2 using your answers from Part 3.

def dfs(graph, startNode, goalNode):
    return generic_search(*generic_dfs)(graph, startNode, goalNode)


def bfs(graph, startNode, goalNode):
    return generic_search(*generic_bfs)(graph, startNode, goalNode)

def hill_climbing(graph, startNode, goalNode):
    return generic_search(*generic_hill_climbing)(graph, startNode, goalNode)
Beispiel #3
0
        for k in path:
            j.append(k)
        j.append(i)
        if not has_loops(j):
            n.append(j)
    return break_ties(n)


def sort_by_heuristic(graph, goalNode, nodes):
    k = sorted(nodes)
    return sorted(k, key=lambda x: graph.get_heuristic_value(x, goalNode))


# You can ignore the following line.  It allows generic_search (PART 3) to
# access the extensions and has_loops functions that you defined in PART 1.
generic_search = make_generic_search(extensions, has_loops)  # DO NOT CHANGE

#### PART 2: Search Algorithms #########################################

# Note: Optionally, you may skip to Part 3: Generic Search,
# then complete Part 2 using your answers from Part 3.


def dfs(graph, startNode, goalNode):
    return generic_search(*generic_dfs)(graph, startNode, goalNode)


def bfs(graph, startNode, goalNode):
    return generic_search(*generic_bfs)(graph, startNode, goalNode)
    '''agenda = [[startNode]]
    notfound = True
Beispiel #4
0
        temp_path.append(edge.endNode)
        if not has_loops(temp_path): 
            new_paths.append(temp_path)
    return sorted(new_paths)


def sort_by_heuristic(graph, goalNode, nodes):


    temp_nodes = nodes[:]
    temp_nodes = sorted(nodes)
    return sorted(temp_nodes, key = lambda node: graph.get_heuristic_value(node, goalNode))



generic_search = make_generic_search(extensions, has_loops)

#### PART 2: Search Algorithms #########################################


def dfs(graph, startNode, goalNode):

    paths = [[startNode]]
    
    while True:
        if paths[0][-1] == goalNode:
            return paths[0]
        extended_path = extensions(graph, paths[0])
        paths[1:1] = extended_path
        if len(paths) >= 2:
            del(paths[0])