Skip to content

Commit ccfcf84

Browse files
committed
Fix failing data structure tests
Fix the failing add_edge test for the graph by checking if the graph contains the to_vertex and the from_vertex independently.
1 parent 47d3187 commit ccfcf84

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pygorithm/data_structures/graph.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,15 @@ def add_edge(self, from_vertex, to_vertex):
254254
for adding the edge between two vertices
255255
"""
256256
# check if vertex is already present,
257-
if from_vertex in self.graph.keys() and to_vertex in self.graph.keys():
257+
if from_vertex in self.graph.keys():
258258
self.graph[from_vertex].append(to_vertex)
259-
self.graph[to_vertex].append(from_vertex)
260259
else:
261-
# else make a new vertex
260+
# otherwise add it to the graph
262261
self.graph[from_vertex] = [to_vertex]
262+
263+
if to_vertex in self.graph.keys():
264+
self.graph[to_vertex].append(from_vertex)
265+
else:
263266
self.graph[to_vertex] = [from_vertex]
264267

265268
def check_cycle(self):

0 commit comments

Comments
 (0)