Search A Maze For Any Path - Depth First Search Fundamentals (Similar To "The Maze" on Leetcode)

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given a 2D array of black and white entries representing a maze with designated entrance and exit points, find a path from the entrance to the exit, if one exists.
Graph search methodologies apply well to problems that have an aspect of a spatial relationship.
Approach 1 (Brute Force)
We could try to enumerate all possible paths in the maze from the start to the finish and then check all paths to see if any of them are valid (have all white squares, aka do not run over a wall).
This is both naive and extremely costly in terms of time.
Approach 2 (Graph BFS or DFS)
We will imagine each cell as a vertex and each adjacent relationship as the edges connecting nodes.
Do we use DFS or BFS?
If we use BFS we know that the path that we find will be the shortest path because of how it searches (wide, going out layer by layer).
If we use DFS we can have the call stack remember the path making things easier to implement.
If we hit the end cell, then we will know that every call below in the call stack has a node apart of the answer path.
Since the problem just wants any path then we will use DFS since it is more straight-forward.
Complexities
Time: O( | V | + | E | )
The standard time complexity for DFS
Space: O( | V | )
We will at maximum the length of the path on the call stack through our recursion
Note: The problem on Leetcode requires BFS to pass because DFS will not always find the shortest path, but I did DFS in this video just for teaching purposes.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 19.1 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.

Пікірлер: 329

  • @BackToBackSWE
    @BackToBackSWE5 жыл бұрын

    Table of Contents:

  • @ceek79
    @ceek795 жыл бұрын

    Honestly, you sitting in front of a computer would be a waste of talent! Take advantage of your skill to explain complex stuff the easy way and make a business out of it.

  • @FrancoMunizAR
    @FrancoMunizAR3 жыл бұрын

    You and Abdul Bari are the kings of explaining algorithms and problem solving. Thanks a LOT for everything you do!

  • @TechAngelx
    @TechAngelx

    Great video and great explanation, but could have been a lot shorter and concise if you didn't explain/repeat what we already understand for every single box. - Can't go right/up/ etc, have to backtrack. We get it already. No need to stop and go verbose on every single white square in the maze. Had to watch it on 2x speed. But still, thanks for the video.

  • @qiuyangshen3569
    @qiuyangshen35693 жыл бұрын

    The call stack is a state! So brilliant words!

  • @leelavathisettu2762
    @leelavathisettu27623 жыл бұрын

    Where is the code for the DFS approach

  • @gijs6560
    @gijs65603 жыл бұрын

    Where is the code?

  • @kylenakamura4353
    @kylenakamura43532 жыл бұрын

    Don't mind me... just a Masters CS student trying to remember how the heck DFS works.

  • @DK-fn6xr
    @DK-fn6xr Жыл бұрын

    He got locked in the library by 2 and a half minutes.

  • @lmnefg121
    @lmnefg1214 жыл бұрын

    I really like that you explain things in such a simple way and teaches the most fundamental method. As a matter of fact, it is always most difficult to learn the most fundamental stuff.

  • @TJ-rf1xl
    @TJ-rf1xl4 жыл бұрын

    Best explanation I've seen of DFS so far. I wish my professors explained things this way. I likely would have dropped out of my MSc if it wasn't for your channel!

  • @frogspawn8
    @frogspawn84 жыл бұрын

    Bruh, your lectures are WAY clearer and more concise than some of the “best” professors I’ve encountered.

  • @betatester03
    @betatester034 жыл бұрын

    Man, you are an excellent teacher. I stumbled onto one of your videos earlier and immediately started up another, right after. I'm about five videos in, now.

  • @GraniLP
    @GraniLP3 жыл бұрын

    Big thanks to you! Currently I am a bit angry that my university is not capable of explaining such stuff in a nice way, but you did it in less time and in a much clearer way!

  • @darkhorse621
    @darkhorse6214 жыл бұрын

    Best Explanation for DFS I've come across so far. Thanks a lot !!!

  • @sindisiwemncube4665
    @sindisiwemncube4665 Жыл бұрын

    This dude is a brilliant tutor. Never have i ever understood an algorithm this clearly, Juss did.

  • @abyssking4062
    @abyssking40622 жыл бұрын

    Till now, your video is still saving lives. I finally completed my damn maze assignment after your explanation. Wherever you are bro, THANK YOU!!!

  • @sairamankilambi5007
    @sairamankilambi50073 жыл бұрын

    I saw the BFS & DFS (15 min) video followed by the maze problem. Wow! It clears out when and where to chose BFS over DFS. Thanks a LOT!

  • @Dunig2
    @Dunig24 жыл бұрын

    This is so clear to me, I’m amazed at how easy you make this to understand

  • @stephentanksleymusic7240
    @stephentanksleymusic72403 жыл бұрын

    This was fantastic. Thank you so much for such a clear, effective explanation of recursive DFS algorithms for maze traversal!