Clone Graph - Depth First Search - Leetcode 133

  Рет қаралды 230,033

NeetCode

NeetCode

Күн бұрын

Пікірлер: 105
@NeetCode
@NeetCode 3 жыл бұрын
🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
@voski4904
@voski4904 2 жыл бұрын
A more brute force way to think about this problem. This is a good way to think about it before jumping into the solution given by NeetCode. #1. Use a hashmap to track originals to their clones (same as NC). #2. Traverse the original graph, visiting each node once, for each node just clone it's value without the neighbors. #3. Traverse the original graph again, visiting each node once, for each node find it''s clone and set the original's neighbors clones as the clone's neighbors. #4. return oldToNew[node]
@lewisw29
@lewisw29 Жыл бұрын
This is more of a bfs thinking and is more straightforward. I began with this approach. Bit more code but easier to think about and understand
@JohnDoe-mr6mq
@JohnDoe-mr6mq Жыл бұрын
This is how I've approached this task too, now came here to look if there's another solution in this video
@sudamlasi2007
@sudamlasi2007 6 ай бұрын
thank you this helped!
@benpurcell591
@benpurcell591 5 ай бұрын
This is what I did. Still O(n) right? And way easier to reason about for me personally
@vitzal3691
@vitzal3691 2 ай бұрын
I did this too at first, then refined it to one loop, then saw this dfs solution.
@VladimirTheAesthete
@VladimirTheAesthete 2 жыл бұрын
Funny thing is that in Python all of this can be done with return copy.deepcopy(node). Your interviewer will probably not be impressed by that though.
@tofumakesvideos
@tofumakesvideos 4 ай бұрын
Python is a cheatcode for some leetcode questions! Like question 371 Sum of Two Integers.
@danyeun01
@danyeun01 4 ай бұрын
how would the compiler link each node on its own
@amynguy
@amynguy Ай бұрын
@@danyeun01 it keeps track of circular references
@arkamukherjee457
@arkamukherjee457 2 жыл бұрын
A general coding advice, avoid naming a variable copy, as it can confuse the code reviewer about library copy functions.
@shivangitomar5557
@shivangitomar5557 3 жыл бұрын
Finally understood and coded it myself after the theory explanation! Thanks :)
@dumdumbringgumgum2940
@dumdumbringgumgum2940 2 жыл бұрын
First Clone(1) which Clones(2) and connects back to 1 (since it is already in the map) then Clones(3) which connects back to 2 and Clones(4) which connects back to 1 and 3. Now 4's neighbors are exhausted therefore 4 is returned to Clone(3) which returns 3 to Clone(2) which returns 2 to Clone(1). Only thing left from 1 is 4. Clone(4) now just returns 4 back to Clone(1) making the last connection.
@minhthinhhuynhle9103
@minhthinhhuynhle9103 2 жыл бұрын
I'm gonna use a HashMap like pretty much every Graph Problems. Thank you :>
@halahmilksheikh
@halahmilksheikh 2 жыл бұрын
I made a stackblitz and the algorithm doesn't happen exactly you wrote. The recursive nature makes it hard to draw out but what I found was that the 1->2 and 1->3 are BOTH the last edges to be made. As the call stack unwinds is when the edges are created (for the most part). I have a demo if anyone's interested
@zachtheyek
@zachtheyek 2 жыл бұрын
I was wondering this too, but tbh I think it was more for the sake of explanation than accuracy; in reality, the algorithm presented visits each node, makes a copy, adds both to the hash map, then continues down the stack until it reaches the original node again, at which point it starts popping back up the stack, and then only does it add the edges (connections).
@itachid
@itachid 2 жыл бұрын
Hey, can you share the demo?
@ivantishchenko4686
@ivantishchenko4686 2 жыл бұрын
Amazing, thank you for your explanation! You are doing a fantastic job. Please don't stop! This is how studying should be like! Education should be accessible to everyone. 👏👍
@halcyonramirez6469
@halcyonramirez6469 Жыл бұрын
i have a much easier time understanding these than subarray problems.. I managed to solve this on my own. this is one was so much easier to me than the ones maxsubarray and subarray related problems. intution is simple. have a hashmap the key being the of the node val and the value being the node itself. if node val not in hashmap and node val and create a new node. traverse neighbors and check if neighbor value is in hashmap. if it is then just get it from the hashmap and don't recurse. the beauty of this is initially your node will not have neighbors in the hashmap but since you are storing references the references get updated. and in the end will give you the correct answer
@castorseasworth8423
@castorseasworth8423 Жыл бұрын
There is an alternative to recursive. Iterative BFS solution: class Solution: def cloneGraph(self, node: 'Node') -> 'Node': if not node: return q, visited = deque([node]), set() clones = defaultdict(Node) while q: n = q.popleft() if n not in visited: clones[n].val = n.val for neighbor in n.neighbors: clones[neighbor].val = neighbor.val clones[n].neighbors.append(clones[neighbor]) q.extend(n.neighbors) visited.add(n) return clones[node]
@youssefel-shabasy833
@youssefel-shabasy833 7 ай бұрын
if not node: return None clonedNode = { node: Node(node.val) } queue = deque([node]) while queue: curr = queue.popleft() for neighbor in curr.neighbors: if neighbor not in clonedNode: clonedNode[neighbor] = Node(neighbor.val) queue.append(neighbor) clonedNode[curr].neighbors.append(clonedNode[neighbor]) return clonedNode[node]
@lajoskicsi6910
@lajoskicsi6910 2 жыл бұрын
It's intereting to mention the Main.Node object in leetcode implements the __hash__() dunder method.
@ashutoshbind8068
@ashutoshbind8068 Жыл бұрын
Super helpful! Solved the linked list cloning q too with the same approach of hashing the old refs to the new ones
@allen724
@allen724 3 жыл бұрын
Hi, thanks a lot for the video. May I ask at 1:38 why you commented that we use HashMap like every other graph problem? Sorry I am new to graph problems. Is Map a standard way to track the relationships of node to neighbours or something?
@NeetCode
@NeetCode 3 жыл бұрын
Yes, a common way of representing a graph is with an Adjacency List. It's usually implemented with a Hashmap because they are efficient. So for each nide, you map it to a list of it's neighbors.
@allen724
@allen724 3 жыл бұрын
@@NeetCode Thanks bro. Ive learned a lot from your channel. Can you please do a video on Graph cycle detection / top sort using in degree?
@Bra1nstormeR
@Bra1nstormeR 7 ай бұрын
Thankyou for providing such detailed solution with explanation.
@nikhildinesan5259
@nikhildinesan5259 3 жыл бұрын
😀😀....was stuck in this same question from few days...
@DBagg-zz4ip
@DBagg-zz4ip 7 ай бұрын
I did BFS with a 101-long list (according to constraints) instead of hashmap+visited set.
@irisi3308
@irisi3308 3 жыл бұрын
Great explanation! I have a quick question - how do you prevent the code from looping through the nodes seen?
@salimzhulkhrni1610
@salimzhulkhrni1610 3 жыл бұрын
preventing iteration through the same nodes that were seen already is handled by map. If a node is already seen, that means we would have already had a cloned node stored in the map. In that case, we just return the cloned node(already seen node) from the map and add it to its current cloned node's neighbor list. If we did not have the check, then we would have the case of infinite cycle.
@ZVEKOfficial
@ZVEKOfficial 2 жыл бұрын
What is the point of this question ? Where do we use this in real life
@symbol767
@symbol767 2 жыл бұрын
That's the joke, we don't use 90% of this garbage in real life since we have prebuilt stuff on the job we will use. This is just mainly nonsense to pass the interview.
@aaen9417
@aaen9417 Жыл бұрын
At this point, even after I land a job, I will keep watching your videos just to enjoy the simplicity and elegance of your solutions
@satadhi
@satadhi 5 ай бұрын
Hi did you land a job man ?
@1000timka
@1000timka 2 ай бұрын
@@satadhi LOL
@draugno7
@draugno7 Ай бұрын
You mean IF, since there is always one person as smart and as in practice as Neetcode to take that job from us :D I got a scholarship for gifted people at uni but still there's always that someone (an Asian who does it better :') ). And my local jobs pay half of the US salary at most
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Very beautiful explanation. Thank you !
@div9210
@div9210 6 ай бұрын
Wowww, it was really simple explanation.
@az.akkkttt
@az.akkkttt 2 жыл бұрын
so first we create copies and then we make connections, right? if we follow the dfs
@tomarintomarin9520
@tomarintomarin9520 2 жыл бұрын
NeetCode is the best
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome explanation
@whatisnextthen9836
@whatisnextthen9836 11 күн бұрын
can we solve this problem using constant space ?
@khandmo
@khandmo Жыл бұрын
If every node is undirected can't that be created upon finding it as a neigbor? Then you would only need a set to contain the cloned nodes.
@chenhaibin2010
@chenhaibin2010 2 жыл бұрын
thanks for the great explanation. I had thought to do DFS-backtrack, I need to remove the nodes that added to hashmap. Why it is not the case in this problem. How should I consider whether I need to remove a state from hashmap when I face a DFS-backtrack problem?
@meowmaple
@meowmaple 2 жыл бұрын
You don't need to do backtracking for this question as the first if statement already checks whether a node is already in the hasmap, and thus has already been visited, and returns if that is the case
@zekonja90
@zekonja90 2 жыл бұрын
You are using HashMap to see if you already visited node, as well to see if node is copied.
@dollyvishwakarma2
@dollyvishwakarma2 2 жыл бұрын
Hey @NeetCode, please come up with a playlist of videos that talk about the basic concepts of Python that come in handy for CP. P.S: really appreciate your videos! Kudos on such great content :)
@netraamrale3850
@netraamrale3850 Жыл бұрын
What is time and space complexity?
@madhumithakolkar_
@madhumithakolkar_ Жыл бұрын
Great solution as always, It would however be better to add the check >> if not node : return None :)
@yilmazbingol4838
@yilmazbingol4838 2 жыл бұрын
in dfs, we return `copy`. Is not it supposed to be the clone of first Node.
@VarunMittal-viralmutant
@VarunMittal-viralmutant 2 жыл бұрын
How is that the custom class could be added as dictionary keys ? Maybe classes on Leetcode have defined some __hash__() methods ?
@NeetCode
@NeetCode 2 жыл бұрын
Yes in python they do
@VarunMittal-viralmutant
@VarunMittal-viralmutant 2 жыл бұрын
@@NeetCode Thanks for confirming that. But if I try to write same code on an IDE first, I'll have to do that myself.
@ifeanyionyejekwe4584
@ifeanyionyejekwe4584 3 жыл бұрын
Space and time complexity?
@yilmazbingol4838
@yilmazbingol4838 3 жыл бұрын
I think time complexity O(N*N) because inside for loop we are making recursive calls. space complexity is O(N)
@dss963
@dss963 Жыл бұрын
@@yilmazbingol4838 nope , ultimate time complexity is n
@SM-si2ky
@SM-si2ky 2 жыл бұрын
i generally like graph problems but this one is a little unintuitive & confusing
@giannizamora7247
@giannizamora7247 3 жыл бұрын
thank you for the explantion NC!
@Whiteroca
@Whiteroca 3 жыл бұрын
What are u working as, NeetCode? Thanks
@triscuit5103
@triscuit5103 2 жыл бұрын
Hey man - I heard he was a dentist, learning programming as a hobby.
@Whiteroca
@Whiteroca 2 жыл бұрын
@@triscuit5103 That's pretty cool
@jesseli5038
@jesseli5038 2 жыл бұрын
@@Whiteroca he works at google
@Whiteroca
@Whiteroca 2 жыл бұрын
@@jesseli5038 Oh alright thanks
@studyaccount794
@studyaccount794 2 жыл бұрын
@@triscuit5103 Hey everyone, welcome back. Let's fix some teeth today.
@JLJConglomeration
@JLJConglomeration Жыл бұрын
I thought Python keys have to be immutable? I'm confused how you're assigning mutable objects (Node objects) as keys
@netraamrale3850
@netraamrale3850 Жыл бұрын
Amazing as always...!! Thanks for all the free content...!!!
@yunyihuang9476
@yunyihuang9476 Жыл бұрын
could someone explain line 20 for me? How does it save all the neighbors' copy into the current node's list?
@pahulhallan563
@pahulhallan563 Ай бұрын
why can't i just traverse the old graph, and each node, create a new and add to the new empty graph?
@chilly2171
@chilly2171 3 жыл бұрын
11:15 the function will always return the root node.
@aalapjethwa6452
@aalapjethwa6452 7 ай бұрын
simple solution: return deepcopy(node)
@vaalarivan_p
@vaalarivan_p Жыл бұрын
dne: 5:00
@river.
@river. Жыл бұрын
underrated
@ployapinon6345
@ployapinon6345 2 жыл бұрын
Clever! thanks for your post :)
@nytymedarktruths7765
@nytymedarktruths7765 Жыл бұрын
Isn't your OldToNew in the wrong scope? The function is going to be called for each node, so you're not actually doing a Deep Clone as Node(1)'s neighbor Node(2) is not the same as Node(2), it's its own instance. Setting Node[1].neighbors[0].val = 22, will not affect Node[2].val. Basically you're creating n*n-1 objects instead of n because the OldToNew is in the function scope instead of a global scope (I know global is frowned upon, but Leet Code's validation system requires it in this specific instance)
@realyangfan
@realyangfan 2 жыл бұрын
Can someone tell me what is the type 'Node' in python? why not just Node? Is it not the Single quotation marks String in python? Thanks!
@yashsrivastava5128
@yashsrivastava5128 2 жыл бұрын
i get an error when i create map of Node,Node ,Has any has java solution similar to this logic
@brecoldyls
@brecoldyls 3 жыл бұрын
Neetcode, have you considered a career in education and research? I feel as though you would be an excellent professor in algorithms and data structures, and would enjoy conducting research in algorithms. One of the difficulties of pursuing such a career path is that professors and researchers often have to seek out and apply for their own funding; however since you have a talent for articulating ideas and are clearly a self-motivated individual I think you would actually enjoy this aspect of it. Have you considered this?
@jointcc2
@jointcc2 Жыл бұрын
I have a gut feeling he won't be interested. Leetcodes are designed for bridging the gap between what you learned in the academia and what you would actually use in a day to day job. In the academia it's mostly just Maths and it's also very hard to make it fun to learn.
@hanif3661
@hanif3661 2 жыл бұрын
4 js langs, try to cache node.val not node itself.
@hanif3661
@hanif3661 2 жыл бұрын
version B: ``` if (!graph) return graph; function dfs (node) { if (node.copy === undefined) { node.copy = new Node(node.val); for (let n of node.neighbors) { node.copy.neighbors.push( dfs(n) ) } } return node.copy; } dfs(graph); return graph.copy; ```
@hwang1607
@hwang1607 Жыл бұрын
Why can’t you use a set instead of a map
@madhavkwatra5888
@madhavkwatra5888 4 ай бұрын
How can you come up with solutions so easily ? where i struggle alot. Graphs are super hard 😭
@MTX1699
@MTX1699 Жыл бұрын
This problem sounds more complicated than it actually is.
@mehmetnadi8930
@mehmetnadi8930 2 жыл бұрын
mind blowing
@VineetKrGupta
@VineetKrGupta 6 ай бұрын
Day -17 of being unemployed and preparing for an interview. I was laid off recently
@farazahmed7
@farazahmed7 Жыл бұрын
This question should be marked easy
@whattokeepbro.5915
@whattokeepbro.5915 6 ай бұрын
sahi kaha ekdum
@AustinCS
@AustinCS Ай бұрын
Nah, you're wrong.
@youssefel-shabasy833
@youssefel-shabasy833 7 ай бұрын
BFS is much better for this problem
@mightyprogrammer2899
@mightyprogrammer2899 5 ай бұрын
Somehow Leetcode has deleted this problem
@cryptshar6759
@cryptshar6759 Жыл бұрын
nice
@timilehinbakare7263
@timilehinbakare7263 Жыл бұрын
its slow because you used dfs
@unitycatalog
@unitycatalog 2 жыл бұрын
What if the node IDs are not unique
@cmelch
@cmelch 2 жыл бұрын
It shouldn't matter. When we create the copy of the node we're just setting the value of the original as the value of the copy but the actual copy itself is a completely different unique object. Much of the same reason we are hashing the deep copy to the original they hold the same value but they are NOT the same node. So if two Nodes exist in the graph with the same value for example. We would create two different unique Node objects in memory that just happen to hold the same value. The actual objects in memory are different so when they exist in the hash they are represented as two unique items. When we look up an object in the hash we are comparing the object in memory and not the value the object holds.
@K4RD050
@K4RD050 2 ай бұрын
even if you used the actual values, the problem states that the values go from 1 - n (for simplicity)
@dr.merlot1532
@dr.merlot1532 2 жыл бұрын
若しかして。。。
@RaksohOap
@RaksohOap 2 жыл бұрын
I figure out an alternative recursive solution that for me is more intuitive. hash_map = {node: Node(node.val)} def dfs(current): if current: for n in current.neighbors: if not n in hash_map: hash_map[n] = Node(n.val) dfs(n) hash_map[current].neighbors.append(hash_map[n])
@skylakefreak3665
@skylakefreak3665 2 ай бұрын
return copy.deepcopy(node)
Word Ladder - Breadth First Search - Leetcode 127 - Python
17:06
Top 5 Most Common Graph Algorithms for Coding Interviews
13:01
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 133 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Clone Graph - Leetcode 133 - Graphs (Python)
13:38
Greg Hogg
Рет қаралды 3,8 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 500 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
Amazon Coding Interview Question - Clone Graph (LeetCode)
13:56
AlgosWithMichael
Рет қаралды 33 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 577 М.
Course Schedule - Graph Adjacency List - Leetcode 207
15:47
NeetCode
Рет қаралды 374 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 300 М.