Skip to content

Commit c01447f

Browse files
author
cheung
committed
Implement Depth-First-Search algorithm
Recursive implementation of DFS The expected input: -graph represented as a adjacency list -starting node (vertex) Ouput: -path to all other nodes
1 parent a518d20 commit c01447f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
depth_first_search.py
3+
4+
Recursive implementation of DFS algorithm on a graph.
5+
6+
Depth First Search Overview:
7+
------------------------
8+
Used to traverse trees, tree structures or graphs.
9+
Starts at a selected node (root) and explores the branch
10+
as far as possible before backtracking.
11+
12+
Time Complexity: O(E + V)
13+
E = Number of edges
14+
V = Number of vertices (nodes)
15+
16+
Pseudocode: https://en.wikipedia.org/wiki/Depth-first_search
17+
"""
18+
def dfs(graph,start,path = []):
19+
path = path + [start]
20+
for edge in graph[start]:
21+
if edge not in path:
22+
path = dfs(graph, edge,path)
23+
return path

algorithms/tests/test_searching.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" Unit Tests for searching """
22
import unittest
3-
from ..searching import binary_search, kmp_search, rabinkarp_search, bmh_search
3+
from ..searching import binary_search, kmp_search, rabinkarp_search, bmh_search, depth_first_search
44

55

66
class TestBinarySearch(unittest.TestCase):
@@ -69,3 +69,60 @@ def test_bmhsearch(self):
6969
rv2 = bmh_search.search(self.string, "ABCDER")
7070
self.assertIs(rv1[0], 9)
7171
self.assertFalse(rv2)
72+
73+
class TestDepthFirstSearch(unittest.TestCase):
74+
"""
75+
Tests DFS on a graph represented by a adjacency list
76+
"""
77+
78+
def test_dfs(self):
79+
self.graph = {'A': ['B','C','E'],
80+
'B': ['A','D','F'],
81+
'C': ['A','G'],
82+
'D': ['B'],
83+
'F': ['B'],
84+
'E': ['A'],
85+
'G': ['C']}
86+
rv1 = depth_first_search.dfs(self.graph, "A")
87+
rv2 = depth_first_search.dfs(self.graph, "G")
88+
self.assertEqual(rv1, ['A', 'B', 'D', 'F', 'C', 'G', 'E'])
89+
self.assertEqual(rv2, ['G', 'C', 'A', 'B', 'D', 'F', 'E'])
90+
self.graph = {1:[2,3,4],
91+
2:[1,6,10],
92+
3:[1,5,10],
93+
4:[1,10,11],
94+
5:[3,10],
95+
6:[2,7,8,9],
96+
7:[6,8],
97+
8:[6,7],
98+
9:[6,10],
99+
10:[3,5,9,12],
100+
11:[4],
101+
12:[10]}
102+
rv3 = depth_first_search.dfs(self.graph,1)
103+
rv4 = depth_first_search.dfs(self.graph,5)
104+
rv5 = depth_first_search.dfs(self.graph,6)
105+
self.assertEqual(rv3, [1, 2, 6, 7, 8, 9, 10, 3, 5, 12, 4, 11])
106+
self.assertEqual(rv4, [5, 3, 1, 2, 6, 7, 8, 9, 10, 12, 4, 11])
107+
self.assertEqual(rv5, [6, 2, 1, 3, 5, 10, 9, 12, 4, 11, 7, 8])
108+
self.graph = {1:[2,3,4,5,6],
109+
2:[1,4,7,8,9],
110+
3:[1,10],
111+
4:[1,2,11,12],
112+
5:[1,13,14,15],
113+
6:[1,15],
114+
7:[2],
115+
8:[2],
116+
9:[2,10],
117+
10:[3,9],
118+
11:[4],
119+
12:[4],
120+
13:[5],
121+
14:[5],
122+
15:[5,6]}
123+
rv6 = depth_first_search.dfs(self.graph,1)
124+
rv7 = depth_first_search.dfs(self.graph,10)
125+
rv8 = depth_first_search.dfs(self.graph,5)
126+
self.assertEqual(rv6, [1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 5, 13, 14, 15, 6])
127+
self.assertEqual(rv7, [10, 3, 1, 2, 4, 11, 12, 7, 8, 9, 5, 13, 14, 15, 6])
128+
self.assertEqual(rv8, [5, 1, 2, 4, 11, 12, 7, 8, 9, 10, 3, 6, 15, 13, 14])

0 commit comments

Comments
 (0)