Я внедрил Breadth First Search. Матрица смежности (adj [] []) представляет взаимосвязь между различными узлами, а узлы хранятся в узлах [].
Однако я не получаю требуемого обхода. Пожалуйста, помогите мне. Ниже мой код. Синтаксических ошибок нет. Вероятно, это логическая ошибка, которую я не могу найти при отладке.
insert(nodes[0]);
nos = nos + 1;
visited[nos] = nodes[0];
while(front != -1)
{
char ch = remove();
for(int i=0;i<n;i++)
{
if(ch == nodes[i])
{
pos = i;
}
}
for(int j=0;j<n;j++)
{
if(adj[j][pos] == 1)
{
for(int k=0;k<=nos;k++)
{
if(visited[k] == nodes[j])
{
goto end;
}
}
nos = nos + 1;
visited[nos] = nodes[j];
insert(nodes[j]);
end:continue;
}
}
}
Попробуйте это. Кажется, это работает для меня
def bfs(graph, start, goal):
if start == goal:
return None
frontier = [start]
explored = []
num_explored = 0
while len(frontier) > 0:
node = frontier.pop(0)
explored.append(node)
for edge in networkx.edges(graph, node):
child = State(graph, node)
if (child not in explored) and (child not in frontier):
if child == goal:
print "Path found in BFS"
return child
else:
frontier.append(child)
num_explored = num_explored + 1
print "BFS path failed"
return None