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
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'.
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
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.
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.
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).
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.
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!!
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(); .... }
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.
@@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.
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.
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.
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.
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'.
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.
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 😢
I found this BFS code not very efficient. You could've moved the "if (visited.contains(node.id))" condition (without continue statement) in the "for each child" loop. That is, enqueue the child node only if it has not been visited. In this case, we can also ensure the dequeued nodes will always be unvisited (no need to check for continue). The current implementation will redundantly enqueue many visited child nodes, especially in an undirected graph!
Thanks a lot for your code! But, one thing that I wanted to ask just in case: you don't need the visited.add(node.id) right before the for loop in this corrected code as it's already within the for loop (but now adding child.id to visited) right?
I think its relative tradeoff depending upon the connectedness of the graph. In your solution your calling contains across ALL children, including nodes that could have 8000 leaf nodes that will never be revisited. Also, if the node in question is found early, you've still called contains on lots of child nodes that have never been walked yet.
@@KhanSlayer ok but even then the code in the video could lead to an infinite loop. If node is in visited yiy should NOT be adding it to "next to visit".
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
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.
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.
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.
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'
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
I liked the explanations of DFS and DFS algorithms. Did not like that the entire code was not covered or made available. The description seems to be for a bidirectional graph. If there is a path form G -> H then there should be one form H -> G. The implementation of the addEdge() does not allow for it. When I did my implementation I used letters instead of numbers. Found it easier to follow.
I wish she would actually show how to actually instantiate this algorithm. Like, HOW do I create the tree and then HOW to do I actually call the search methods?
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!!!
To understand a coding topic, I used to check your videos first, it leaves me bit confused, I check other videos, and things become clear then.. You have some good videos but not the coding ones, I think you may work harder to simplify things, you may try to explain the code while linking it with the algorithm. Thanks for your efforts anyways.
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
Great explanation madam..respect you.What is the best programming language to learn data structures and algorithms as a beginner??..please answer me madam..
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.
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?
Sorry, it might be my own problem but I am struggling to understand your explaination about the s to t to a b c thing. I feel quite confusing about the figure youre pointing at.
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.
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!
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.
Node node = nextToVisit.remove() the above method is wrong as remove() method returns a boolean value. Node node = nextToVisit.removeFirst() will work and returns next node to visit.
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'?
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?
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
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.
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
If it is an undirected graph, I think the addEdge method should be : public void addEdge(int source, int destination) { Node s = getNode(source); Node d = getNode(destination); s.adjacent.add(d); d.adjacent.add(s); }
I see, so if it's directed then we can have it as. public void addEdge(int source, int destination) { Node s = getNode(source); Node d = getNode(destination); s.adjacent.add(d); } Not sure of this though but that is what I think
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.
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.
notice that she's not going too deep into the concepts and making it easier for us, but she's impressing us just enough to get us interested in her book. after all every video starts with her introduction then her book! or I'm just overthinking
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?
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? 🤔
We can actually take all four of her DFS/BFS methods and condense them into a single method as follows (isBFS = true for a BFS, and isBFS = false for a DFS): public boolean hasPath(int source, int destination, boolean isBFS) { LinkedList nextToVisit = new LinkedList(); HashSet visited = new HashSet(); nextToVisit.add(getNode(source)); while (!nextToVisit.isEmpty()) { Node node = isBFS == true? nextToVisit.remove() : nextToVisit.removeLast(); if (node == getNode(destination)) return true; visited.add(node.id); for (Node child : node.adjacent){ if (!visited.contains(child.id)) nextToVisit.add(child); } } return false; }
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!
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
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