Загрузка страницы

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

Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Subscribe To Live Tech Offers: https://offerfeed.io
Join Our Coaching Service: https://backtobackswe.com/coaching

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: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww

Tuschar Roy: https://www.youtube.com/user/tusharroy2525

GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ

Jarvis Johnson: https://www.youtube.com/user/VSympathyV

Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 19.1 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.

Видео Search A Maze For Any Path - Depth First Search Fundamentals (Similar To "The Maze" on Leetcode) канала Back To Back SWE
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
20 января 2019 г. 9:00:55
00:17:49
Яндекс.Метрика