Skip to content

Commit 5c9383c

Browse files
committed
[Feature] Added depth first search for 2D graph.
1 parent ebc1e07 commit 5c9383c

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ $ coverage report -m
3535
* [Breadth First Search](searches/breadth_first_search.py)
3636
* [2D Grid BFS](searches/breadth_first_search.py)
3737
* [Graph BFS](searches/breadth_first_search.py)
38+
* [Depth First Search](searches/depth_first_search.py)
39+
* [2D Grid DFS](searches/depth_first_search.py)
3840
* [Finding Duplicates](searches/find_duplicates.py)
3941
* [Dijkstra's Pathfinding](searches/dijkstras.py)
4042

searches/depth_first_search.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def depth_first_search(grid, start, target):
2+
"""
3+
Search a 2d grid for a given target starting at start.
4+
5+
Args:
6+
grid: the input grid as a List[List]
7+
start: the start grid in format (x,y) zero index
8+
target: the target value to find in the grid
9+
Returns:
10+
Coordinate of the target. Or None if cannot be found.
11+
"""
12+
height = len(grid)
13+
if not height:
14+
return None
15+
width = len(grid[0])
16+
17+
x_start = start[0]
18+
y_start = start[1]
19+
20+
# short circuit the start lookup
21+
if grid[y_start][x_start] == target:
22+
return (x_start, y_start)
23+
24+
visited = set()
25+
26+
stack = [(x_start, y_start)]
27+
visited.add((x_start, y_start))
28+
29+
while stack:
30+
current = stack.pop()
31+
32+
for coor in [(current[0], current[1]-1),(current[0]-1, current[1]),(current[0]+1, current[1]),(current[0], current[1]+1)]:
33+
if coor[0] < 0 or coor[0] > width-1 or coor[1] < 0 or coor[1] > height-1:
34+
continue
35+
if grid[coor[1]][coor[0]] == target:
36+
return coor
37+
else:
38+
if coor not in visited:
39+
stack.append(coor)
40+
visited.add(current)
41+
return None

tests/test_2d_dfs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
from searches.depth_first_search import depth_first_search
3+
4+
class TestBFS(unittest.TestCase):
5+
6+
def make_simple_grid(self):
7+
return [
8+
[1,0,3,4],
9+
[5,6,2,6],
10+
[8,8,1,3],
11+
]
12+
13+
def make_complex_grid(self):
14+
return [
15+
[1,0,3,4,7,12,4,111],
16+
[5,6,2,6,0,1,54,3],
17+
[8,8,1,3,2,6,8,22],
18+
[8,8,1,3,4,0,77,1],
19+
[8,8,1,3,-1,4,2,9],
20+
]
21+
22+
def test_dfs_basic(self):
23+
self.assertEqual((1,2), depth_first_search(self.make_simple_grid(), (3, 0), 8))
24+
25+
def test_find_farther_first(self):
26+
self.assertEqual((3,2), depth_first_search(self.make_simple_grid(), (0, 2), 3))
27+
28+
def test_dfs_complex(self):
29+
self.assertEqual((7,0), depth_first_search(self.make_complex_grid(), (7, 4), 111))
30+
31+
self.assertEqual((0,4), depth_first_search(self.make_complex_grid(), (0, 4), 8))
32+
33+
def test_dfs_empty(self):
34+
self.assertEqual(None, depth_first_search([],(0,1),0))
35+
36+
def test_dfs_not_found(self):
37+
self.assertEqual(None, depth_first_search(self.make_simple_grid(), (0,0), -1))

0 commit comments

Comments
 (0)