Skip to content

Commit 0660da9

Browse files
committed
change list to set in depth_first_search
1 parent 28a6ce5 commit 0660da9

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

graphs/graph_algorithms.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ def undirected_connected_components(gr):
5757

5858
def DFS(gr, s):
5959
""" Depth first search wrapper """
60-
path = []
60+
path = set([])
6161
depth_first_search(gr, s, path)
6262
return path
6363

6464
def depth_first_search(gr, s, path):
6565
""" Depth first search
6666
Returns a list of nodes "findable" from s """
6767
if s in path: return False
68-
path.append(s)
68+
path.add(s)
6969
for each in gr.neighbors(s):
7070
if each not in path:
7171
depth_first_search(gr, each, path)
@@ -144,7 +144,8 @@ def shortest_path(digr, s):
144144
from s using Dijkstra's algorithm in O(mlogn) time. Uses heaps
145145
for super fast implementation """
146146
nodes_explored = set([s])
147-
nodes_unexplored = DFS(digr, s)[1:] # all accessible nodes from s
147+
nodes_unexplored = DFS(digr, s) # all accessible nodes from s
148+
nodes_unexplored.remove(s)
148149
dist = {s:0}
149150
node_heap = []
150151

0 commit comments

Comments
 (0)