Find if Path Exists in Graph | Easiest Explanation | Microsoft | Leetcode 1971 | codestorywithMIK

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ม.ค. 2025

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

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

    Time Complexity of Both Approaches : O(m+n) ,
    where m = number of edges
    n = number of vertices
    We build an adjacent list of all m edges in graph which takes O(m).
    Each node is added to the queue and popped from the queue once, it takes O(n) to handle all nodes.
    Total : O(m+n)

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

      I used disjoint set-
      class dj{
      public:
      vector parent;
      vector size;
      dj(int n){
      parent.resize(n);
      size.resize(n);
      for(int i=0;isize[pu]){
      size[pv]+=size[pu];
      parent[pu]=pv;
      }
      else{
      size[pu]+=size[pv];
      parent[pv]=pu;
      }
      }
      };
      class Solution {
      public:
      bool validPath(int n, vector& edges, int source, int destination) {
      dj d(n);
      for(int i=0;i

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

      Awesome ❤️😇

  • @gauravbanerjee2898
    @gauravbanerjee2898 8 หลายเดือนก่อน +17

    Jinhone bhaiya ka graphs and concept playlist kia hai unke liye ye question halwa hai 😂😂❤❤. Thanks a lot bhaiya ❤❤

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

    This channel is a GEM 💎

  • @aws_handles
    @aws_handles 8 หลายเดือนก่อน +1

    You are the best. I had completed your Graph concepts playlist and these Qns I can do on my own.

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

    Solved on my own .........all because of your graph playlist ......Thank u so much from the bottom of my heart💖💖

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

      I am so happy that we are now able to solve and think about Graph qns on our own. Thank you for sharing your experience 🙏🙏❤️❤️

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

    I did this Question using DSU.
    Thank you so much Bhaiya, Now i start feeling Confident in Graphs.
    You just liked my comment, U may be awake, I want you to know that Im very very Grateful🫂

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

      So glad to know ♥️😇
      Yes I was awake. Travelling ✈️

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

    I was able to solve this question by my own after completing your Graph concepts and Questions Playlist...It helped me a lot in improving my graph concepts. Thank you so much Bhaiya😊

  • @amanverma7694
    @amanverma7694 4 หลายเดือนก่อน +1

    Solved on my own, using Union find, all thanks to your graph playlist

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

      So happy to see this.
      Feel free to share your code here so that others can also benefit from it ❤️🙏😇

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

      Thanks for the reply ❤❤
      Here is my code in Java
      class DisjointSet{
      int[] parent, rank;
      int n;
      DisjointSet(int n){
      this.n = n;
      parent = new int[n];
      rank = new int[n];
      makeSet();
      }
      public void makeSet(){
      for(int i=0;irank[yRoot])parent[yRoot] = xRoot;
      else if(rank[yRoot]>rank[xRoot])parent[xRoot] = yRoot;
      else{
      parent[yRoot] = xRoot;
      rank[xRoot]++;
      }
      }
      }
      class Solution {
      public boolean validPath(int n, int[][] edges, int source, int destination) {
      DisjointSet ds = new DisjointSet(n);
      for(int i=0;i

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

      Thanks for the reply ❤
      Here is my Union find code in Java
      class DisjointSet{
      int[] parent, rank;
      int n;
      DisjointSet(int n){
      this.n = n;
      parent = new int[n];
      rank = new int[n];
      makeSet();
      }
      public void makeSet(){
      for(int i=0;irank[yRoot])parent[yRoot] = xRoot;
      else if(rank[yRoot]>rank[xRoot])parent[xRoot] = yRoot;
      else{
      parent[yRoot] = xRoot;
      rank[xRoot]++;
      }
      }
      }
      class Solution {
      public boolean validPath(int n, int[][] edges, int source, int destination) {
      DisjointSet ds = new DisjointSet(n);
      for(int i=0;i

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

    done using 3 methods DFS,BFS,DSU

  • @impatientgaming9868
    @impatientgaming9868 8 หลายเดือนก่อน +1

    good one

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

    Please make playlist on Graphs. And thanks for this video.

  • @nikhilbhatta3065
    @nikhilbhatta3065 8 หลายเดือนก่อน +1

    bhaiya computer fundamentals jo interview mey puchi jati hai unpe bhi playlist suuru kardo ya batado kaha se kya kya aur kitna padna hai !!

  • @codeguru55
    @codeguru55 8 หลายเดือนก่อน +1

    "Minimums number of operation to satisfy Conditions", pe bhi vidio bna dijie

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

    This channel is G.O.L.D

  • @aizad786iqbal
    @aizad786iqbal 8 หลายเดือนก่อน

    DFS line 12-13 ... idk c++ much, but vector int is like an int array??, so how are you pushing the int there?

  • @manishv.8167
    @manishv.8167 2 ปีที่แล้ว +2

    Please start the Playlist for not only graph but also for other as well till advance

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

      Hi Manish, i have planned to cover all topics. Soon planning. One by one
      Thanks for watching ❤️❤️❤️

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

    bhaiya daily challenge k sath sath leetcode best question ka topicwise playlist karao sirf leetcode

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

    You are just awesome

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

    bro can you make video explanation of letter combination of a phone number from leetcode

  • @iamnoob7593
    @iamnoob7593 3 หลายเดือนก่อน +1

    Thank you , Can be done using DSU as well. dsu.findPar(src) == dsu.findPar(dest) return true else false

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

    In line 15 of your code for DFS in leetcode IDE, before doing the recursive call check if the node in the map is already visited.
    if (visited[node] === false) {
    // do the recursive call here, it'll reduce number of recursive calls for already visited nodes.
    // you can remove line 8 & 9, as you're already checking the same before the recursive call.
    }

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

      Indeed.
      Thank you so much for pointing this out ❤️❤️❤️

    • @Ybash2979
      @Ybash2979 6 หลายเดือนก่อน

      Thanks dude and thats imp since it can lead to multiple waste calls leading to TLE

  • @EB-ot8uu
    @EB-ot8uu 8 หลายเดือนก่อน

    100th Like in this video 🤩

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

    This was my code:
    public static boolean validPath(int n, int[][] edges, int source, int destination) {

    List adj = new ArrayList();
    for (int i = 0; i < n; i++) {
    adj.add(new ArrayList());
    }
    int[] vis = new int[n];
    // creating the adjacency list.
    for (int[] list : edges) {
    int u = list[0];
    int v = list[1];
    adj.get(u).add(v);
    adj.get(v).add(u);
    }
    return dfs(source, destination, adj, n, vis);
    }
    private static boolean dfs(int node, int destination, List adj, int n, int[] vis) {
    vis[node] = 1;

    if (node == destination) {
    return true;
    }
    for(Integer it : adj.get(node)) {
    if (vis[it] == 0 && dfs(it, destination, adj, n, vis)) {
    return true;
    }
    }
    return false;
    }
    support++
    reach++

  • @shabananoor9423
    @shabananoor9423 8 หลายเดือนก่อน

    ❤❤

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

    done [24.12.23] ✅✅

  • @kumkumslab5811
    @kumkumslab5811 8 หลายเดือนก่อน

    mera TLE maar rha 30th (lat) test case par

    • @codestorywithMIK
      @codestorywithMIK  8 หลายเดือนก่อน

      Share your code

    • @akshaychavan5511
      @akshaychavan5511 8 หลายเดือนก่อน

      Try this -
      def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
      paths = collections.defaultdict(lambda: list())
      # build graph using adjacency list
      for [v1, v2] in edges:
      paths[v1].append(v2)
      paths[v2].append(v1)
      # apply DFS
      visited = [False] * n
      def dfs(s, d):
      if s == d: return True
      if visited[s]: return False
      visited[s] = True
      res = False
      for v in paths[s]:
      res = res or dfs(v, d)
      return res
      return dfs(source, destination)

    • @kumkumslab5811
      @kumkumslab5811 8 หลายเดือนก่อน

      @@codestorywithMIK class Solution {
      public:
      bool solve( unordered_map&adjList,int s,int d,vector&vis){
      if(s==d)return 1;
      if(vis[s]==1)return 0;
      vis[s]=1;
      for(auto ele:adjList[s]){
      if(solve(adjList,ele,d,vis))return 1;
      }
      return 0;
      }
      bool validPath(int n, vector& edges, int source, int destination) {
      unordered_mapadjList;
      for(auto i:edges){
      adjList[i[0]].push_back(i[1]);
      adjList[i[1]].push_back(i[0]);
      }
      vectorvis(n+1,0);
      return solve(adjList,source,destination,vis);

      }
      };

    • @Ybash2979
      @Ybash2979 6 หลายเดือนก่อน +2

      @@kumkumslab5811 same mera bhi tle de rh

  • @Engineering.Wallah
    @Engineering.Wallah 8 หลายเดือนก่อน

    We have used visted array only because it's a bidirectional graph?

    • @tejasjhamnani7724
      @tejasjhamnani7724 8 หลายเดือนก่อน

      No ,
      even if it's a directional graph ,
      still we will be using the visited array ,
      consider an example where there is an edge from 3 --> 5 and another edge from 5 --> 3 ,
      in that case the code will go from node 3 to node 5 and again from node 5 to node 3 (Because we have not marked the nodes visited), so it will visit the nodes again and again and this will be repeated and it will give you TLE/Memory LIMIT Exceeded.

    • @Engineering.Wallah
      @Engineering.Wallah 8 หลายเดือนก่อน +1

      @@tejasjhamnani7724 thanks

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

      Thanks @tejashamnani7724 . You are correct ❤️