Algorithms: Graph Search, DFS and BFS

  Рет қаралды 954,698

HackerRank

HackerRank

Күн бұрын

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....

Пікірлер: 297
@tourniquet3306
@tourniquet3306 7 жыл бұрын
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 жыл бұрын
Haha! Nailed it! :D
@Bladebreak3r
@Bladebreak3r 7 жыл бұрын
literally that was so confusing and unnecessary!!
@testtesttesttesttest884
@testtesttesttesttest884 6 жыл бұрын
Ridiculous video
@Jack_X075
@Jack_X075 6 жыл бұрын
Step 4: Boop Boop Boop Step 5: Profit $$$$$$$$$$
@Bruno-fe4tp
@Bruno-fe4tp 6 жыл бұрын
Hahaha. Exactly
@DavidBadilloMusic
@DavidBadilloMusic 7 жыл бұрын
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 жыл бұрын
Thanks dude
@HalfTimesTwo
@HalfTimesTwo 6 жыл бұрын
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 жыл бұрын
it is confusing because she uses chars for both the data values and the variable names.
@I3uzzzzzz
@I3uzzzzzz 4 жыл бұрын
stfu simp
@victorglaviano
@victorglaviano 3 жыл бұрын
Or use 's' for start and 'g' for goal... Pick your poison!
@miceshinoda
@miceshinoda 7 жыл бұрын
Boop boop boop boop
@laytonmiller5865
@laytonmiller5865 7 жыл бұрын
There were five boops.
@guanlunzhao6408
@guanlunzhao6408 6 жыл бұрын
that is accurate bro
@khaled-h4w4ry
@khaled-h4w4ry 6 жыл бұрын
LOL
@AzusxD
@AzusxD 5 жыл бұрын
hahahha
@thndesmondsaid
@thndesmondsaid 4 жыл бұрын
I had a genetics professor who always used 'boops' in her explanations as well, good times
@limitless1692
@limitless1692 5 жыл бұрын
You guys are experts at making things more confusing than they already are..
@sundarkris1320
@sundarkris1320 3 жыл бұрын
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 жыл бұрын
100%
@raffayhussain6717
@raffayhussain6717 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 Жыл бұрын
Fr
@tylerpritchard1385
@tylerpritchard1385 4 ай бұрын
That's how you become the person who decides what we all have to learn to get a job.
@makii1715
@makii1715 5 жыл бұрын
When you hear break-fast search instead of breadth-first search you know you should go take a nap
@ElephantSoup
@ElephantSoup 4 жыл бұрын
or go eat
@II_xD_II
@II_xD_II 4 жыл бұрын
really underrated
@Blogger553
@Blogger553 2 жыл бұрын
Made me laugh at the middle of a dark night 😂
@minhvu8682
@minhvu8682 Ай бұрын
Thank you so much. The way you explain is so simple and easy to understand.
@Hecticam
@Hecticam 4 жыл бұрын
0:57 "Hey ass" ... I literally got terror flashbacks of my life
@beedavis3596
@beedavis3596 7 жыл бұрын
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 4 жыл бұрын
Very intuitive if you actually knew how to code lol
@martincopes3818
@martincopes3818 7 жыл бұрын
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 7 жыл бұрын
Both implementations have the same memory complexity. Stack implementation uses heap memory (dynamic) while recursion uses stack memory.
@BlackJar72
@BlackJar72 6 жыл бұрын
The iterative is also simpler and easier to understand, at least to me.
@imochurad
@imochurad 5 жыл бұрын
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.
@mugi1726
@mugi1726 3 жыл бұрын
damn i finally understand after watching for the 5th time
@Arunnn241
@Arunnn241 4 жыл бұрын
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).
@beedavis3596
@beedavis3596 7 жыл бұрын
So ... You never show the code for the "getNode" method. It's folded up and we can't see it ...
@ricardo072
@ricardo072 5 жыл бұрын
Missing method: private Node getNode(int id){ return nodeLookup.get(id); }
@Funkfreed
@Funkfreed 4 жыл бұрын
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.
@ricardo072
@ricardo072 5 жыл бұрын
Missing method: private Node getNode(int id){ return nodeLookup.get(id); }
@sizmostudent
@sizmostudent 3 жыл бұрын
thanks
@AlancRodriguez
@AlancRodriguez 3 жыл бұрын
what is nodeLookup??
@user-vl4do1fs5r
@user-vl4do1fs5r 3 жыл бұрын
@@AlancRodriguez I think nodeLoopup is the hashmap for all the nodes in the graph in Key(id)-Value(Node) format.
@cutiko
@cutiko 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.
@Manu-wb2uv
@Manu-wb2uv 5 жыл бұрын
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 4 жыл бұрын
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 4 жыл бұрын
@@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.
@LearnWithEmKay
@LearnWithEmKay 4 жыл бұрын
In the BFS section, there might be as many as O(n^2) nodes in the nextToVisit. For example consider a complete graph.
@Ronny2332
@Ronny2332 3 жыл бұрын
Great video! Helped me a lot implementing DFS and BFS!
@wonggran9983
@wonggran9983 7 жыл бұрын
Great video! Filling the need for a Khan-academy channel for CS topics. More please!
@shubhc5
@shubhc5 7 жыл бұрын
Can you please provide this complete programme?
@annaa8632
@annaa8632 7 жыл бұрын
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 7 жыл бұрын
I total agree with you on the S node which was actually g node and how confused i was.
@oneforallah
@oneforallah 5 жыл бұрын
Is Gayle really helping or tormenting us at this point I gotta ask myself
@lolololololololol11
@lolololololololol11 7 жыл бұрын
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'.
@garciamilord549
@garciamilord549 4 жыл бұрын
What's in her private Node getNode(int id) body and what is it returning?
@jackli5654
@jackli5654 6 жыл бұрын
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();
@YogeshPersonalChannel
@YogeshPersonalChannel 7 жыл бұрын
It's amazing how elegantly sophisticated you made such an easy concept.
@yashashwisen
@yashashwisen 6 жыл бұрын
kzbin.info/www/bejne/mHqkaXt7erqrkKs
@prashanthnehemiah
@prashanthnehemiah 6 жыл бұрын
True
@ASoftwareEngineer
@ASoftwareEngineer 6 жыл бұрын
@@yashashwisen that was a good video
@WahranRai
@WahranRai 5 жыл бұрын
Are you looking for a rendez-vous ?
@irynaprokopenko6438
@irynaprokopenko6438 5 жыл бұрын
@@yashashwisen good video actually
@theatomicfoxchannel8235
@theatomicfoxchannel8235 7 жыл бұрын
Thank you for the video. Helped me a lot!
@ihora2863
@ihora2863 2 жыл бұрын
The content of the getNode method was not explained
@OzoneGamerStation
@OzoneGamerStation 5 жыл бұрын
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 😢
@nitroGPT
@nitroGPT 2 жыл бұрын
That’s my morning routine. Breakfast search.
@mollyambrai1157
@mollyambrai1157 3 жыл бұрын
Clear and simple. Thank you!
@MohitKumar-jy6wm
@MohitKumar-jy6wm 2 жыл бұрын
if you are exhausted and want some fun 1:40
@vitaminb4869
@vitaminb4869 7 жыл бұрын
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.
@omarsherif6198
@omarsherif6198 3 жыл бұрын
Just because it's Gayle McDowell she isn't not getting slammed for this explanation
@chrizonline
@chrizonline 6 жыл бұрын
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(); .... }
@kar-unboxed
@kar-unboxed 5 жыл бұрын
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'
@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?
@odayprogrammer
@odayprogrammer 3 жыл бұрын
just use a queue for BFS, why the need for a linked list
@evelynnmimijae
@evelynnmimijae Ай бұрын
3:03 Would changing `while len(stack)>=0:` to `while stack:` turn this into a BFS from a DFS? import networkx as nx def DFS(G,v): stack =[] discovered = [] stack.append(v) while len(stack)>=0: u = stack.pop() if not u in discovered: discovered.append(u) for neighbor in sorted(G.neighbors(u)): stack.append(neighbor) return discovered # Use libraries if possible G = nx.Graph() G.add_nodes_from([0,1,2,3,4,5]) G.add_path([0,1,2,3]) G.add_path([1,4,5]) print(DFS(G, 0))
@bobo87268
@bobo87268 4 жыл бұрын
This video really helps! Anyone knows where the source code of this tutorial is?
@lovebisaria3325
@lovebisaria3325 7 жыл бұрын
in the addEdge function, should we also not do something like d.adjascent(s);
@doctornkz
@doctornkz 6 жыл бұрын
I think so, my test(1,4) fault at 1-2, 1-3, 3-2, 3-4 configuration.
@Manu-wb2uv
@Manu-wb2uv 5 жыл бұрын
It depends if you want your graph to be a digraph or not :).
@academicconnorshorten6171
@academicconnorshorten6171 7 жыл бұрын
Great explanation, thank you!
@RadimSafran
@RadimSafran 7 жыл бұрын
Hi, what microphone do you use in the beggining? It sounds very clear ! Thanks :-)
@ScottMeesey
@ScottMeesey 4 ай бұрын
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.
@TheHalalPolice
@TheHalalPolice 3 жыл бұрын
What are the complexities of both DFS and BFS in terms of O() and Omega() ?
@hasnaa0ibraheem
@hasnaa0ibraheem 5 жыл бұрын
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.
@Papayalexius
@Papayalexius 6 жыл бұрын
Psyduck uses Graphs with characters for both name and values Enemy Pokemon is confused
@tejaspatel9766
@tejaspatel9766 4 жыл бұрын
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.
@eleanor7894
@eleanor7894 6 жыл бұрын
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 6 жыл бұрын
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.
@CalvinJKu
@CalvinJKu 6 жыл бұрын
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?
@RyanLeiTaiwan
@RyanLeiTaiwan 7 жыл бұрын
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!
@justinmendiguarin3626
@justinmendiguarin3626 7 жыл бұрын
for(Node child : node.adjacent) { if (!visited.contains(child.id)) { visited.add(child.id); nextToVisit.add(child); } }
@depshallburn
@depshallburn 6 жыл бұрын
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?
@KhanSlayer
@KhanSlayer 6 жыл бұрын
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.
@youngcitybandit
@youngcitybandit 5 жыл бұрын
@@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".
@KhanSlayer
@KhanSlayer 5 жыл бұрын
@@youngcitybandit valid point
@zy4663
@zy4663 2 жыл бұрын
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.
@tonyfadin
@tonyfadin 5 жыл бұрын
All who got confused did not pass the interview.
@pravin4256
@pravin4256 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?
@mandar.vaidya
@mandar.vaidya 4 жыл бұрын
Hey Gayle , very nice explanation !!! do you have graph traversal videos in C also ? If yes , can you please provide links.
@Ltsoftware3139
@Ltsoftware3139 2 жыл бұрын
I'm sorry! I really tried to follow your explanation, but it's too confusing.
@hakoHiyo271
@hakoHiyo271 3 жыл бұрын
Sorry, I like your other videos, but this video I'm confused...
@somerandomguy000
@somerandomguy000 3 жыл бұрын
HashSet x = HashMap? so you didn't even mind to run the algo? nice!
@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.
@connorbunch3577
@connorbunch3577 2 жыл бұрын
1:39 When she said "boop boop boop boop boop", that really hit me
@m0tivati0n71
@m0tivati0n71 3 жыл бұрын
Did anyone manage to make this work? and has the complete code.
@RobertJohnson-fm4vl
@RobertJohnson-fm4vl 7 жыл бұрын
How do you find the cost of a path?
@videovideoguy
@videovideoguy 6 жыл бұрын
A graph data structure can be represented by using a Map as well
@shwethasubbu3385
@shwethasubbu3385 2 жыл бұрын
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 2 жыл бұрын
She also explains it towards the end of the video with an example so that helps
@aspanbu
@aspanbu 3 жыл бұрын
Run this in .75 speed for better understanding!!
@doctor3397
@doctor3397 5 жыл бұрын
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.
@liuculiu8366
@liuculiu8366 3 жыл бұрын
books on my desk: python, java, c boos on legends' desks: python, java, c, Korean, French, Russian......
@arunraj2527
@arunraj2527 7 жыл бұрын
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.
@daoduyemi
@daoduyemi 7 жыл бұрын
I believe she used the remove() method that returns the removed element and not the boolean.
@MrAvinashBhattarai
@MrAvinashBhattarai 7 жыл бұрын
She could define her own methods.
@johncanessa2250
@johncanessa2250 4 жыл бұрын
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.
@brianotte8696
@brianotte8696 5 жыл бұрын
This was fantastic! I'm so grateful this is on KZbin
@hfontanez98
@hfontanez98 4 жыл бұрын
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.
@LTTheReal
@LTTheReal 3 жыл бұрын
Her use of variables in place of the list values confused me so badly, why go for a simple explanation while aiming for a confusing visual approach? Readability is important here.
@hernanvelazquez1421
@hernanvelazquez1421 2 жыл бұрын
I did not see that she used nodeLookup (the hashMap at the beginning). :(
@drormik
@drormik 2 жыл бұрын
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.
@UnitedLionForPeace
@UnitedLionForPeace 6 жыл бұрын
It's so confusing! Why are you renaming nodes when they already have letters on it? Instead of 'G' node you write 'S', instead of 'R' you're writing 'a'??? Why on earth you have to do this? Just keep it simple, from G to R, from R to A and so on and so forth. No need for extra letters...
@pavelerokhin1512
@pavelerokhin1512 4 жыл бұрын
I just love Gayle! The best explanation ever. See also her book!
@HillChris1234
@HillChris1234 6 жыл бұрын
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?
@ulrikalundholm5458
@ulrikalundholm5458 4 жыл бұрын
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?
@codingwithflavio8534
@codingwithflavio8534 2 жыл бұрын
This video is unclear, im sorry but its true
@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.
@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
@jonneymendoza
@jonneymendoza 4 жыл бұрын
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
@MyBinaryLife
@MyBinaryLife 3 жыл бұрын
Really should have used numbers for the nodes on that first graph instead of the same letters youd then use as variables to represent totally different nodes later, but i got it after rewatching over a few times.
@Grassmpl
@Grassmpl 3 жыл бұрын
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?
@metabolic_jam
@metabolic_jam 4 жыл бұрын
Why would you use letters all over the place for nodes? 😓 More confusing than it needs to be
@abeechr
@abeechr 7 жыл бұрын
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.
@chronocide
@chronocide 4 жыл бұрын
She didn't use visited HashMap for the depth first search - how would this ever terminate?
@AlexandrBorschchev
@AlexandrBorschchev 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!
@onkar-nu4oi
@onkar-nu4oi 7 жыл бұрын
Has anyone actually compiled and executed this program in Java? Would you mind sharing the source code, please? I'm still confused as to how am I supposed to create a graph in a main() method? I mean how can I create all these nodes and then perform this DFS/BFS traversals? What's the point of making "Node" class 'static' and it's constructor 'private'? How to create the instances of Node class then? Can anyone please help me out? Thanks.
@eggyeggbean
@eggyeggbean Ай бұрын
I like how her recursive sound is BOOPBOOPBOOP. Mine is FFFFRRRRRRRR (like the sound of cards being shuffled).
@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
@ififif31
@ififif31 6 жыл бұрын
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; }
@mishalubich9366
@mishalubich9366 4 жыл бұрын
A well explained video on this topic. Thank you.
@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!!!
@martijndegeus3275
@martijndegeus3275 3 жыл бұрын
WTF this is not helping!
@navjotsandhu3373
@navjotsandhu3373 2 жыл бұрын
nice tutorials! :)
@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?
@ankitavyas7996
@ankitavyas7996 7 жыл бұрын
What is to be written in the getNode() method in dfs? Please provide link for the code
@kidou123456
@kidou123456 7 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Eyal is Correct!
@Sen0719
@Sen0719 7 жыл бұрын
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; }
@JohnTheHumbleMan
@JohnTheHumbleMan 3 жыл бұрын
Why the Node class has a private constructor? (line 7)
@JohnTheHumbleMan
@JohnTheHumbleMan 3 жыл бұрын
Ok I see, "if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor"
@g7-smart-logistic-app
@g7-smart-logistic-app 6 жыл бұрын
Is there is a lag in the explanation at 1:13. I feel something going weird.
Algorithms: Solve 'Lonley Integer' Using Bit Manipulation
3:22
HackerRank
Рет қаралды 61 М.
Data Structures: Trees
9:57
HackerRank
Рет қаралды 1 МЛН
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,3 МЛН
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 58 МЛН
Algorithms: Memoization and Dynamic Programming
11:17
HackerRank
Рет қаралды 969 М.
Algorithms: Bit Manipulation
9:06
HackerRank
Рет қаралды 537 М.
Depth First Search DFS
8:39
Anitha Ramesh
Рет қаралды 13 М.
Graph Search Visualization in Python (BFS and DFS)
19:12
NeuralNine
Рет қаралды 20 М.
Lecture 13: Breadth-First Search (BFS)
50:48
MIT OpenCourseWare
Рет қаралды 702 М.
Top 8 Data Structures for Coding Interviews
14:00
NeetCode
Рет қаралды 157 М.
How To Pass Technical Interviews When You Suck At LeetCode
14:32