Skip to content

Commit e08aadd

Browse files
Ved Prakash VishwakarmaVed Prakash Vishwakarma
Ved Prakash Vishwakarma
authored and
Ved Prakash Vishwakarma
committed
Issue 12192 fixed, Traversal direction is considered from top to bottom as stated.
1 parent 03a4251 commit e08aadd

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Diff for: sorts/topological_sort.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
2121
# add current to visited
2222
visited.append(current)
2323
neighbors = edges[current]
24+
#as we are traversing in from top to down in tree like graph (direction not given) we consider direction from top to down
25+
#as the current node encounter add it to the topo sort list
26+
sort.append(current)
2427
for neighbor in neighbors:
2528
# if neighbor not in visited, visit
2629
if neighbor not in visited:
2730
sort = topological_sort(neighbor, visited, sort)
28-
# if all neighbors visited add current to sort
29-
sort.append(current)
3031
# if all vertices haven't been visited select a new one to visit
3132
if len(visited) != len(vertices):
3233
for vertice in vertices:

0 commit comments

Comments
 (0)