Exemplo n.º 1
0
 def connection_level(self, panda1, panda2):
     if panda1 not in self.panda_list or panda2 not in self.panda_list:
         return False
     else:
         if (bfs(self.panda_list, panda1, panda2) == 0):
             return -1
         else:
             return (bfs(self.panda_list, panda1, panda2))
Exemplo n.º 2
0
 def path_between(self, node_a, node_b):
     if node_a not in self.gr_dict:
         self.gr_dict[node_a] = []
     if bfs(self.gr_dict, node_a, node_b) == 0:
         return False
     else:
         return True
Exemplo n.º 3
0
start_abstract_state, _ = state2topology(start_state,
                                         update_edges=True,
                                         update_faces=True)
end_abstract_state = [
    state2topology(state, update_edges=True, update_faces=False)
    for state in states
]

dataset_abstract_actions = []
dataset_traj_params = []
for i, (astate, intersection) in enumerate(end_abstract_state):
    intersect_points = [i[0]
                        for i in intersection] + [i[1] for i in intersection]
    if len(set(intersect_points)) < len(intersect_points):
        continue
    _, path_action = bfs(start_abstract_state, astate, max_depth=1)
    if len(path_action) == 1:
        dataset_abstract_actions.append(path_action[0])
        dataset_traj_params.append(traj_params[i])


def hash_dict(abstract_action):
    tokens = [k + ':' + str(v) for k, v in abstract_action.items()]
    return ' '.join(sorted(tokens))


dataset = {}
for abstract_action, traj_param in zip(dataset_abstract_actions,
                                       dataset_traj_params):
    abstract_action_str = hash_dict(abstract_action)
    classified = False
Exemplo n.º 4
0
from time import time
from BFS import bfs
from DFS import dfs
from node import Node

initial_state = [3, 3, 0]

Node.num_of_instances = 0
t0 = time()
solution = dfs(initial_state)
t1 = time() - t0
print('Solution:', solution)
print('space:', Node.num_of_instances)
print('time:', t1, 'seconds')

Node.num_of_instances = 0
t0 = time()
solution = bfs(initial_state)
t1 = time() - t0
print('Solution:', solution)
print('space:', Node.num_of_instances)
print('time:', t1, 'seconds')
Exemplo n.º 5
0
edges = open("random_edges.txt").readlines()
for i in range(0, len(edges)):
    edges[i] = edges[i].replace('\n', '').split(' ', 1)

for e in edges:
    graph.add_edge(e[0], e[1])
#draw_graph(graph, edges)
V = graph.get_nodes()

# +++ COMPUTE EXACT AVERAGE DISTANCE +++
# 1. compute all pair vertices distances
matrix_dist = {node: dict() for node in V}
diameter = 0
for k in range(0, len(V)):
    node = V[k]
    [tree, predecessor] = bfs(node, graph)
    #draw_tree(tree, './grafiPDF/BFS_' + str(node))
    count_distance(matrix_dist[node], predecessor, V)
    # compute diameter so far
    d = max(matrix_dist[node].items(), key=operator.itemgetter(1))[1]
    if diameter < d:
        diameter = d
print_matrix(matrix_dist)
print('diameter = ' + str(diameter))
# 2. compute average distance
real_average_distance = sum_all_distances(matrix_dist) / (len(V) *
                                                          (len(V) - 1))
print("Real average distance: " + str(real_average_distance), end='\n\n')

num_iter = 30
results = [0] * num_iter
from BFS import bfs, print_path

t = []
with open("input.txt", "r") as inf:
    v_n, e_v = [int(x) for x in inf.readline().split()]
    g = [[] for i in range(v_n)]
    for i in range(e_v):
        t = [int(x) for x in inf.readline().split()]
        g[t[0]].append(t[1])
        t[0], t[1] = t[1], t[0]
        g[t[0]].append(t[1])

    for i, j in enumerate(g):
        print(i, "--", *j)
    print(g)

p, d = bfs(g, v_n, 0)

print_path(0, 2, p)
print()
print(d)
Exemplo n.º 7
0
from tree import root, Node
from BFS import bfs


def insert(root, key):
    queue = []
    queue.append(root)

    while len(queue) > 0:
        temp = queue.pop(0)

        if temp.left is None:
            temp.left = Node(key)
            return

        else:
            queue.append(temp.left)

        if temp.right is None:
            temp.right = Node(key)
            return
        else:
            queue.append(temp.right)


if __name__ == "__main__":
    insert(root, 10)
    insert(root, 11)
    bfs(root)
Exemplo n.º 8
0
from Gulosa import Gulosa
from A_Estrela import A_Estrela
from puzzle import Puzzle
from puzzle2 import puzzle2

#Posicao inicial do puzzle8
inicio=[[2, 6, 1,
        7, 5, 3,
        0, 8, 4]]
#posicao final   [1,2,3,
#                8,0,4,
#                7,6,5]

for i in range(0,1):
    t0=time()
    bfs_met=bfs(inicio[i])
    t1=time()-t0
    print('Caminho do BFS até a resposta:', bfs_met)
    print('Tempo Gasto pelo BFS:',t1,"\n")

    t0=time()
    AlgGuloso = Gulosa(inicio[i])
    t1=time()-t0
    t1= t1 + 0.01
    print('Caminho do Algoritmo Guloso até a resposta:',AlgGuloso)
    print('Tempo Gasto:',t1,"\n")

    t0 = time()
    aEstrela = A_Estrela(inicio[i])
    t1 = time() - t0
    print('caminho do Algoritmo A* até a resposta:',aEstrela)
Exemplo n.º 9
0
from Node import Node
from BFS import bfs

node1 = Node("A")
node2 = Node("B")
node3 = Node("C")
node4 = Node("D")
node5 = Node("E")
node6 = Node("F")

node1.neighbourList.append(node2)
node1.neighbourList.append(node3)
node2.neighbourList.append(node4)
node2.neighbourList.append(node5)
node3.neighbourList.append(node6)

bfs(node1)