Algorithms: Solve 'Shortest Reach' Using BFS

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

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

  • @mudaix34
    @mudaix34 8 ปีที่แล้ว +23

    What should public Graph(int size), private class Node, private Node getNode(int id), and public void addEdge(int first, int second) look like?

    • @zhegemingzigouchang
      @zhegemingzigouchang 7 ปีที่แล้ว

      If you are stuck, here's my solution to this (I didn't implement Node class). bit.ly/2E0rfDX

    • @90krishika
      @90krishika 4 ปีที่แล้ว +1

      @@zhegemingzigouchang - Why v + 6 ? Doesn't make sense

    • @primosdelbarrio
      @primosdelbarrio 4 ปีที่แล้ว

      @@90krishika I would imagine that the +6 is comes from hardcoding the EDGE_DISTANCE static variable from the video.

  • @davidwoosley
    @davidwoosley 5 ปีที่แล้ว +11

    The link provided in the description is broken: 404 error.

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

    This video is designed for individuals with prior knowledge of BFS and DFS concepts. It serves as a review for those already familiar with these topics, providing an opportunity to reinforce existing understanding. If you are not acquainted with BFS and DFS, it is recommended to gain proficiency in these concepts before engaging with this content.

  • @pawandeepchor89
    @pawandeepchor89 6 ปีที่แล้ว +96

    Please don't just start coding, try to explain the logic first.

  • @TeeFoles
    @TeeFoles 7 ปีที่แล้ว +7

    how would I find the shortest path between two nodes? say I gave two nodes as parameters

    • @Ankit5311
      @Ankit5311 5 ปีที่แล้ว

      you have to track the parent node...

    • @sahilsharma2952
      @sahilsharma2952 4 ปีที่แล้ว

      @@Ankit5311 Use LCA.

  • @sukumarkuppusamy4218
    @sukumarkuppusamy4218 6 ปีที่แล้ว +1

    Do adding 0 in distance array would print that too? Are you eliminating 0 to remove the start node index from the distance and printing it?

    • @prakhargandhi8919
      @prakhargandhi8919 6 ปีที่แล้ว

      it means distance from start node to itself =0,if a cycle is formed we might found distance larger than 0

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

    I think it should be
    if((distance[neighbor] < distance[node] + edge) || distance[neighbor] == -1)
    Say starting node =1, end node = 4. Now say 1->3 is 10unit and 3->4 is 100unit.
    Then using above BFS it will find neighbor of 1 which is 2, 3...then ones it gets to 3 it'll find neighbor 0, 4, 6 and will update distance[4] = 110
    But say 3->0 is 10unit and 0 -> 5 10unit and 5 -> 4 is 10 unit. Then once we get to neighbor of 5 the shortest reach using above program will say ohh I already visited "4" (because distance[4] != -1 it's 110) and it will skip updating the distance where it could have been 40Units(1->3 , 3->0, 0->5, 5->4 all 10 units) instead of 110.

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

    what is nodes[node].neighbours ???

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

      neighbors of the node..connected nodes .. a list may be

    • @yuqingpang1794
      @yuqingpang1794 5 ปีที่แล้ว +1

      Must be hidden in the "Node" data structure, something not covered in the video.

    • @brutefarts184
      @brutefarts184 4 ปีที่แล้ว

      yea she folds the Node class you can see its truncated in the ide

    • @hfontanez98
      @hfontanez98 4 ปีที่แล้ว +1

      The class "Node" has a LinkedList of neighbors. These are the nodes connected to current node. She included the implementation on her video titled: "Algorithms: Graph Search, DFS and BFS."
      What she didn't cover is how to create the graphs so that you could test your implementation. For this, you need two methods: 1) One to add Node objects to the Node array (on the other video, she used a Linked List and called it something different) and 2) the implementation of addEdge(int, int). That implementation is actually quite simple:
      public void addEdge(int source, int destination) {
      Node s = getNode(source);
      // This can return null
      Node d = getNode(destination);
      // This can return null
      s.neigbors.add(d);
      d.neigbors.add(s);
      }
      you could avoid returning null by using the Null Object pattern and have the getNode method return a null Node Object rather than a null pointer. Then, you need to connect nodes that are not instances of Null Node class. But this is a topic for another time.
      Also, d.neigbors.add(s) should be done only if you want bidirectional routes from node to node. Perhaps it is better to do without this step and add the edges when you create the graph, i.e. graph.addEdge(1, 2) and graph.addEdge(2, 1)
      . This way, you could make certain paths unidirectional and other bidirectional if needed.

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

    It will not work on weighted graph, when different edges have diff weights.

    • @ane5312
      @ane5312 3 ปีที่แล้ว

      yeah cause bfs is not applied for weighted graphs

  • @LaggLtd
    @LaggLtd 8 ปีที่แล้ว +1

    was wondering if you could help me with a Maze program in java im working on, I'm trying to use a BFS to find the shortestPath and could really use your help :)

  • @tirthdoshi7463
    @tirthdoshi7463 5 ปีที่แล้ว +4

    Thank you very much ! Great idea to optimize by not keeping the visited array :)

  • @sivar4300
    @sivar4300 7 ปีที่แล้ว +6

    How does BFS gives shortest path ?

    • @hireshtrivedi9731
      @hireshtrivedi9731 7 ปีที่แล้ว +12

      Since the graph is not weighted and bfs traverses all the child nodes at the same level first, the immediate child nodes will have the smallest distance from the starting node

    • @TheZwirek
      @TheZwirek 6 ปีที่แล้ว

      I believe that the point is that the traversing method has nothing to do with finding shortest path. You can find shortest path using any traversing method. Simply speaking the point was made in one of the previous videos, that BFS can be more optimal for finding shortest path in cases where the target node is a direct child of the node that is currently being considered.

    • @doctor3397
      @doctor3397 5 ปีที่แล้ว

      My understanding is that you will get a distances array. And you will have to find the smallest number in the ararry and that would be the shortest path.

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

      Its been 6 years, how you guys doing? Any update brothers 😂

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

    Dijkstra?

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

    For anyone who did not get how graph is represented in this coding question!
    www.hackerrank.com/challenges/ctci-bfs-shortest-reach?h_r=internal-search

  • @soumitramehrotra5547
    @soumitramehrotra5547 4 ปีที่แล้ว

    You have initialized EDGE_DISTANCE as 6. Isn't BFS shortest distance works when edge distance is 1?

    • @primosdelbarrio
      @primosdelbarrio 4 ปีที่แล้ว

      BFS works when all the edges have the same distance. Because every single edge has a distance of 6 it's the same as if all of the edges had distance of 1. There would be an issue if different edges had different distances.

    • @soumitramehrotra5547
      @soumitramehrotra5547 4 ปีที่แล้ว

      ​@@primosdelbarrio Ok. I thought DFS works when all edges have weight 1. Thanks

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

    very wonderfully done....thank you.....

  • @oceanview-u8q
    @oceanview-u8q 6 ปีที่แล้ว

    shortest reach from where to where is not quite clear.. i see only startid.. since she didn't display whole class code, i'm not sure my question is valid or not but... if 6 is target and startid is 1 in the picture, i think it should be like distance[node] + 1, not EDGE_DISTANCE.. if i'm interviewer, i may ask find shortest path between two node.. then the 1 distance should be added up in each round of for loop to check neighbors.. anyway, most of videos(geeks, byte, leetcode, etc...) that explains BFS used visited[] but using array for distance seems really good idea cause i can use one array for both visited and distance which is cool to me. thanks to her actually.

  • @AD-jz5sq
    @AD-jz5sq 4 ปีที่แล้ว +14

    Seems like memorized solution. I’d never go straight to the code without solving the problem on paper first.

  • @TheZwirek
    @TheZwirek 6 ปีที่แล้ว

    It was said close to the end of the video, that DFS wouldnt be a good choice for this problem. I have to challenge that. In this particular task we are calculating all the distances to all the nodes from starting node. Both DFS and BFS have the same time and space complexity (its just a stack over queue). Therefore Im making a statement that it would be equally good to use either of them here. What am i missing?

    • @oceanview-u8q
      @oceanview-u8q 6 ปีที่แล้ว +2

      i'm not quite expert but just to share my understanding... still to me BFS is better one for shortest path in time perspective because,, assuming the graph is huge and my target is pretty much close to start but it's in the end of neighbors, then DFS will do search all children in previous neighbors and take whole time to finally find my target..

    • @nimblest
      @nimblest 5 ปีที่แล้ว +1

      DFS would fail in the drawn graph here. Let's say our DFS always prioritizes the node with the lowest #. After doing 1, 2, it would do 1, 3, 0, 5, 4. Node 4 would be marked as visited with a distance of 4. After backtracking to 3, it would go to 6 next without visiting 4 again, leaving us with the wrong distance of 4 for Node 4 rather than 2.

    • @karthikrangaraju9421
      @karthikrangaraju9421 4 ปีที่แล้ว

      Since this is about finding 'shortest' path, BFS is better because if we end up finding the vertex, we would immediately exit, whereas DFS could be really slow if it took the wrong path and explored everything

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

    If you're a beginner trying to understand BFS, this is not the place

  • @YT-yt-yt-3
    @YT-yt-yt-3 3 ปีที่แล้ว +2

    start to where? what is the purpose hiding other methods? No explanation of overall logic. She just memorized the code and typed it on camera.. worst channel.

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

    Heard some doggos at 4:42

  • @larssmeets773
    @larssmeets773 2 ปีที่แล้ว

    Why is the Linkedlist called a queue :-)

  • @happyvoyage599
    @happyvoyage599 8 ปีที่แล้ว

    good job gayle laakmann!!!!!!!!1

  • @SandyRocks007
    @SandyRocks007 6 ปีที่แล้ว

    what is the IDE you are using ?

  • @arvindsinha1566
    @arvindsinha1566 5 ปีที่แล้ว +1

    logic is working.

  • @_dinesh
    @_dinesh 4 ปีที่แล้ว

    isn't this Dijkstra?

  • @agmeister7446
    @agmeister7446 5 ปีที่แล้ว

    fiquei na mesma dps deste video

  • @90krishika
    @90krishika 5 ปีที่แล้ว

    Please share your code?