Ep20 - Rat in a Maze | Recursion | DSA series by Fraz | Notes available in description

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

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

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

    Whenever we want to generate all possible paths or answers, we need to think in terms of Recursion and Backtracking. Backtracking is simply undoing the change which we have already done.
    Summary:
    1. We(mouse) are standing at (0,0) and we need to generate all possible paths so that the mouse reachers the last cell of the maze (n-1,n-1)
    2. We are allowed to move in 4 directions only- Up, Down, Left and Right. We need to return a vector ans containing all possible paths through which the mouse can reach the end of maze.
    3. Base conditions are pretty simple. Firstly, we cannot go beyond the boundaries of the matrix, secondly we cannot go to a cell which contains a blockage that is, matrix[i][j] denotes a blockage or wall and we cannot go through it. Also we need to mark every cell as visited with any other value(Fraz sir has marked with 0 for better understanding) such that we don't come back to the same cell again and again. Lastly, when we reach the target cell (n-1,n-1) we need to put our path string in ans[] vector and return as we have completed our objective.
    a) if(i =n)
    return;
    b) if(matrix[i][j] == 0)
    return;
    c) if(i==n-1 && j==n-1)
    {
    ans.push_back(path);
    return;
    }
    4. Now, we have four options. But according to the problem we need to print the paths in sorted manner so we will move like- Down, Left, Right, Up. And before that we will mark the cell as visited by matrix [i][j] = 0 and Back-track at the end.
    // Mark as visited
    matrix[i][j] = 0;
    // Move Down
    help(i+1,j,path+'D');
    // Move Left
    help(i,j-1,path +'L');
    //Move Right
    help(i,j+1,path + 'R');
    // Move Up
    help(i-1,j,path+'U);
    // Undo the change
    matrix[i][j] = 1;
    5. Recursion will generate all the possible paths to reach end. We don't have to do anything else.
    Time Complexity: O(4^n*n) [ Because in worst case we will have a maze all filled with 1 and we will traverse all the cells to reach the end. And we have n*n cells in the maze and for each cell we have 4 option that's why Time Complexity is O(4^n*n) ]
    Space Complexity: O(n*n)
    [ This is equal to the height of the Recursive Tree and since we need to reach the end (n-1,n-1) from (0,0) that's why we need to traverse the entire matrix. ]

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

      Thanks I think 3.b)if(matrix[i][j] == 0) return; code is missed by Fraz in the video.

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

      @@saurabhN1393 Welcome🤗

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

      @@arghya_0802 I like your notes.Do you maintain notes for Babar DSA or any other series as well? Do you know when Fraz will start uploading DP videos? He is a great teacher.

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

      @@saurabhN1393 No I only commented on Fraz sir's video. And I don't know when the series will get resumed😔

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

    This Guy is Really Important to our Coding Community
    Linked List Series was 💯💯
    Ans now Recursion Series is going to be the Best one too!!

  • @ManikMahajan-b7x
    @ManikMahajan-b7x 7 หลายเดือนก่อน

    🎯 Key Takeaways for quick navigation:
    00:00 *🧩 Recursion Basics and Problem Introduction*
    - Recursion and backtracking explained as interconnected concepts.
    - Introduction to the maze problem where a rat needs to navigate through a grid to reach a destination.
    - Description of the maze grid structure and the objective of reaching a specific destination cell.
    03:52 *🛤️ Understanding Recursive Approach to Maze Problem*
    - Explains how the maze problem is inherently recursive.
    - Describes the recursive nature of exploring all possible paths from a given position to the destination.
    - Emphasizes the need for systematically exploring all possible directions in the maze.
    08:20 *🧭 Implementing Recursive Solution and Backtracking*
    - Details the recursive function for exploring paths in the maze.
    - Discusses the importance of backtracking to avoid revisiting already explored cells.
    - Demonstrates how to mark visited cells and backtrack to explore alternate paths.
    10:25 *📊 Space and Time Complexity Analysis*
    - Analyzes the space complexity related to recursion, emphasizing the height of the recursion tree.
    - Explores the time complexity through the geometric progression of recursive calls.
    - Provides a breakdown of the number of nodes in the recursion tree to deduce the time complexity formula.
    15:36 *💻 Coding the Recursive Solution*
    - Walks through the coding process of implementing the recursive solution in C++.
    - Demonstrates how to handle base cases, explore directions, mark visited cells, and backtrack.
    - Provides a step-by-step explanation of the code implementation.

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

    Yesterday, I was visiting ur channel all the the time for next lecture Over recursion. I couldn't get next lecture then go for sleeping. Now just now I would see that one video would be uploaded on it. There's no any word to tell about ur consistency & dedication. Uh are a such down to earth man for students. Who don't have selfishness natural at all........God bless uh bhaiya!!......One thing it was most awaited video so far " RAT IN THE MAZE".......I will like to meet to uh once bhaiya!!
    Bhaiya.....I would complete all the questions of Contest 1 yesterday well.....Now am waiting for Contest 2!!

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

    Notes:
    Similar problem to word search, gives us more confidence on how backtracking can be used to solve similar problems. Was able to do by making some changes in word search problem's code.
    T: O(4^(n * n))
    S: O(n * n)

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

    Did the question by my own sir, thankyou so much for your recursion course. god bless you 😇

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

    Thanks for amazingly simple solution 🙋🏻‍♂️🤗

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

    Episode 20 completed!!
    Rat in a maze
    Here, we will be given a rat and maze(matrix) and we need to find to find in how many paths the rat can reach the (n-1) (n-1) destination Here, the position in which rat can travel will be denoted by 1 and the positions which are prohibited are denoted by 0
    1) Here, for each position we have four directions D, L, R, U so we can go in all these directions to reach the destination
    2) So we will call recursive functions for all the four directions and ask recursion to do the next step
    3) The base conditions are:-
    i and j cant go negative or out of the grid and if can't go to positions which are prohibited
    4) And also we can travel through the position which we already travelled so while we are travelling we mark that position as 0
    Time complexity:- O(4^n*n)
    Space complexity:- O(n*n)
    Once again amazing Explanation bhaiya!! 😍

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

    Excellent lecture bro keep uploading we keep learning

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

    Thank you for the efforts you put in and your consistency is incredible 🔥💫

  • @Shakib.Siddiqui
    @Shakib.Siddiqui 2 ปีที่แล้ว +1

    Bhai aj me soch rha tha lecture nhi upload kiya......
    Thank you bhai 😎

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

    Your explanation is dope

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

    Bro when I solve this question before your video came it kind of look confusing as I am not able to understand what they are trying to say by not using same cell again and agai again but when u solve it on board then I solve it in one time code before seeing ur code❤❤❤❤❤❤❤❤❤❤❤❤❤

  • @AmitYadav-kr6vf
    @AmitYadav-kr6vf 2 ปีที่แล้ว +1

    Great sir, Thank you❤🌹🙏

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

    You are vrry hardworking bhiya 🥰❤

  • @Lucifer-xt7un
    @Lucifer-xt7un 2 ปีที่แล้ว +1

    Extremely good content bro

  • @HarshitSharma-si4wl
    @HarshitSharma-si4wl 2 ปีที่แล้ว +2

    Very good explanation

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

    bhaiya when basic of programming language with dsa will launched . we are waiting .

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

    Plz start beginners DSA course soon
    We are waiting

  • @Ajaysingh-dg8pi
    @Ajaysingh-dg8pi 11 หลายเดือนก่อน

    Bro can you let me know what are you using to draw out things, I mean which digital pen?

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

    very nice pls keep uploading videos. !

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

    Thankyou so much bhaiya for your content.

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

    Thanks I think if(matrix[i][j] == 0) return; code is missed by Fraz in the video.

  • @PRIYANSHUKUMAR-un9zu
    @PRIYANSHUKUMAR-un9zu 2 ปีที่แล้ว

    Great bhaiya, thanks u so much🔥🔥❤❤

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

    Thankyou bhaiya for this nice session

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

    great video. Good explanation

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

    👍🏻

  • @Abhishek-fo3fc
    @Abhishek-fo3fc 2 ปีที่แล้ว +1

    Done understood ✅❤

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

    thank you bhaiya for have a such good content.

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

    want this video because I was having doubts

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

    Reach++

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

    When beginners DSA course will start

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

    👍💯

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

    Consistency++

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

    Hey bro,, i am hare,😁😁 for u... 😍 thank.u bhAiya🥰

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

    Thank you 🔥🔥

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

    Episode 20 done!

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

    Thanks for the video sir♥

  • @HarshRaj-kp7gk
    @HarshRaj-kp7gk 2 ปีที่แล้ว +1

    #🥰🥰

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

    Bro You are using c++ but i'm Java programmer how would I understand the DSA syntax

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

      Did you understand the concept?

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

    Maja Aggaya bhaiya

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

    day 20
    bohut zyada late ab kal dekhunga, abhi mood nahi hai 12 k baad padne ka

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

      Yesterday, I was visiting ur channel all the the time for next lecture Over recursion. I couldn't get next lecture then go for sleeping. Now just now I would see that one video would be uploaded on it. There's no any word to tell about ur consistency & dedication. Uh are a such down to earth man for students. Who don't have selfishness natural at all........God bless uh bhaiya!!......One thing it was most awaited video so far " RAT IN THE MAZE".......I will like to meet to uh once bhaiya!!

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

    How we will solve find the number of even numbers can be formed using the digits from 0 to 9 such that 3 and 5 are not adjacent.

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

    In which language.. the solution of problem is coded?

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

    ThnQ ones again

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

    Fraz is better than didi and bhaiya who just rant I am for you.

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

    codes kab aayenge

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

    #include
    void solve(vector&a,char ch,int x,int y,string &s,vector&ans,vector&d){
    if(xa[0].size()-1||a[x][y]!=1||d[x][y]!=-1){
    return;
    }
    if(x==a.size()-1&&y==a.size()-1){
    s.push_back(ch);
    ans.push_back(s);
    s.pop_back();//backtracking is required to check different possible cases in a given question
    return;
    }
    if(x==0&&y==0){
    }
    else{
    s.push_back(ch);
    }
    d[x][y]=0;
    solve(a,'L',x,y-1,s,ans,d);
    solve(a,'R',x,y+1,s,ans,d);
    solve(a,'D',x+1,y,s,ans,d);
    solve(a,'U',x-1,y,s,ans,d);
    s.pop_back();
    d[x][y]=-1;
    }
    vector < string > searchMaze(vector < vector < int >> & arr, int n) {
    // Write your code here.
    vectord;
    for(int i=0;i

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

    bro i was watching your previous videos and trying to solving those
    bro im didnt getting space and time complexity between two solution
    Combination |||
    solution -1
    -------------------------------------------
    static void Solve(int ind,int k,int sum,int n,ArrayList ans,ArrayList ds){
    if(ds.size()>=k){
    if(sum==0){
    ans.add(new ArrayList(ds));
    }
    return;
    }
    if(ind==10)return;

    ds.add(ind);
    Solve(ind+1,k,sum-ind,n,ans,ds);
    ds.remove(ds.size()-1);
    Solve(ind+1,k,sum,n,ans,ds);

    }
    ----------------------------------------------------------------------------
    SOlution 2 :
    static void Solve(int ind,int k,int sum,int n,ArrayList ans,ArrayList ds){
    if(ds.size()>=k){
    if(sum==0){
    ans.add(new ArrayList(ds));
    }
    return;
    }

    for(int i=ind;i

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

      Bro please help me

  • @Lucifer-xt7un
    @Lucifer-xt7un 2 ปีที่แล้ว +1

    Consistency ++