Clone Graph | Leetcode - 133 | Google, Facebook, Amazon | DFS+BFS | Explanation ➕ Live Coding🧑🏻‍💻

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

ความคิดเห็น • 69

  • @anuppatankar4294
    @anuppatankar4294 ปีที่แล้ว +15

    Graph was very tough for me but because of your graph concepts playlist today I can solve most of the graph questions easily. Thanks a Lot 😊

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +5

      You made my day ❤️❤️❤️

    • @shivanshchauhan52
      @shivanshchauhan52 ปีที่แล้ว +4

      @@codestorywithMIK Keep going this channel future is very bright !!!

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +2

      Thank you so much Shivansh ❤️❤️❤️

    • @kumkumslab5811
      @kumkumslab5811 9 หลายเดือนก่อน +1

      prooved
      @@shivanshchauhan52

  • @codestorywithMIK
    @codestorywithMIK  ปีที่แล้ว +12

    The most detailed and deep dived explanation to this problem you will ever find. Hope you will like it.
    Please find all the code below :
    //Approach-1 (DFS) Using vector as map
    //Approach-2 (DFS) (Using unordered_map)
    //Approach-3 (BFS) Using vector as map
    //Approach-4 (BFS) (Using unordered_map)
    Link : github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว +4

    speechless with this masterpiece

  • @oqant0424
    @oqant0424 ปีที่แล้ว +5

    Yaar bhaiya kya padhaya hai aapne ...........dil khush kar diya

  • @JJ-tp2dd
    @JJ-tp2dd ปีที่แล้ว +3

    Outstanding bhai! You prove every time why you are the undisputed graph king 👑💙💙

  • @iamnoob7593
    @iamnoob7593 2 วันที่ผ่านมา +1

    Thank you , Amazing explanation.

  • @nk-fs8bj
    @nk-fs8bj 9 หลายเดือนก่อน +2

    Awesome after listing story of 20 minutes write code by own🥳

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว +1

    16:59 ---> That's one of the reasons I come to your channel. I am always sure that I will understand more from your explanations.

  • @HarshSharma-zz9di
    @HarshSharma-zz9di ปีที่แล้ว +1

    Was able to do today's question without any help, but still here just to like the video. Amazing channel and well structured videos!

  • @gauravbanerjee2898
    @gauravbanerjee2898 9 หลายเดือนก่อน +2

    Solved today's GFG POTD after watching this video thank you so much bhaiya ❤❤

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak 11 หลายเดือนก่อน +3

    Awesome !! Worth watching 41mins

    • @codestorywithMIK
      @codestorywithMIK  11 หลายเดือนก่อน +1

      Thank you so much for watching 😇❤️🙏

  • @r.beditz3674
    @r.beditz3674 ปีที่แล้ว +1

    Never regret watching your explanation videos.❤
    Thanks again sir. Here are the Java implementations of your BFS and DFS approaches -:
    BFS CODE :
    class Solution {
    Map map;
    public Node cloneGraph(Node node) {
    map = new HashMap();
    if(node == null)
    return null;
    Node clone = new Node(node.val);
    map.put(node, clone);
    Queue q = new ArrayDeque();
    q.add(node);
    while(!q.isEmpty()){
    Node curr = q.poll();
    Node currClone = map.get(curr);
    for(Node n : curr.neighbors){
    if(!map.containsKey(n)){
    Node c = new Node(n.val);
    map.put(n, c);
    currClone.neighbors.add(c);
    q.add(n);
    }
    else
    currClone.neighbors.add(map.get(n));
    }
    }
    return clone;
    }

    }
    ----------------------------------------------------------------------------------------------------
    DFS CODE :
    class Solution {
    Map map;
    public Node cloneGraph(Node node) {
    map = new HashMap();
    if(node == null)
    return null;
    Node clone = new Node(node.val);
    map.put(node, clone);
    dfs(node, clone);
    return clone;
    }
    private void dfs(Node node, Node clone){
    for(Node n : node.neighbors){
    if(!map.containsKey(n)){
    Node curr = new Node(n.val);
    map.put(n, curr);
    clone.neighbors.add(curr);
    dfs(n, curr);
    }
    else
    clone.neighbors.add(map.get(n));
    }
    }
    }

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      Thank you so so much
      Means a lot to me ❤️

  • @ShivamMishra-fo1wx
    @ShivamMishra-fo1wx 9 หลายเดือนก่อน

    Great Explanation Makes the question very easy to understand and not only graph u make easy dp also

  • @fshimcds
    @fshimcds 2 หลายเดือนก่อน +1

    ThankU for best Explanation

  • @rahularyan9808
    @rahularyan9808 9 หลายเดือนก่อน

    Really great explanation

  • @shivkumar-og4ow
    @shivkumar-og4ow ปีที่แล้ว +1

    thanks a lot, bhaiya, nice explanations, are easy to understand.

  • @level_up.1908
    @level_up.1908 ปีที่แล้ว +3

    bhai kyu padhate hon itna badhiya 😍

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว +2

    The K.I.N.G of Graphs

    • @JJ-tp2dd
      @JJ-tp2dd ปีที่แล้ว

      so true!

  • @tutuimam3381
    @tutuimam3381 ปีที่แล้ว +1

    Thanks a lot

  • @udayshankar-e6v
    @udayshankar-e6v 7 หลายเดือนก่อน +2

    Ye channel bahut aage jane vale h.. bahut bawal samjhata h ye bhai 😍bhai bs bata do kon sa aap use karte ho drawing ke liye .. main bhi install kar lunga..interview me bada proplem hota h diagram vagairh bana ke show karne me

    • @codestorywithMIK
      @codestorywithMIK  7 หลายเดือนก่อน +1

      Hi there
      I use default notes app ❤️🙏
      Thank you ☺️

  • @gui-codes
    @gui-codes 5 หลายเดือนก่อน +2

    A message to new comers to this channel - "Graph is Hard until you watch codestorywithMIK"

  • @souvickmaity4867
    @souvickmaity4867 ปีที่แล้ว +1

    Shi Haai bhai

  • @aryanvashishtha0001
    @aryanvashishtha0001 ปีที่แล้ว +2

    i love you bro

  • @abhiverma5135
    @abhiverma5135 ปีที่แล้ว +1

    why you wrote map.clear() at the beginning of the function ? whereas we hadn't put any value in the map we just declared it globally?
    BTW great content ❤✌✌

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +3

      Hi abhi,
      Thanks a lot for your appreciation.
      Actually multiple calls for the function is made. So we don’t want stale entries in the map to be there when a new call is made to the function.
      But most of the time , even if you don’t clear it, the solution works.
      But it’s a good practice to do that.

  • @jyotishmoydeka6804
    @jyotishmoydeka6804 ปีที่แล้ว +1

    Bhaiya it's showing your github page is not available.. and btw thank you for wonderfull explainantion..

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Sorry for the typo.
      Link corrected.
      github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp

  • @vishalgautam-f6v
    @vishalgautam-f6v ปีที่แล้ว +1

    bro u are great at explaining things but u should change ur channel name to a short and simple name, so that we can also recommend ur channel to others as ur current name is slightly difficult to remember

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +3

      I really appreciate your suggestion ❤️❤️❤️
      I will plan to change it to CSM (Code Story With MIK).
      Right now, my Instagram, linkedIn and , Facebook all have this name and hence might take time to migrate.
      Thank you so much for your precious suggestions and watching my videos ❤️❤️❤️

  • @satvrii
    @satvrii ปีที่แล้ว +2

    ❤❤❤❤

  • @badamadevi9497
    @badamadevi9497 ปีที่แล้ว

    Sir java ka code bhi explain krr

  • @YashSinghal
    @YashSinghal ปีที่แล้ว +1

    github link in the description for this problem has a typo its a space instead of underscore so %20 instead of _

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Thank you so much Yash. Corrected now.
      github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp

  • @bhartendupant8859
    @bhartendupant8859 9 หลายเดือนก่อน +1

  • @ankitraj4179
    @ankitraj4179 3 หลายเดือนก่อน

    Java Solution
    /*
    // Definition for a Node.
    class Node {
    public int val;
    public List neighbors;
    public Node() {
    val = 0;
    neighbors = new ArrayList();
    }
    public Node(int _val) {
    val = _val;
    neighbors = new ArrayList();
    }
    public Node(int _val, ArrayList _neighbors) {
    val = _val;
    neighbors = _neighbors;
    }
    }
    */
    class Solution {
    public Node cloneGraph(Node node) {

    if(node == null) return node ;
    Node clone = new Node(node.val) ;
    HashMap map = new HashMap() ;
    map.put(node,clone) ;
    DFS(node,clone,map) ;
    return clone ;
    }
    public static void DFS(Node org , Node clone , HashMap map){
    for(Node neigh : org.neighbors){
    if(!map.containsKey(neigh)){
    Node tempNode = new Node(neigh.val) ;
    clone.neighbors.add(tempNode) ;
    map.put(neigh,tempNode) ;
    DFS(neigh,tempNode,map) ;
    }
    else{
    clone.neighbors.add(map.get(neigh));
    }
    }
    }
    }

    • @ankitraj4179
      @ankitraj4179 3 หลายเดือนก่อน

      do like if it helped.

  • @riyakansal3312
    @riyakansal3312 ปีที่แล้ว +1

    Error is coming while opening ur github:(

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      Link corrected. Can you try again please
      github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Graph/Clone%20Graph.cpp

  • @kartikkk4583
    @kartikkk4583 ปีที่แล้ว +2

    Hogya bhai ye, recursion + map, time laga thoda dimag lagane me, @mik bhai aapne competetive programming ki hai kya :
    unordered_map m;
    Node* cloneGraph(Node* node) {
    if(!node)return node;
    Node* root= new Node(node->val);
    vector v;
    m[node]=root;
    for( auto x : node->neighbors){
    if(m.find(x)==m.end()){
    v.push_back(cloneGraph(x));
    }
    else{
    v.push_back(m[x]);
    }
    }
    root->neighbors=v;
    return root;
    }

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Hi Kartik,
      No, i have not done much in Competitive Coding. But would love to try

  • @akshayzade9761
    @akshayzade9761 ปีที่แล้ว +1

    Thanks Mike for efforts :)
    Please find below Java Code -
    class Solution {
    Map map = new HashMap();
    public Node cloneGraph(Node node) {

    if(node == null){
    return null;
    }
    Node clone_node = new Node(node.val);
    map.put(node, clone_node);
    dfs_traversal(node, clone_node);
    return clone_node;
    }
    void dfs_traversal(Node node, Node clone_node){
    for(Node n : node.neighbors){
    if(!map.containsKey(n)){
    Node clone = new Node(n.val);
    map.put(n, clone);
    clone_node.neighbors.add(clone);
    dfs_traversal(n, clone);
    }
    else{
    clone_node.neighbors.add(map.get(n));
    }
    }
    }
    }

  • @JJ-tp2dd
    @JJ-tp2dd ปีที่แล้ว

    Why is my comment getting removed?😮

    • @JJ-tp2dd
      @JJ-tp2dd ปีที่แล้ว

      tried several times to paste the Java code but it is getting removed every time automatically 😥😥

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      Hi JJ
      Can you try again. I am also not sure why this is happening

    • @souravjoshi2293
      @souravjoshi2293 ปีที่แล้ว +1

      Was waiting for your java code bro

    • @JJ-tp2dd
      @JJ-tp2dd ปีที่แล้ว

      @@codestorywithMIK @asimzohr2293 added, hoping it does not get removed

    • @JJ-tp2dd
      @JJ-tp2dd ปีที่แล้ว

      @@codestorywithMIK it got removed again it seems 😐

  • @IshanGuleria-l6b
    @IshanGuleria-l6b 5 หลายเดือนก่อน

    iska iterative dfs ka solution h kisi k pass to please send

    • @vinitsinghengineer
      @vinitsinghengineer 4 หลายเดือนก่อน

      unordered_map mp;
      void dfs(Node* node){
      Node* temp = new Node(node->val);
      mp[node] = temp;
      for(auto it : node->neighbors){
      if(mp.find(it) != mp.end()) temp->neighbors.push_back(mp[it]);
      else {
      dfs(it);
      temp->neighbors.push_back(mp[it]);
      }
      }
      }
      Node* cloneGraph(Node* node) {
      if(!node) return NULL;
      dfs(node);
      return mp[node];
      }

  • @abcd76820
    @abcd76820 9 หลายเดือนก่อน +1

    class Solution{
    Node cloneGraph(Node node){
    Node temp = new Node(node.val);
    //ab clone krna h recursively iske neighbours ko
    // saath me ek map bna lete hain taaki koi node bn gya h
    // to dubara nahi bnega
    //map ek purana node usk corresponding new node jo
    //hmne bnaya
    Map map = new HashMap();
    map.put(node,temp);
    DFS(node,temp,map);
    return temp;
    }
    void DFS(Node node,Node temp,Map map)
    {
    //original node k neighbours dhund kr clone krte jaao
    for(Node neigh : node.neighbors)
    {
    // ab do case ya to clone hua hoga ya to nai
    if(!map.containsKey(neigh))
    {
    Node cloneNeigh = new Node(neigh.val);
    map.put(neigh,cloneNeigh);
    temp.neighbors.add(cloneNeigh);
    DFS(neigh,cloneNeigh,map);
    }
    else
    {
    temp.neighbors.add(map.get(neigh));
    }
    }
    }
    }

  • @sarthakgudwani4608
    @sarthakgudwani4608 ปีที่แล้ว +2

    Java Implementation
    class Solution
    {
    HashMap mp = new HashMap();

    void DFS(Node node, Node clone_node)
    {
    for (Node n : node.neighbors)
    {
    if (!mp.containsKey(n))
    {
    Node clone = new Node(n.val);
    mp.put(n, clone);
    clone_node.neighbors.add(clone);
    DFS(n, clone);
    }
    else
    {
    clone_node.neighbors.add(mp.get(n));
    }
    }
    }
    Node cloneGraph(Node node)
    {
    if (node == null)
    {
    return null;
    }
    mp.clear();
    Node clone_node = new Node(node.val);
    mp.put(node, clone_node);
    DFS(node, clone_node);
    return clone_node;
    }
    }

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 9 หลายเดือนก่อน

    class Solution {
    Node cloneGraph(Node node){
    Node st=node;
    HashSetvis=new HashSet();
    HashMapmpp=new HashMap();
    dfs(st,vis,mpp);
    addEdges(mpp);
    return mpp.get(st);
    }
    public static void addEdges(HashMapmpp){
    for(Map.Entery e:mpp.entrySet()){
    Node u=(Node)e.getKey();
    mpp.get(e.getKey()).neighbors.add(u);
    }
    }
    public void dfs(Node st,HashSetvis,HashMapmpp){
    vis.add(st);
    Node copyNode=new Node(st.val);
    mpp.put(st,copyNode);
    for(Node v:st.neighbors){
    if(!vis.contains(v))
    dfs(v,vis,mpp);
    }
    }
    }
    🎉❤