Breadth First Search Algorithm Explained (With Example and Code)

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 12

  • @sudonick2161
    @sudonick2161 3 ปีที่แล้ว +5

    Damn...you're so underrated...I watched so many videos on the topic and yours was the only one that I found helpful. Thanks a lot, dude..for uploading this.

  • @allwinr4802
    @allwinr4802 3 ปีที่แล้ว +3

    This cannot be used to find the shortest path. For the input d = {
    1: [2, 3],
    2: [1, 3],
    3: [1, 2]
    }
    and the shortest path between 1 and 3,
    It returns [1, 2, 3] as the shortest path instead of [1, 3]

    • @bonjerne4911
      @bonjerne4911 2 ปีที่แล้ว +1

      Hello mate, got same problem with a bit more bigger graph. It will find the shortest path if we will swap 2 and 3 in your example (obviously it will change parent of 3). But I cant figure out what's wrong with algorithm, im pretty sure I miss something simple. Do you have thoughts where to dig?
      Thanks anyway.
      Edit.
      This is works.
      def bfs(graph, start, end):
      visit = {k: False for k in graph.keys()}
      parent = {k: None for k in graph.keys()}
      q = Queue()
      visit[start] = True
      q.put(start)
      while not q.empty():
      x = q.get()
      for v in graph[x]:
      if not visit[v]:
      visit[v] = True
      parent[v] = x
      q.put(v)
      return short(parent, start, end)
      def short(parent, start, end):
      path = []
      while end is not None:
      path.append(end)
      end = parent[end]
      return path[::-1]

    • @Arkonis1
      @Arkonis1 2 ปีที่แล้ว +2

      I think the issue could be fixed if we put after line 12 (in bfsShortestPath) the check that the neighbor node is the endNode, and in that case break the loop.
      for example if you add after line 12:
      if neighbor == endNode:
      return shortestPath(parentNodes, startNode, endNote)
      Then it works

    • @jonsmith568
      @jonsmith568 ปีที่แล้ว

      this version of bfs code only works for graphs without cycles, else u need to change the code

  • @tomarshiv7213
    @tomarshiv7213 3 ปีที่แล้ว +3

    Pls dfs as well

  • @olaboyeolanrewaju134
    @olaboyeolanrewaju134 หลายเดือนก่อน

    What if we want to set a goal node so instead of wanting the algorithm to read all the nodes we have a destination node we want it to stop at how do we factor that in into the code ?

    • @breonthibodeaux9916
      @breonthibodeaux9916 หลายเดือนก่อน

      I think you could pass in a parameter for goalnode and check if your current node is equal to that, now if you want the shortest path to that node then he shows that implementation

  • @olafschlammbeutel
    @olafschlammbeutel 4 ปีที่แล้ว +2

    Must watch 2020

  • @tomarshiv7213
    @tomarshiv7213 3 ปีที่แล้ว +2

    Dfs???

  • @tanyejie7694
    @tanyejie7694 7 หลายเดือนก่อน

    def dfs(graph,startNode):
    visitedNodes = []
    queue = [startNode]
    while queue:
    currentNode = queue.pop()
    if currentNode not in visitedNodes:
    visitedNodes.append(currentNode)
    for neighbour in reversed(graph[currentNode]):
    if neighbour not in visitedNodes:
    queue.append(neighbour)
    return visitedNodes
    ^Depth-search (use chatgpt for this)

  • @Beatboxerskills
    @Beatboxerskills หลายเดือนก่อน

    i copied everything but got ['0', '4', '1', '5'] as a result