Ejemplo n.º 1
0
    G.add_edges_from(edges)

    # You must change here if there is a different cost per edge
    CostperEdge = [1 for i in range(len(edges))]

    # Adding the respective cost for each edge in the graph
    for u, v in edges:
        G.add_weighted_edges_from([(u, v, CostperEdge[edges.index((u, v))])])

    positions = plotGraph(G, 1, None)

    # Creating an problem object based on FindPath class
    Problema = FindPath(G)

    # Creating an object for breadth first search algorithm for ``FindPath`` problem
    SearchObj = breadth_first_search(Problema)

    start = 'Pouso Alegre'
    target = 'Campinas'
    print('\nSearching %s starting from %s...' % (target, start))
    solution, path, path_edges = SearchObj.search(start, target)
    print('Done!\n')
    if solution:
        print('Path found!')
        printPath(path, start)
        for u, v in edges:
            if (u, v) not in path_edges:
                G.remove_edge(u, v)
        plotGraph(G, 1, positions)
    else:
        print('Path not found!')
Ejemplo n.º 2
0
from SearchAlgorithms.data_structures import Failure
from problem1 import problem as pr
from SearchAlgorithms import breadth_first_search as b

p = pr.Problem()

sol = b.breadth_first_search(p)
if isinstance(sol, Failure):
    print('BFS failed.')
else:
    print('BFS:')
    print("visited = {} , expanded = {} , max_memory = {} , path_cost = {}".
          format(sol.visited, sol.expanded, sol.max_memory, sol.path_cost))
    print('Path:')
    for node in sol.best_path:
        print(node.state, end='')
        print(' , ', end=' ')