Algorithms: Graph Search, DFS and BFS

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2016
  • Learn the basics of graph search and common operations; Depth First Search (DFS) and Breadth First Search (BFS). This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell. www.hackerrank.com/domains/tut...
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @limitless1692
    @limitless1692 4 ปีที่แล้ว +97

    You guys are experts at making things more confusing than they already are..

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

      She’s literally explaining what each line of code but not explaining the big picture of the implementation.. confusing explanation of search in the beginning

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

      100%

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

      What do you not understand about the implementation? I think it would help if you first look up Reducible's graph video where they animate stuff to make it easier to understand.

    • @AbdulHaq-tv6gy
      @AbdulHaq-tv6gy 11 หลายเดือนก่อน

      Fr

    • @tylerpritchard1385
      @tylerpritchard1385 12 วันที่ผ่านมา

      That's how you become the person who decides what we all have to learn to get a job.

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

    Step 1: Use English characters as data for each node
    Step 2: Assign English letters to the nodes in the algorithm and make the explanation confusing.
    Step 3: Watch everybody struggle

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

      Haha! Nailed it! :D

    • @renamons614
      @renamons614 6 ปีที่แล้ว +39

      literally that was so confusing and unnecessary!!

    • @testtesttesttesttest884
      @testtesttesttesttest884 5 ปีที่แล้ว +17

      Ridiculous video

    • @Jack_X075
      @Jack_X075 5 ปีที่แล้ว +40

      Step 4: Boop Boop Boop
      Step 5: Profit $$$$$$$$$$

    • @Bruno-fe4tp
      @Bruno-fe4tp 5 ปีที่แล้ว +4

      Hahaha. Exactly

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

    People are getting confused with those 'data' values inside the node circles and the 'variable names' Gayle uses to explain the search process. Heck, I was confused myself. But, start by ignoring the letter values inside the node circles, those are there just to represent data. When Gayle says "I'm going to call this node 's' and this other node 't', she is not talking about the data that you can see inside those circles, she is more likely talking about the variable names that she is going to use to carry out the search process. The 's' node will be the source node, or the node you are using as your starting point. The node 't' will be your target node, the node you are looking for. Watch the video again and you will see how she draws 's' and an arrow, then draws 't' and an arrow. Those are the nodes she is referring to when she says 's' or 't'.

    • @blablabla4231able
      @blablabla4231able 6 ปีที่แล้ว +4

      Thanks dude

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

      Why would that be confusing? Shouldn't it be pretty obvious, or do you think she should have used numbers as the vertex data instead of letters?

    • @blablabla4231able
      @blablabla4231able 6 ปีที่แล้ว +21

      it is confusing because she uses chars for both the data values and the variable names.

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

      stfu simp

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

      Or use 's' for start and 'g' for goal... Pick your poison!

  • @makii1715
    @makii1715 4 ปีที่แล้ว +116

    When you hear break-fast search instead of breadth-first search you know you should go take a nap

    • @ElephantSoup
      @ElephantSoup 4 ปีที่แล้ว +6

      or go eat

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

      really underrated

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

      Made me laugh at the middle of a dark night 😂

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

    Notice that the space complexity required for the recursive implementation of DFS is O(h) where h is the maximum depth of the graph. This is because of the space needed to store the recursive calls.
    DFS can also be implemented iteratively following the same idea as BFS but using a stack instead of a queue. This implementation uses O(v) extra memory where v is the number of vertices in the graph.

    • @sothearithsreang2600
      @sothearithsreang2600 6 ปีที่แล้ว +9

      Both implementations have the same memory complexity. Stack implementation uses heap memory (dynamic) while recursion uses stack memory.

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

      The iterative is also simpler and easier to understand, at least to me.

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

    This was fantastic! I'm so grateful this is on TH-cam

  • @YogeshPersonalChannel
    @YogeshPersonalChannel 6 ปีที่แล้ว +73

    It's amazing how elegantly sophisticated you made such an easy concept.

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

    0:57 "Hey ass" ...
    I literally got terror flashbacks of my life

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

    Boop boop boop boop

    • @laytonmiller5865
      @laytonmiller5865 6 ปีที่แล้ว +24

      There were five boops.

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

      that is accurate bro

    • @el-hawary2932
      @el-hawary2932 6 ปีที่แล้ว +2

      LOL

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

      hahahha

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

      I had a genetics professor who always used 'boops' in her explanations as well, good times

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

    Everyone here comlaining about the data and letters for each node blah blah, well I just want to thank you ! I am new at programming and have a project to do for college using graphs, this helped me a lot! Thanks!!

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

    Issues with this:
    - This implementation will work for graphs whose nodes are all connected to one another (e.g. every node has a path to every other node whether you have to go down the tree/graph or back up it and then perhaps down again). This will *not* work for _disconnected_ graphs with nodes that are disconnected to one another. Example: 1->2->3->4->5, 6->7->8 (no connection between 1 through 5 and 6 through 8)
    - Breath First Search was not explained for more than one iteration down the tree. You should really explain using at least two, typically three iterations so people can understand the process of the loop and the decision-making of the loop (e.g. how do we know when to add nodes to the queue, helps explain what happens if there's duplicates).

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

    Hi, what microphone do you use in the beggining? It sounds very clear ! Thanks :-)

  • @mandar.vaidya
    @mandar.vaidya 3 ปีที่แล้ว +2

    Hey Gayle , very nice explanation !!! do you have graph traversal videos in C also ? If yes , can you please provide links.

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

    For those who wonder why there isn't a nextToVisit.remove() in hasPathBFS(....), thats because you have to use a Queue. She mentioned it at 3:27
    So it should be like:
    private boolean hasPathBFS(Node source, Node destination) {
    ...
    Queue nextToVisit = new LinkedList();
    while(!nextToVisit.isEmpty()) {
    Node node = nextToVisit.remove();
    ....
    }

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

    Can you please provide this complete programme?

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

    Amazing explanations. Long live HackerRank

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

    That's a great explanation!!
    One question though,
    In BFS implementation I haven't seen the use of visited HashSet. shouldn't we check if a node already exists in the set before adding it into the queue?

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

    Something that could have been explained better is the LinkedList nextToVisist procedure. That LinkedList is a queue. So the first time we add the original node, the queue is size 1, inside the while, we remove the first element and obtained at the same time, the queue is size 0. When we add the children, then the queue size grows again and the while can keep looping. THE KEY IS: Understand the LinkedList as a supermarket. The first elements, add more elements, then those child elements are checked first because the elements that each of those adds, are added to the last position to the queue. Lets the first node has 2 children. The first node is checked, then add 2 children, child A and child B. The first child is checked, B, and that child adds C, D, E, but only after the second children. So the third iteration the queue looks like this [C, D, E]. And that is why is breadth-first because the queue makes the children get ordered in the back of the line. So every ancestor is checked first.

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

    Would be nice to see this also return the distance between the nodes. I implemented this by queuing this information together with the node being queued, but I wonder if that's the best way to do it or if there is a more elegant way.

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

    A well explained video on this topic. Thank you.

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

    good tutorial I did something similar and just used a vector node pointers...this looks like c++?
    Also a paired vector is good for holding a node and it edge values
    Is there a tutorial for searches when node edges have values to find the path with the lowest cost?

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

    Thanks for the video. Just one question tho. Why is that there's no value in the Node class? I mean there's id and which other nodes are connected to it, but there's no value? Why?

  • @LearnWithEmKay
    @LearnWithEmKay 4 ปีที่แล้ว +6

    In the BFS section, there might be as many as O(n^2) nodes in the nextToVisit. For example consider a complete graph.

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

    Yes this tutorial is advanced and so detailed, but it is so helpful. thank you so much.

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

    Great video! Helped me a lot implementing DFS and BFS!

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

    Great video! Filling the need for a Khan-academy channel for CS topics. More please!

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

    I just love Gayle! The best explanation ever. See also her book!

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

    Thank you for the video. Helped me a lot!

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

    Great explanation, thank you!

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

    Is there an efficient way to compute the level of connectivity of a graph? That is the number of vertices (or edges) that must be deleted to disconnect the graph. Or it is this NP hard?

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

    I love your videos and recently bought your book. Do you have samples of all of the data structures in javascript? That would be a fantastic resource to supplement your material--especially for noobs like me! Thank you.

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

    Clear and simple. Thank you!

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

    Is the order in graph near the end correct or am I missing something? 5 and 7 are calendar before 6 and 8 but there's no other path connecting them to S

  • @imochurad
    @imochurad 5 ปีที่แล้ว +24

    Missing a method:
    public void addNode(int id) {
    nodeLookup.put(id, new Node(id));
    }

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

      I think this would be the addEdge method that has been collapsed. It's not enough to add the node to nodeLookup, you also need to set it as the adjacent node of whatever node you want to link it to, hence addEdge having the parameters of source and destination.

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

    Thanks for this, it saved me!

  • @beedavis3596
    @beedavis3596 6 ปีที่แล้ว +34

    Very good overview ... but when you get to the code you rush through important detail that leave me a little in the dark.

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

      Very intuitive if you actually knew how to code lol

  • @beedavis3596
    @beedavis3596 6 ปีที่แล้ว +33

    So ... You never show the code for the "getNode" method. It's folded up and we can't see it ...

    • @ricardo072
      @ricardo072 4 ปีที่แล้ว +10

      Missing method:
      private Node getNode(int id){ return nodeLookup.get(id); }

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

    @5:32 in the hasPathDfs function, it takes two parameters and you try to recursively call it with 3 parameters?

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

      She's calling the private method which has 3 parameters.

  • @Manu-wb2uv
    @Manu-wb2uv 4 ปีที่แล้ว +4

    An optimization for the BFS is to put:
    if(visited.contains(child.id)) continue;
    inside the for loop so it won't add unnecessary items to the queue.

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

      I am not sure this is correct. Removing that check from where it is, triggers unnecessary looping. The idea is to skip unnecessary iterations altogether.

    • @Manu-wb2uv
      @Manu-wb2uv 3 ปีที่แล้ว

      @@hfontanez98 exactly the opposite. Unnecessary looping happens if the visited.contains it's where it is now. Because you add nodes to the queue that were already visited.

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

    This video really helps! Anyone knows where the source code of this tutorial is?

  • @g7-smart-logistic-app
    @g7-smart-logistic-app 5 ปีที่แล้ว

    Is there is a lag in the explanation at 1:13. I feel something going weird.

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

    Where do I add adjacent nodes to source node to build graph can you please post that code

  • @lovebisaria3325
    @lovebisaria3325 6 ปีที่แล้ว +4

    in the addEdge function, should we also not do something like d.adjascent(s);

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

      I think so, my test(1,4) fault at 1-2, 1-3, 3-2, 3-4 configuration.

    • @Manu-wb2uv
      @Manu-wb2uv 4 ปีที่แล้ว

      It depends if you want your graph to be a digraph or not :).

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

    really helpful. Thanks Gayle.

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

    What ‘s the name of the compiler your using ?

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

    What are the complexities of both DFS and BFS in terms of O() and Omega() ?

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

    Would it be possible show an example being ran? For those of us watching this video to learn, I am unsure of how to run your program.

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

    Well I’m surely going to save this implementation for last to memorize from scratch. Already got Linked Lists, Doubly Linked Lists, Stacks, and Queues down. Currently on trees but Graphs just seem a doozy 😢

  • @RobertJohnson-fm4vl
    @RobertJohnson-fm4vl 7 ปีที่แล้ว +2

    How do you find the cost of a path?

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

    Great videos!

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

    Awesome and very simple explanation! Enthralled . Superb mam. You are so Good . !

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

    There seems to be a bug in your BFS code. once you find that a nide has already been visited why are you continuing and then adding that same mode back to the que? It should be a if conditoons checking that the node id is "not visited" and then continue and add the node id to the visted que

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

    Video could be somewhat confusing at first. Specifically the ambiguity of node 's' vs. node 'S'.
    It's completely clear after a couple of seconds of thought, though still annoying.
    There is already a node 'S', which is different the node 's' that the narrator talks about. Wasn't sure which 's' she meant after statements as seen below:
    At 1:07 the narrator says, "Hmm...I'm not sure let me go ask [S's children]". Then immediately after that t is displayed pointing to H, a child of 'S'.

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

    I am unable to visualize the graph data structure with this implementation. In the implementation, each node has a linked list of adjacent nodes. That just makes the data structure a very long linked list right? I am unable to correlate this with the graph shown at 2:11. 's' has 2 children - 'a' and 'b'. In this case, what will be the 'adjacent' linked list of 's'?

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

      She also explains it towards the end of the video with an example so that helps

  • @ScottMeesey
    @ScottMeesey 13 วันที่ผ่านมา

    It's nice to see that terminology like "boo-boo-boo-boo-boop" has survived the test of time. No shade, I think I've used it in my career at some point.

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

    What's in her private Node getNode(int id) body and what is it returning?

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

    Finally cleared the mist... Thank you very much...

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

    A graph data structure can be represented by using a Map as well

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

    Hi everyone, i am learning algorithms. I looked through lots of syllabus and found them interesting, but i dont know what resource to learn them from. Theres a whole list of algorithms and clever tricks i would like to learn, sorting, search, dp, recursion, etc. Please, if you have a good resource to learn these from, they would be very helpful!

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

    I think it was confusing because the voice over is little ahead so when she says "I'm gonna call it 's' " - it doesn't appear making us look at the node with value 'S'

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

    damn i finally understand after watching for the 5th time

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

    What is to be written in the getNode() method in dfs?
    Please provide link for the code

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

      private Node getNode(int id) {
      Node node = new Node(id);
      return node;
      }
      Though the constructor of Node is private, all members in Graph have access to it, since Node is nested class in Graph. They have been made private, because only Graph instance can create nodes by calling getNode().

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

      Michael Zhang this would work if the search methods were relying on equals() rather than ==, otherwise the comparison is done at memory address level which is not what is intended (the comparison should be per id)

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

      Hey Ankita,
      Actually the getNode method is to retreive the Node from the graph itself
      (private HashMap nodeLookup) according its id:
      Private Node getNode(int id)
      {
      return nodeLookup.get(id);

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

      Eyal is Correct!

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

      According to me this is how getNode() method looks like:
      private Node getNode(int id)
      {
      if(nodeLookup.containsKey(id)) return nodeLookup.get(id);
      Node node = new Node(id);
      nodeLookup.put(id,Node);
      return node;
      }

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

    Can someone please explain to me why in the DFS, we return false as soon as we found the node was already visited. While in BFS we continue when we found the node was visited? 🤔

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

      If it was visited, then its children had inserted in nextToVisit.

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

    It feels like everybody in the comments thinks they are smarter than her. A messup happens, so what. If you dont count the start, the video teached me the basics and thats why I watched this

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

    As the video goes further I get more confused.
    First the variable name with mismatching values inside where variable name is the one being referred to without prior knowledge on what she is referring.
    Once the coding started she started jumping all over the place as if it's the weekend and she has to catch the bus.
    It would also be nice if we get a copy of the code since getNode was not shown which will allow us to study how the code works ourselves.

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

    Great explanation madam..respect you.What is the best programming language to learn data structures and algorithms as a beginner??..please answer me madam..

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

    visited Set should Set I think. Otherwise, we assume all nodes have unique ids

  • @jackli5654
    @jackli5654 6 ปีที่แล้ว +4

    For BFS, why use a linked list when you can just use a queue?

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

      LinkedList is a Queue in java
      Queue adjacent = new LinkedList();

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

    Anyone knows what software/hardware is being used to write and present this? Tnx

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

      Hackerrank website's own code editor

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

    1:39 When she said "boop boop boop boop boop", that really hit me

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

    Do anyone know what's the benefit of using a LinkedList for storing the adjacent children of the Node class. Why not use a std::vector for this?

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

    if visited.contains (node.id) check should be at start of loop in BFS I believe

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

    What is the point of visited hashset in your BFS implementation? You aren't ever using it for anything (just continuing).. ?

    • @KaranSingh-ge7wz
      @KaranSingh-ge7wz 5 ปีที่แล้ว +1

      to avoid an infinite loop sort of scenario? so that we dont check on a node that has already been checked*

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

    Honestly for the first 2 minutes, I was so confused I had to come to the comment. Now I see she just spelled the word GRAPH DFS BFS as the nodes. I thought those were the values. It would be better to point that out next time. But Great Explanation!!!

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

    a. thank you for the video.
    b. i think there is an issue with the BFS function.
    The entire condition - if (visited.contains(node.if)) - seems meaningless.
    because no action is taken - whether the condition is met or not.

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

    Hello ma'am, I am new to programming. According to my understanding Node class should not be static. Because we need to create multiple nodes, which means we need many instances of the same class. Please correct me if I am wrong. Thank you

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

      Akshay Chandrachood Static classes are not like static variables. You can create many instances of them. In Java, you can create static NESTED classes for encapsulation. I’m For example, the Graph class can access the Node classes private variables and methods because it is nested. However, functionally, the Node class isn’t different from any other class. In fact, I don’t know why people use static classes at all haha

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

      Thanks for the clarification.

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

      @@jessicahuang5507 in C++, you cannot instantiate a static class. I think this is the origin of the confusion.

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

    Why are adjacent nodes a LinkedList rather than an array?

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

    Hello dear teacher could you please add video on adding and delete node/ edge in graph

  • @annaa8632
    @annaa8632 6 ปีที่แล้ว +36

    It is really confusing to use letters as node values and then also use letters as node variables ! At the beginning I was like what we are taking about : S node is actually G node .. what?? I just stopped watching it.

    • @kirk-patrickbrown866
      @kirk-patrickbrown866 6 ปีที่แล้ว +1

      I total agree with you on the S node which was actually g node and how confused i was.

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

      Is Gayle really helping or tormenting us at this point I gotta ask myself

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

    It's funny to see book "КАРЬЕРА ПРОГРАММИСТА" on the table)) It could be translated as "Programmer Career". What it is the first and second one?

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

    Instead of adding values in hashset we should add the entire node so that we may not stuck in uniqueness property in graph!! This will solve problem when the graph contains same value but the nodes are different and also their neighbors are different

    • @Lucas-of6ou
      @Lucas-of6ou 4 ปีที่แล้ว

      Yep, i noticed that too.

  • @ricardo072
    @ricardo072 4 ปีที่แล้ว +12

    Missing method:
    private Node getNode(int id){ return nodeLookup.get(id); }

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

      thanks

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

      what is nodeLookup??

    • @user-vl4do1fs5r
      @user-vl4do1fs5r 2 ปีที่แล้ว

      @@AlancRodriguez I think nodeLoopup is the hashmap for all the nodes in the graph in Key(id)-Value(Node) format.

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

    This implementation has a flaw. If you create a graph with, for example, four nodes labeled 1, 2, 3, and 4 and you pass something like graph.hasPathXXX(20, 44) will return true instead of false because the getNode methods can return null both, which will pass the source == destination test. To avoid this, you have to add a case that checks for null and returns false if either the source node or the destination node is null.
    I tried this implementation at home and was able to confirm it by running a simple test like the one I included above.

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

    where from did you get s and t and a, stay consistent with your graph labels

    • @user-vl4do1fs5r
      @user-vl4do1fs5r 2 ปีที่แล้ว

      I think 's', 't', 'a' are the alias of the node. "graph labels" inside of the circle are the node values, could be anything, integers, strings,etc. In this case it just happened to be letters. for example, from node s to node t could mean from New York to Vancouver.

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

    Thank you!

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

    loved the video but i wish you could have attached the source code in the description...

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

    What was the point of that continue if statement for bfs??? Also wouldnt you NOT want to add the node to "visited" if its already inside visited???

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

      Continue skips to the while loop again. If you have already visited this node there is no need to add their child again.

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

      @@MrJeffFeng oh woops completely forgot. Thanks

  • @nawras.hawamdeh
    @nawras.hawamdeh 5 ปีที่แล้ว

    I read the comments before watching the video, and now i really don't know what to do! Is it really helpful or confusing?

    • @nawras.hawamdeh
      @nawras.hawamdeh 5 ปีที่แล้ว

      After 3 minutes watching: Hill man.. I'm gonna quite!

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

    In DFT,
    We can't say if visited has source node then path doesn't exist.
    Instead we should modify the algorithm as :
    Algorithm hasPathDFS(source , target, visited) {
    if(source == target ) {
    return true;
    }
    visited.add(source);
    for(Node child : sources.adjecent) {
    if(!visited.contains(child)) {
    if( hasPathDFS(child , target, visited) ) {
    return true;
    }
    }
    }
    return false;
    }
    Explanation and examples are really good. Thanks for this video.

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

      yes. i do not understand why we need 'if visited.contains(source.id) then return false'.

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

      The line "if(visited.contains(source.id) return false" is important. It simply means if visited contains the source.id, then there is a cycle and no need to start checking from that source again and again.

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

    very clever using a queue

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

    Why would you do this(inside hasBFS method):
    if (visited.contains(firstNode.id)){
    continue;
    }
    if you are adding node.id to visited list anyway?
    also we can check the condition below first thing in the method so if it is equal we dont have to go through any extra operation
    if(source == destination){
    return true;
    }

    • @Manu-wb2uv
      @Manu-wb2uv 4 ปีที่แล้ว

      Or better just put it inside the for loop so it won't add items that we've already check to the queue

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

    Thank you very much. That was very good and clear explanation. At first a little confusing as the voice and video got out of sync, but overall very valuable.

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

    what is the code inside getNode method?

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

      public Node getNode(int id)
      {
      Node S = nodeLookup.get(id);
      return S;
      }

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

    When explaining DFS/BFS, why don't you use the existing graph nodes instead of arbitrary nodes, thereby causing unnecessary confusion in the process?

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

    am fresh peogramming student and can I have the link to your source code

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

    For DFS, I think there should be a null check since it is possible that there are no children to a node - i.e. the leaf node.

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

    how can the methods on lines 24 and 31 have the same name? I don't write Java code, just curious.

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

      That's polymorphism, OOP concept, not any language-specific feature.

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

      @@ShuvoAmin interesting! Not possible in the language I write OOP code in (Ruby).

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

      @@WylliamJudd I know little Ruby, however, good to know that, it doesn't support overriding 🤔

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

    We ask question "Hey S, do you have path to node T?" but there is no node T. But later she points S to G and T to H.... can somebody please explain me this part.

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

      Ignore the letters inside the node circles, those are there just to represent data. When she says "I'm going to call this node 's' and this other node 't', she is not talking about the data that you can see inside those circles, she is more likely talking about the variable name that she is going to use to carry out the search process. The 's' node will be the source node, or the node you are using as your starting point. The node 't' will be your target node, the node you are looking for. Watch the video again and you will see how she draws 's' and an arrow, then draws 't' and an arrow. Those are the nodes she is referring to when she says 's' or 't'.

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

    I don't understand why line 33 (in DFS) has 'return false' ... I understand that if the node has been visited previously from the same node, then there is a loop. So a loop exists in the graph... How does that imply that there is no path to the destination (it could be in another path of the graph?) Thanks
    EDIT - I just saw the comment below by swapnil gaikwad . He thinks this part of the algorithm is wrong!

    • @1Minute-Movie
      @1Minute-Movie 5 ปีที่แล้ว

      I was thinking this is just a sanity check for disconnected graphs? where the destination is not connected, but even then I feel like this condition is unnecessary.

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

    For anyone still watching this, if you want to run it make a main method and then call the class and set up your Hash Map
    In your main method:
    Graph (Whatever name) = new Graph;
    System.out.println((Whatever name).hasPathDFS(1, 5));
    It'll turn true or false depending on how your hash table is set up