0

Писал алгоритм поиска в ширину по графам
Нужно было остановить функцию, когда vertex == f, всячески пытался остановить рекурсию никак не получается

def BFS(filename,vertex):
  sosed = neighbour(filename,vertex)
  f = finish(filename)
  if vertex in closed:
      return
  closed.add(vertex)
  bfs.append(vertex)
  for i in sosed:
      if not i in closed:
        opened.append(i)
  while opened:                 
    BFS(filename,opened.pop(0))
0

1 ответ 1

1

Примерно так:

 def bfs(vertex, target):
    q = [vertex]
    used = set()
    while q:
        v = q.pop(0)
        if v == target:
            return что нужно  #нашли
        used.add(v) 
        for neighbor in v.neigbors:
            if not (neighbor in used):
                q.append(neighbor)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.