Shortest Path Visiting All Nodes | Clean | Simple | Intuitive | BFS | GOOGLE | Leetcode-847 |

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 134

  • @insertname6
    @insertname6 Рік тому +21

    OMG! Never seen such fine explainations on all of youtube. It's really important to know and understand the thinking process for a question, if not more important than the solution itself because it helps you to think in similar ways for similar questions. Thought process is really helpful in coding interviews.
    Really good work. Keep it up, Sir!

  • @codestorywithMIK
    @codestorywithMIK  Рік тому +16

    Hi Everyone,
    If you want the Complete Dry Run of Entire Example , let me know, I will create another video for that ❤️❤️❤️
    Thank you all

    • @ms-ej4gd
      @ms-ej4gd Рік тому +3

      Yes

    • @FanIQQuiz
      @FanIQQuiz Рік тому +1

      Bhai maine khud se dry run karliya. Thanks to you.

    • @showsshorts4696
      @showsshorts4696 Рік тому +1

      sir explain so well that we dont need explanation again

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      @showsshorts4696 😇❤️🙏

  • @thefinalfit
    @thefinalfit Рік тому +9

    I can bet, no one can teach intuition building like you. Bring any famous youtuber, this guy stands alone 🔥🔥🔥
    Thank you so much ❤

  • @shabananoor9423
    @shabananoor9423 Рік тому +3

    The intuition building was phenomenal 😲😲😲🔥🔥🔥

  • @Utkarsh_Singh_
    @Utkarsh_Singh_ Рік тому +3

    This man deserves so much more yyr. Never Ever seen someone with such a clean and elegant code, with dry run and complete intuition, ill do my best to make sure my friends start studying from you only from now on.

  • @karthikkaushik7559
    @karthikkaushik7559 Рік тому +3

    This is nothing but pure gold thank you so much!

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Means a lot 😇❤️

    • @karthikkaushik7559
      @karthikkaushik7559 Рік тому

      Hi Mik,
      I had another question I know you have iterated in the video but just for clarification I wanted to know for any problem in graphs if there is a shortest path if we use BFS from any index will it by default be the shortest path?
      Regards,
      Karthik Kaushik

  • @adilkhatri7475
    @adilkhatri7475 Рік тому +2

    just because of you meri leetcode consistency maintained hai. 50 days ho chuka hai max streak. thank you so much for your amazing content ❤🙌

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      50 days 🔥🔥👌👌👌
      Keep it up
      Thank you 😇❤️🙏

  • @Saryupareen
    @Saryupareen Рік тому +6

    next level teaching

  • @FanIQQuiz
    @FanIQQuiz Рік тому +1

    Salute hai bhai aise unique teaching skill ko. No one clears intuition better than you.

  • @iamnoob7593
    @iamnoob7593 2 місяці тому +1

    Man what a marvelous explanation with that bit manipulation technique to check if we are going to redundant state. Thank you.

  • @TheProblemSolvers38
    @TheProblemSolvers38 Рік тому +2

    At 28:10 , When you have popped (0,1), For node 0, you have only added the first neighbour of it. I think you have to add 2 and 3 as well. You directly went to next node.... I'm not pointing out, just asking, Correct me if I'm wrong

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Yes yes, you are 100% correct. Actually i added the caption (subtitles) as the correction for checking all neighbors (might take time to reflect)
      Apologies to have missed it but i really hope the intuition was clear 😇❤️🙏

  • @codeandtalk6
    @codeandtalk6 Рік тому +9

    You are an inspiration bro ❤❤

  • @wearevacationuncoverers
    @wearevacationuncoverers Рік тому +1

    You deserve millions of subscribers. Never seen dedication like this.

  • @ReenaRote
    @ReenaRote Рік тому +3

    Hats off to your explanation! Your thought process and your strategy for building intuition are outstanding! I do have a small suggestion, though. It would be greatly helpful if you could include inline comments when writing functions in your Git repository. Thank you!

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Feedback taken. Thank you so much for the suggestion. I will definitely include more comments in my code in github 😇🙏

  • @souravjoshi2293
    @souravjoshi2293 Рік тому

    One of the finest explanations. I did the dry run on my own. Loved the intuition building.
    Thanks a lot

  • @umeshbisht1054
    @umeshbisht1054 Рік тому +1

    Thanku Bhaiya ❤ and congratulations for 9k

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Thank you so much for trusting my channel 🙏😇❤️

  • @youcantgetme1126
    @youcantgetme1126 Рік тому +1

    Bhai yard... I'm Wordless... You are so underrated yaar... you deserve many more subs... Love you bhai 🥰🥰🥰..... And thank you so much yarn bhai.

  • @tutuimam3381
    @tutuimam3381 Рік тому +2

    Amazing❤❤

  • @floatingpoint7629
    @floatingpoint7629 Рік тому +1

    thanks for the superb explanation

  • @ravneetsingh6131
    @ravneetsingh6131 10 місяців тому

    Next Level Undertstanding🔥🔥🔥

  • @girikgarg8
    @girikgarg8 Рік тому +1

    Wonderful explanation ! I remember struggling with this question sometime back, you made it so intuitive!

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      I am so glad the intuition was helpful 😇❤️🙏

  • @sauravchandra10
    @sauravchandra10 Рік тому +1

    Kaafi hard problem tha aaj ka, pr concepts clear hogye kaafi

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Glad to hear that my video helped ❤️❤️❤️

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Also, If you think I should create a separate video in which I should do a complete dry run for the video, then let me know, i will make one for sure ❤️❤️❤️

    • @sauravchandra10
      @sauravchandra10 Рік тому

      Bhaiya give us the chance to do some hard work too :) @@codestorywithMIK

  • @easyvedicmaths886
    @easyvedicmaths886 Рік тому +1

    In java code, why we take 2D array for visited. Why cant we take Set visited?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Yep you can.
      I just tried to show two different ways to use visited 😇❤️

  • @Tejaswi-xd4re
    @Tejaswi-xd4re Рік тому +6

    Sir it wud be helpful if u make a question list video(leetcode )which covers all type of problems..and. after doing those problems we wud be in a position to tackle any other problems..pls sir
    From past 2-3 weeks i am asking this

    • @aadil4236
      @aadil4236 Рік тому +1

      I think right now He only has C++ solutions. We should contribute to his repo in our respective programing language, so it's helpful for other people. He's usually realy busy. He travels a lot and on top of that he makes this amazing videos to explain stuff.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Hi Tejaswi,
      Would you confirm, are you referring to a DSA sheet like ??

    • @mileshsoni5016
      @mileshsoni5016 Рік тому +1

      bhai please bana do ek sheet because bht questions h leetcode p or waha se selective or bht acche questions krna is very difficult, apki teaching style or knowledge bht achi h bhai@@codestorywithMIK

    • @Tejaswi-xd4re
      @Tejaswi-xd4re Рік тому +1

      ​@@codestorywithMIK yes sir

    • @Tejaswi-xd4re
      @Tejaswi-xd4re Рік тому +1

      ​@@codestorywithMIK congratulations for 9k subscribers

  • @parasjain4-yearb.tech.mech441
    @parasjain4-yearb.tech.mech441 Місяць тому +1

    Dhanyawad Bhai

  • @vinaymaurya4895
    @vinaymaurya4895 Рік тому +1

    Congratulations bhaiya🎉🎉🎉 for 9k , you deserves millions❤❤❤

  • @codestorywithMIK
    @codestorywithMIK  Рік тому +3

    Time Complexity : 0(2^N * N^2).
    Notice the while loop that performs the BFS traversal of the graph.
    In the worst case, the while loop can run 2^N iterations, where N is the number of nodes in the graph. This is because the program explores all possible combinations of nodes using bit masking i.e. each bit can be either 0 or 1 (2 possibilities) --> 2^N
    Within each iteration, there is a nested loop that iterates through the neighbors of the current node. In the worst case, this inner loop iterates N times for each node.
    So, the overall time complexity is 0(2^N * N^2).

  • @sejalrai30
    @sejalrai30 Рік тому

    Thank you.
    You explain things very well

  • @mohit_you9270
    @mohit_you9270 Рік тому

    Seeing first time your video, awesom skill bro. Keep it up.

  • @vasujhawar.6987
    @vasujhawar.6987 Рік тому +1

    Subscribed for this solution, i was confused about implementation you cleared my doubt how using bfs will always give us shortest path if put all nodes into queue

  • @gui-codes
    @gui-codes 3 місяці тому +1

    goodness. Loved the explanation.
    #GODOFDSAMIK

  • @pabitrakb5291
    @pabitrakb5291 Рік тому +1

    Great explanation!!

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Thank you so much 😇🙏❤️
      If you think I should create a separate video in which I should do a complete dry run for the video, then let me know, i will make one for sure ❤️❤️❤️

  • @aswithasai4015
    @aswithasai4015 Рік тому +1

    great explanation sir soo helptul tq

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Thank you so much for watching ❤️❤️
      Also, If you think I should create a separate video in which I should do a complete dry run for the video, then let me know, i will make one for sure ❤️❤️❤️

    • @aswithasai4015
      @aswithasai4015 Рік тому

      yess sir if you have time then make a complete video for dry run .i understood and tried to write the code but got a bit confused ,@@codestorywithMIK

  • @VishalKumar-yc3wr
    @VishalKumar-yc3wr 2 місяці тому

    Thank you so much ❤

  • @taneyasoni
    @taneyasoni Рік тому +1

    30:55 when we pop the first element {0,1} Why we do not push {2,5} and {3,9} along with {1,3}. 0 has 3 three neighbours 1,2 and 3 why do we only push {1,3} in the queue and move to {1,2}.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Yes yes, my bad they should have been added too. But i covered adding the neighbors further in the video. I hope that cleared the confusion.
      But please let me know, if you guys need a separate FULL DRY RUN video on this, kindly let me know, i will make one for sure 🙏😇

    • @taneyasoni
      @taneyasoni Рік тому +1

      thank you so much for replying and clearing my doubt. I gave a lot of time to it and then finally decided to write a comment. make a full dry run video, if manageable and add its reference to the title/pinned comment/description of the video.@@codestorywithMIK

  • @yashgaur1716
    @yashgaur1716 Рік тому +2

    I was trying to use normal DFS here, what I was doing was I will check everynode as the starting node to get the minimum.
    In dfs function I am checking all the nodes I can travel, once I make a dfs call to that node, I remove that edge from the graph and after I calculate the ans. I add this edge back to the graph.
    But I am not getting the optimal ans. Can someone help me?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      For getting shortest path, DFS can never give you optimal path.
      Always remember that shortest path can only be found using BFS.
      DFS never guarantees shortest path

    • @yashgaur1716
      @yashgaur1716 Рік тому

      @@codestorywithMIK Thanks broo

  • @Abhay14
    @Abhay14 Рік тому +1

    iss question ko kl re-attempt krunga bahott hi jyada achha question h 1 baar me samajh me nhi aaya

  • @sachinmohanty4577
    @sachinmohanty4577 Рік тому +1

    MIK it was as smooth as it could be ❤

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Thank you so much Sachin ❤️
      please let me know, if you guys need a separate FULL DRY RUN video on this, kindly let me know, i will make one for sure 🙏😇

    • @sachinmohanty4577
      @sachinmohanty4577 Рік тому +1

      @@codestorywithMIK it's okay MIK I think u need not to do the full dry because if we work hard more than u then only it will benefit us 🙂

    • @sachinmohanty4577
      @sachinmohanty4577 Рік тому +1

      @@codestorywithMIK mik one doubt why we are using the while loop while(n--){--} for that n chunk processing what if we don't do that ??

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      @sachinmohanty4577 If you remember I mentioned that we have to start BFS from all nodes at once.
      If we don’t start BFS from all nodes at a chunk (level) at once, then we will never get shortest path guarantee.
      Starting BFS once for a chunk ensures that we are starting BFS from all nodes at once.
      I will suggest you to do a dry run in a small example and try doing BFS one by one instead of chunk by chunk.

    • @sachinmohanty4577
      @sachinmohanty4577 Рік тому

      @@codestorywithMIK so basically nested bfs right??

  • @harsh.jain22
    @harsh.jain22 Рік тому

    Your videos are superb, My placement is near and covering all videos of all playlists is a long process, can you please make a of very imp. and frequently asked questions? Also it would be very great if for each question in the sheet we have a link to your video.
    I know there are lots of sheets available but , I only trust you , please consider this request, it will help a lot to all students.

  • @shivamtomar9658
    @shivamtomar9658 Рік тому +1

    thank you so much sir♥

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Thank you so much for watching ❤️❤️
      Also, If you think I should create a separate video in which I should do a complete dry run for the video, then let me know, i will make one for sure ❤️❤️❤️

    • @shivamtomar9658
      @shivamtomar9658 Рік тому +1

      @@codestorywithMIK no sir I will try from myself ♥️

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      @shivamtomar9658 sure 😇🙏❤️

  • @viveksingh4801
    @viveksingh4801 Рік тому

    Great Explanation !! Do you think that doind the daily challenge everyday consistently would help improve the problem solving skills ?

  • @Engineer_With_A_Life
    @Engineer_With_A_Life Рік тому +2

    Hi Mik, Thankyou for the clear explanation. Awesome Video! as always.
    i have one question, what is the need of bit masking? i mean cant we simply use a 2d array which tracks the state of (parent, child) i.e if i am coming from 0th node and going to 1st node then i will simply set it as isvisited[0][1] = true.
    next time, when i come in this same situation i.e 0 to 1, i can simply check if isvisited[0][1] = true or not.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Hi Ayush,
      That’s such an IMPORTANT qn you asked.
      Actually i took a very small example.
      In big examples- the repeated path can be large as well
      For example : you came to 3 via 0,1,2
      Path - 0 -> 1 -> 2 -> 3
      Suppose you came to 3 via 0,4,2,3
      Here if you notice, you came to 3 from 2 but the entire path is different.
      Hence keeping track of only previous node will not be enough, we need entire trail path to see if we have seen the path before or not.
      Hope this answers your qn.
      And thank you so much for your precious comments ❤️❤️❤️

    • @floatingpoint7629
      @floatingpoint7629 Рік тому +1

      ​@@codestorywithMIK thanks for the answer. i was wondering the same thing

    • @FanIQQuiz
      @FanIQQuiz Рік тому +1

      @@codestorywithMIK Thanks a lot for this. And good Qn bhai

    • @Engineer_With_A_Life
      @Engineer_With_A_Life Рік тому +1

      @@codestorywithMIK thanks for your response, I will dry run few scenarios to see where it fails.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Sure Ayush.
      Just simply write the code as you explained. Then any test case failing will be caught. 😇❤️

  • @Shivam-kz2dg
    @Shivam-kz2dg Рік тому

    Contest k solutions bhi provide kro bhai please

  • @taneyasoni
    @taneyasoni Рік тому +1

    finally 17/30

  • @gomzysharma
    @gomzysharma Рік тому +1

    just silly mistake, the mask for 0 is 0000 35:27

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Hi there,
      Mask for 0 will be - 0001
      Because from 0 it means that 0th bit will be set i.e. we will do (1 (0001)
      I hope that clears 😇❤️🙏
      Thank you so much for watching

  • @RAHULSHARMA-dl1jc
    @RAHULSHARMA-dl1jc Рік тому +1

    Bhaiya can we use unordered set?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      C++ doesn’t allow to use pair inside unordered set. That’s why you will have to use ordered set only ❤️😇

    • @RAHULSHARMA-dl1jc
      @RAHULSHARMA-dl1jc Рік тому

      @@codestorywithMIK thanks bhaiya for this explanation

  • @Rajdweep
    @Rajdweep Рік тому +1

    bro where have you been

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Hi there, actually 15th Sept and 16th sept POTD videos were already present in my playlist -
      15th Sept POTD -
      Using Prim’s - ua-cam.com/video/hsr7KolYDH0/v-deo.htmlsi=fqkY9yq7s42MhvR_
      Using Kruskal’s - ua-cam.com/video/O6wQQtv71S0/v-deo.htmlsi=bJXhfRIhjiXQ49eq
      16th Sept POTD -
      ua-cam.com/video/QIu9HeyEjPc/v-deo.htmlsi=N7C15PELOKP0UjDS
      Hope that helps 😇🙏❤️

    • @Rajdweep
      @Rajdweep Рік тому

      @@codestorywithMIK oh i thought u might have got sick so no videos 🙂

  • @Rajat_maurya
    @Rajat_maurya Рік тому +1

    Thank you

  • @dss963
    @dss963 8 місяців тому

    Nobody would come up with a solution , if they have not seen such problems.

  • @Abhay14
    @Abhay14 Рік тому +1

    and bhaiya isme hmne visited ko set me kyun store kiya h vector me kyun nhi

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      You can use vector also.
      See my Github link in the description.
      I have provided both - using 2-D array for memoization (in Java code) and using set in C++

    • @Abhay14
      @Abhay14 Рік тому

      @@codestorywithMIK ohh , hmm set tabhi use krte h jbb hmme duplicate element nhi chahiye hotte h na

  • @dhairyachauhan6622
    @dhairyachauhan6622 Рік тому +1

    hi bhaiya. Aaj aapse ek general question puchna tha.
    i have some friends who are taking online one-to-one coaching. It is giving me a feeling of lagging behind. I don't think i would be needing any of those coz i have you as my mentor and i have already gone through all the data stuctures. Please guide me how to deal with this situation i am stuck 😔😔.
    My current strategy (my daily routine)
    1) I make sure to solve atleast 5 problems
    2) I understand the question from your video and give myself 1/2 hr -1hr on the approach
    3) If i dont get it i watch the solution save it to my weekly-repeat list and move ahead
    4) Resolve it on a span of 5 days then move it to the monthly repeat section
    5) I give all contests only on leetcode
    6)Upsolve the question on the next day.
    Struggling
    1) I did 60 question on graph then lost touch. Now i know the concepts but i forget some of the details.
    This is what i do daily. I am able to code 30-40% of the questions you do. Is this the right path?Please suggest

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Wow. First of all, “weekly-repeat list”,
      Very rare people do this. But this will be very fruitful in long run. You just never stop this. This is a very good way of revision.
      See, it you are consistent and following the path you showed above, then with time you will keep growing and you will definitely don’t need anyone for help.
      Please never stop giving contests, its a Gem .
      Also, if you forget concepts of Graph, not to worry, you can revisit them in your free times.
      Always remember, even dope coders of Korea, China etc. also forget concepts, they also have to revise timely and they also struggle in some qns. But they keep the practice consistent, and that’s what I want to tell you, be consistent with this strategy and never stop this.
      Trust me, give it some time and you will definitely see change in you.

    • @dhairyachauhan6622
      @dhairyachauhan6622 Рік тому +1

      @@codestorywithMIK thank you as always for guiding me

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      @dhairyachauhan6622 Happy to help.
      But I am really impressed that you keep a weekly revision list. Blows my mind 👍🏻👍🏻👌👌

    • @wearevacationuncoverers
      @wearevacationuncoverers Рік тому

      I also have a list where I keep Problems for revising again after 5-10 days.
      But I often break this routine. Being consistent is tougher than studying DSA I believe. But this channel has helped me a lot to regain my consistency. Hope to be on this path for long until I crack a good company.

  • @samarthsinghthakur
    @samarthsinghthakur Рік тому +1

    wow🎩📴

  • @craftsworld3237
    @craftsworld3237 Рік тому +1

    time complexity?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Time Complexity : 0(2^N * N^2).
      Notice the while loop that performs the BFS traversal of the graph.
      In the worst case, the while loop can run 2^N iterations, where N is the number of nodes in the graph. This is because the program explores all possible combinations of nodes using bit masking i.e. each bit can be either 0 or 1 (2 possibilities) --> 2^N
      Within each iteration, there is a nested loop that iterates through the neighbors of the current node. In the worst case, this inner loop iterates N times for each node.
      So, the overall time complexity is 0(2^N * N^2).

    • @craftsworld3237
      @craftsworld3237 Рік тому

      yeah got it one more question the type of bfs you used in the question isn't same as multisource bfs??
      @@codestorywithMIK

  • @RAHULSHARMA-dl1jc
    @RAHULSHARMA-dl1jc Рік тому

    Bhaiya in first iteration 0 had 3 choices right?

  • @halchal41
    @halchal41 Рік тому +1

    thanksssssssssssssss

  • @FanIQQuiz
    @FanIQQuiz Рік тому

    Sir, Will you reveal your face when you receive Silver Button ? 😃

  • @Abhay14
    @Abhay14 Рік тому +2

    36:33 bhaiya yha pr aapne 0011 OR 0010 = 0011 galat likha h

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Hi Abhay, OR of 0011 and 0010 will be (0011) only.
      Can you please confirm if this is what you are referring to ? ❤️

    • @Abhay14
      @Abhay14 Рік тому +1

      @@codestorywithMIK sorry bhaiya mai confuse ho gya tha maine se socha tha
      Ye kaun sa operation hai bss ye btao do
      0011 OR 0010 = 0101

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      @Abhay14 This is actually OR operation.
      If you remember, OR operation is done like this :
      1 OR 0 = 1
      1 OR 1 = 1
      0 OR 1 = 1
      0 OR 0 = 0
      Hope this helps 😇

  • @RanjeetKumarceb
    @RanjeetKumarceb Рік тому

    why we didn't make adj list 🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

  • @M.m554
    @M.m554 Рік тому +2

    Sir take oa questions also not just leetcode ones🫠🫠...