Тёмный

Clone Graph - Depth First Search - Leetcode 133 

NeetCode
Подписаться 764 тыс.
Просмотров 197 тыс.
50% 1

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Twitter: / neetcode1
Discord: / discord
Coding Solutions: • Coding Interview Solut...
Problem Link: neetcode.io/problems/clone-graph
0:00 - Read the problem
1:30 - Drawing Explanation
8:02 - Coding Explanation
leetcode 133
This question was identified as a google interview question from here: github.com/xizhengszhang/Leet...
#clone #graph #python

Наука

Опубликовано:

 

25 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 98   
@NeetCode
@NeetCode 3 года назад
🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤
@voski4904
@voski4904 Год назад
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 11 месяцев назад
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 9 месяцев назад
This is how I've approached this task too, now came here to look if there's another solution in this video
@sudamlasi2007
@sudamlasi2007 Месяц назад
thank you this helped!
@benpurcell591
@benpurcell591 Месяц назад
This is what I did. Still O(n) right? And way easier to reason about for me personally
@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 28 дней назад
Python is a cheatcode for some leetcode questions! Like question 371 Sum of Two Integers.
@danyeun01
@danyeun01 17 дней назад
how would the compiler link each node on its own
@shivangitomar5557
@shivangitomar5557 3 года назад
Finally understood and coded it myself after the theory explanation! Thanks :)
@arkamukherjee457
@arkamukherjee457 2 года назад
A general coding advice, avoid naming a variable copy, as it can confuse the code reviewer about library copy functions.
@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. 👏👍
@ashutoshbind8068
@ashutoshbind8068 Год назад
Super helpful! Solved the linked list cloning q too with the same approach of hashing the old refs to the new ones
@MP-ny3ep
@MP-ny3ep Год назад
Very beautiful explanation. Thank you !
@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.
@netraamrale3850
@netraamrale3850 Год назад
Amazing as always...!! Thanks for all the free content...!!!
@minhthinhhuynhle9103
@minhthinhhuynhle9103 Год назад
I'm gonna use a HashMap like pretty much every Graph Problems. Thank you :>
@giannizamora7247
@giannizamora7247 2 года назад
thank you for the explantion NC!
@ployapinon6345
@ployapinon6345 2 года назад
Clever! thanks for your post :)
@halcyonramirez6469
@halcyonramirez6469 11 месяцев назад
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
@madhursrivastava1762
@madhursrivastava1762 3 месяца назад
Thankyou for providing such detailed solution with explanation.
@nikhildinesan5259
@nikhildinesan5259 3 года назад
😀😀....was stuck in this same question from few days...
@lajoskicsi6910
@lajoskicsi6910 Год назад
It's intereting to mention the Main.Node object in leetcode implements the __hash__() dunder method.
@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 Год назад
Hey, can you share the demo?
@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 Месяц назад
Hi did you land a job man ?
@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?
@krateskim4169
@krateskim4169 Год назад
Awesome explanation
@divyanshubaranwal3791
@divyanshubaranwal3791 2 месяца назад
Wowww, it was really simple explanation.
@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 :)
@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.
@yunyihuang9476
@yunyihuang9476 Год назад
could someone explain line 20 for me? How does it save all the neighbors' copy into the current node's list?
@aziz1329
@aziz1329 Год назад
so first we create copies and then we make connections, right? if we follow the dfs
@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!
@tomarintomarin9520
@tomarintomarin9520 2 года назад
NeetCode is the best
@yilmazbingol4838
@yilmazbingol4838 2 года назад
in dfs, we return `copy`. Is not it supposed to be the clone of first Node.
@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.
@yashsrivastava5128
@yashsrivastava5128 2 года назад
i get an error when i create map of Node,Node ,Has any has java solution similar to this logic
@khandmo
@khandmo 11 месяцев назад
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.
@DBagg-zz4ip
@DBagg-zz4ip 3 месяца назад
I did BFS with a 101-long list (according to constraints) instead of hashmap+visited set.
@castorseasworth8423
@castorseasworth8423 11 месяцев назад
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 3 месяца назад
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]
@timothyhuang7353
@timothyhuang7353 2 года назад
Do you know how I can input it on and IDE, I realized Node is commented out on leetcode.
@RC-bm9mf
@RC-bm9mf 9 месяцев назад
Remove #s in Comment
@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.
@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.
@mehmetnadi8930
@mehmetnadi8930 Год назад
mind blowing
@madhumithakolkar_
@madhumithakolkar_ 8 месяцев назад
Great solution as always, It would however be better to add the check >> if not node : return None :)
@river.
@river. Год назад
underrated
@netraamrale3850
@netraamrale3850 9 месяцев назад
What is time and space complexity?
@chilly2171
@chilly2171 2 года назад
11:15 the function will always return the root node.
@JLJConglomeration
@JLJConglomeration 9 месяцев назад
I thought Python keys have to be immutable? I'm confused how you're assigning mutable objects (Node objects) as keys
@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
@nytymedarktruths7765
@nytymedarktruths7765 11 месяцев назад
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)
@Whiteroca
@Whiteroca 2 года назад
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.
@SM-si2ky
@SM-si2ky 2 года назад
i generally like graph problems but this one is a little unintuitive & confusing
@cryptshar6759
@cryptshar6759 8 месяцев назад
nice
@aalapjethwa6452
@aalapjethwa6452 3 месяца назад
simple solution: return deepcopy(node)
@vaalarivan_p
@vaalarivan_p Год назад
dne: 5:00
@madhavkwatra5888
@madhavkwatra5888 21 день назад
How can you come up with solutions so easily ? where i struggle alot. Graphs are super hard 😭
@hwang1607
@hwang1607 11 месяцев назад
Why can’t you use a set instead of a map
@brecoldyls
@brecoldyls 2 года назад
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 Год назад
4 js langs, try to cache node.val not node itself.
@hanif3661
@hanif3661 Год назад
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; ```
@MTX1699
@MTX1699 10 месяцев назад
This problem sounds more complicated than it actually is.
@VineetKrGupta
@VineetKrGupta 2 месяца назад
Day -17 of being unemployed and preparing for an interview. I was laid off recently
@farazahmed7
@farazahmed7 10 месяцев назад
This question should be marked easy
@whattokeepbro.5915
@whattokeepbro.5915 2 месяца назад
sahi kaha ekdum
@mightyprogrammer2899
@mightyprogrammer2899 Месяц назад
Somehow Leetcode has deleted this problem
@dr.merlot1532
@dr.merlot1532 2 года назад
若しかして。。。
@youssefel-shabasy833
@youssefel-shabasy833 3 месяца назад
BFS is much better for this problem
@timilehinbakare7263
@timilehinbakare7263 9 месяцев назад
its slow because you used dfs
@unitycatalog
@unitycatalog 2 года назад
What if the node IDs are not unique
@connorwelch6265
@connorwelch6265 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.
@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])
Далее
Evaluate Division - Leetcode 399 - Python
17:37
Просмотров 25 тыс.
Course Schedule - Graph Adjacency List - Leetcode 207
15:47
How I would learn Leetcode if I could start over
18:03
Просмотров 367 тыс.
Rotate Image - Matrix - Leetcode 48
15:46
Просмотров 218 тыс.
Красиво, но телефон жаль
0:32
Просмотров 1,5 млн
НОВЫЕ ФЕЙК iPHONE 🤯 #iphone
0:37
Просмотров 109 тыс.
Новые iPhone 16 и 16 Pro Max
0:42
Просмотров 956 тыс.