Skip to content

Commit 27e9de2

Browse files
Santosh BandichodeSantosh Bandichode
authored andcommitted
Added base case and return the path as soon as end node is visited
1 parent 65651fd commit 27e9de2

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

patterns/other/graph_search.py

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,41 +53,81 @@ def find_shortest_path_bfs(self, start, end):
5353
dist_to = {start: 0}
5454
edge_to = {}
5555

56+
if start == end:
57+
return queue
58+
5659
while len(queue):
5760
value = queue.pop(0)
5861
for node in self.graph[value]:
5962
if node not in dist_to.keys():
6063
edge_to[node] = value
6164
dist_to[node] = dist_to[value] + 1
6265
queue.append(node)
63-
if end in edge_to.values():
64-
break
65-
66-
path = []
67-
node = end
68-
while dist_to[node] != 0:
69-
path.insert(0, node)
70-
node = edge_to[node]
71-
path.insert(0, start)
72-
return path
66+
if end in edge_to.keys():
67+
path = []
68+
node = end
69+
while dist_to[node] != 0:
70+
path.insert(0, node)
71+
node = edge_to[node]
72+
path.insert(0, start)
73+
return path
7374

7475

7576
def main():
7677
"""
7778
# example of graph usage
78-
>>> graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}
79+
>>> graph = {
80+
... 'A': ['B', 'C'],
81+
... 'B': ['C', 'D'],
82+
... 'C': ['D', 'G'],
83+
... 'D': ['C'],
84+
... 'E': ['F'],
85+
... 'F': ['C'],
86+
... 'G': ['E'],
87+
... 'H': ['C']
88+
... }
7989
8090
# initialization of new graph search object
81-
>>> graph1 = GraphSearch(graph)
91+
>>> graph_search = GraphSearch(graph)
8292
83-
>>> print(graph1.find_path_dfs('A', 'D'))
93+
>>> print(graph_search.find_path_dfs('A', 'D'))
8494
['A', 'B', 'C', 'D']
85-
>>> print(graph1.find_all_paths_dfs('A', 'D'))
95+
96+
# start the search somewhere in the middle
97+
>>> print(graph_search.find_path_dfs('G', 'F'))
98+
['G', 'E', 'F']
99+
100+
# unreachable node
101+
>>> print(graph_search.find_path_dfs('C', 'H'))
102+
None
103+
104+
# non existing node
105+
>>> print(graph_search.find_path_dfs('C', 'X'))
106+
None
107+
108+
>>> print(graph_search.find_all_paths_dfs('A', 'D'))
86109
[['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']]
87-
>>> print(graph1.find_shortest_path_dfs('A', 'D'))
110+
>>> print(graph_search.find_shortest_path_dfs('A', 'D'))
88111
['A', 'B', 'D']
89-
>>> print(graph1.find_shortest_path_bfs('A', 'D'))
112+
>>> print(graph_search.find_shortest_path_dfs('A', 'F'))
113+
['A', 'C', 'G', 'E', 'F']
114+
115+
>>> print(graph_search.find_shortest_path_bfs('A', 'D'))
90116
['A', 'B', 'D']
117+
>>> print(graph_search.find_shortest_path_bfs('A', 'F'))
118+
['A', 'C', 'G', 'E', 'F']
119+
120+
# start the search somewhere in the middle
121+
>>> print(graph_search.find_shortest_path_bfs('G', 'F'))
122+
['G', 'E', 'F']
123+
124+
# unreachable node
125+
>>> print(graph_search.find_shortest_path_bfs('A', 'H'))
126+
None
127+
128+
# non existing node
129+
>>> print(graph_search.find_shortest_path_bfs('A', 'X'))
130+
None
91131
"""
92132

93133

0 commit comments

Comments
 (0)