Clone Graph - Depth First Search - Leetcode 133

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024

Комментарии • 99

  • @NeetCode
    @NeetCode  3 года назад +18

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @voski4904
    @voski4904 Год назад +91

    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 Год назад +2

      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 10 месяцев назад +3

      This is how I've approached this task too, now came here to look if there's another solution in this video

    • @sudamlasi2007
      @sudamlasi2007 3 месяца назад

      thank you this helped!

    • @benpurcell591
      @benpurcell591 2 месяца назад +1

      This is what I did. Still O(n) right? And way easier to reason about for me personally

  • @VladimirTheAesthete
    @VladimirTheAesthete 2 года назад +40

    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 2 месяца назад +1

      Python is a cheatcode for some leetcode questions! Like question 371 Sum of Two Integers.

    • @danyeun01
      @danyeun01 2 месяца назад

      how would the compiler link each node on its own

  • @arkamukherjee457
    @arkamukherjee457 2 года назад +92

    A general coding advice, avoid naming a variable copy, as it can confuse the code reviewer about library copy functions.

  • @shivangitomar5557
    @shivangitomar5557 3 года назад +36

    Finally understood and coded it myself after the theory explanation! Thanks :)

  • @dumdumbringgumgum2940
    @dumdumbringgumgum2940 2 года назад +24

    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 Год назад +5

    I'm gonna use a HashMap like pretty much every Graph Problems. Thank you :>

  • @ivantishchenko4686
    @ivantishchenko4686 2 года назад +8

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

  • @halahmilksheikh
    @halahmilksheikh 2 года назад +4

    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 года назад +4

      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 Год назад +1

      Hey, can you share the demo?

  • @castorseasworth8423
    @castorseasworth8423 Год назад +8

    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 4 месяца назад

      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]

  • @halcyonramirez6469
    @halcyonramirez6469 Год назад +5

    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

  • @lajoskicsi6910
    @lajoskicsi6910 2 года назад +3

    It's intereting to mention the Main.Node object in leetcode implements the __hash__() dunder method.

  • @allen724
    @allen724 3 года назад +10

    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 года назад +21

      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?

  • @aaen9417
    @aaen9417 Год назад +8

    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 3 месяца назад

      Hi did you land a job man ?

    • @1000timka
      @1000timka 3 дня назад

      @@satadhi LOL

  • @ashutoshbind8068
    @ashutoshbind8068 Год назад

    Super helpful! Solved the linked list cloning q too with the same approach of hashing the old refs to the new ones

  • @madhursrivastava1762
    @madhursrivastava1762 4 месяца назад

    Thankyou for providing such detailed solution with explanation.

  • @ZVEKOfficial
    @ZVEKOfficial 2 года назад +11

    What is the point of this question ? Where do we use this in real life

    • @symbol767
      @symbol767 2 года назад +15

      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.

  • @dollyvishwakarma2
    @dollyvishwakarma2 2 года назад +4

    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 :)

  • @DBagg-zz4ip
    @DBagg-zz4ip 4 месяца назад

    I did BFS with a 101-long list (according to constraints) instead of hashmap+visited set.

  • @nikhildinesan5259
    @nikhildinesan5259 3 года назад +1

    😀😀....was stuck in this same question from few days...

  • @netraamrale3850
    @netraamrale3850 Год назад

    Amazing as always...!! Thanks for all the free content...!!!

  • @MP-ny3ep
    @MP-ny3ep Год назад

    Very beautiful explanation. Thank you !

  • @irisi3308
    @irisi3308 3 года назад +6

    Great explanation! I have a quick question - how do you prevent the code from looping through the nodes seen?

    • @salimzhulkhrni1610
      @salimzhulkhrni1610 3 года назад +12

      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.

  • @divyanshubaranwal3791
    @divyanshubaranwal3791 4 месяца назад

    Wowww, it was really simple explanation.

  • @tomarintomarin9520
    @tomarintomarin9520 2 года назад +1

    NeetCode is the best

  • @madhumithakolkar_
    @madhumithakolkar_ 9 месяцев назад

    Great solution as always, It would however be better to add the check >> if not node : return None :)

  • @brecoldyls
    @brecoldyls 2 года назад +12

    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 Год назад +1

      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.

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

  • @aziz1329
    @aziz1329 2 года назад

    so first we create copies and then we make connections, right? if we follow the dfs

  • @chenhaibin2010
    @chenhaibin2010 2 года назад +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.

  • @yunyihuang9476
    @yunyihuang9476 Год назад

    could someone explain line 20 for me? How does it save all the neighbors' copy into the current node's list?

  • @giannizamora7247
    @giannizamora7247 2 года назад

    thank you for the explantion NC!

  • @VarunMittal-viralmutant
    @VarunMittal-viralmutant 2 года назад +1

    How is that the custom class could be added as dictionary keys ? Maybe classes on Leetcode have defined some __hash__() methods ?

    • @NeetCode
      @NeetCode  2 года назад +3

      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.

  • @krateskim4169
    @krateskim4169 Год назад

    Awesome explanation

  • @yilmazbingol4838
    @yilmazbingol4838 2 года назад

    in dfs, we return `copy`. Is not it supposed to be the clone of first Node.

  • @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!

  • @ifeanyionyejekwe4584
    @ifeanyionyejekwe4584 3 года назад +6

    Space and time complexity?

    • @yilmazbingol4838
      @yilmazbingol4838 3 года назад +1

      I think time complexity O(N*N) because inside for loop we are making recursive calls. space complexity is O(N)

    • @dss963
      @dss963 Год назад +1

      @@yilmazbingol4838 nope , ultimate time complexity is n

  • @JLJConglomeration
    @JLJConglomeration 10 месяцев назад

    I thought Python keys have to be immutable? I'm confused how you're assigning mutable objects (Node objects) as keys

  • @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)

  • @SM-si2ky
    @SM-si2ky 2 года назад +2

    i generally like graph problems but this one is a little unintuitive & confusing

  • @ployapinon6345
    @ployapinon6345 2 года назад

    Clever! thanks for your post :)

  • @yashsrivastava5128
    @yashsrivastava5128 2 года назад

    i get an error when i create map of Node,Node ,Has any has java solution similar to this logic

  • @madhavkwatra5888
    @madhavkwatra5888 2 месяца назад

    How can you come up with solutions so easily ?
    where i struggle alot.
    Graphs are super hard 😭

  • @chilly2171
    @chilly2171 2 года назад +2

    11:15 the function will always return the root node.

  • @Whiteroca
    @Whiteroca 2 года назад +3

    What are u working as, NeetCode? Thanks

    • @triscuit5103
      @triscuit5103 2 года назад +7

      Hey man - I heard he was a dentist, learning programming as a hobby.

    • @Whiteroca
      @Whiteroca 2 года назад

      @@triscuit5103 That's pretty cool

    • @jesseli5038
      @jesseli5038 2 года назад +3

      @@Whiteroca he works at google

    • @Whiteroca
      @Whiteroca 2 года назад

      @@jesseli5038 Oh alright thanks

    • @studyaccount794
      @studyaccount794 2 года назад +3

      @@triscuit5103 Hey everyone, welcome back. Let's fix some teeth today.

  • @netraamrale3850
    @netraamrale3850 11 месяцев назад

    What is time and space complexity?

  • @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 11 месяцев назад

      Remove #s in Comment

  • @vaalarivan_p
    @vaalarivan_p Год назад +1

    dne: 5:00

  • @mightyprogrammer2899
    @mightyprogrammer2899 2 месяца назад +1

    Somehow Leetcode has deleted this problem

  • @aalapjethwa6452
    @aalapjethwa6452 5 месяцев назад

    simple solution: return deepcopy(node)

  • @VineetKrGupta
    @VineetKrGupta 4 месяца назад

    Day -17 of being unemployed and preparing for an interview. I was laid off recently

  • @river.
    @river. Год назад

    underrated

  • @mehmetnadi8930
    @mehmetnadi8930 Год назад

    mind blowing

  • @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 11 месяцев назад

    This problem sounds more complicated than it actually is.

  • @farazahmed7
    @farazahmed7 Год назад +2

    This question should be marked easy

  • @hwang1607
    @hwang1607 Год назад

    Why can’t you use a set instead of a map

  • @cryptshar6759
    @cryptshar6759 10 месяцев назад

    nice

  • @youssefel-shabasy833
    @youssefel-shabasy833 4 месяца назад

    BFS is much better for this problem

  • @timilehinbakare7263
    @timilehinbakare7263 10 месяцев назад

    its slow because you used dfs

  • @dr.merlot1532
    @dr.merlot1532 2 года назад

    若しかして。。。

  • @unitycatalog
    @unitycatalog 2 года назад

    What if the node IDs are not unique

    • @connorwelch6265
      @connorwelch6265 2 года назад +1

      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])