From 3ceccfb59cec84f5871ad5efd31a3e43d42867b7 Mon Sep 17 00:00:00 2001
From: VarshiniShreeV <varshinishreevelumani@gmail.com>
Date: Sun, 27 Oct 2024 00:21:12 +0530
Subject: [PATCH 1/6] Fixed

---
 sorts/topological_sort.py | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index efce8165fcac..7613d07b2d6c 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -1,10 +1,3 @@
-"""Topological Sort."""
-
-#     a
-#    / \
-#   b  c
-#  / \
-# d  e
 edges: dict[str, list[str]] = {
     "a": ["c", "b"],
     "b": ["d", "e"],
@@ -14,28 +7,16 @@
 }
 vertices: list[str] = ["a", "b", "c", "d", "e"]
 
-
 def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
-    """Perform topological sort on a directed acyclic graph."""
+    visited.append(start)
     current = start
-    # add current to visited
-    visited.append(current)
-    neighbors = edges[current]
-    for neighbor in neighbors:
-        # if neighbor not in visited, visit
+    for neighbor in edges[start]:
         if neighbor not in visited:
-            sort = topological_sort(neighbor, visited, sort)
-    # if all neighbors visited add current to sort
+            topological_sort(neighbor, visited, sort)
     sort.append(current)
-    # if all vertices haven't been visited select a new one to visit
-    if len(visited) != len(vertices):
-        for vertice in vertices:
-            if vertice not in visited:
-                sort = topological_sort(vertice, visited, sort)
-    # return sort
     return sort
 
-
 if __name__ == "__main__":
     sort = topological_sort("a", [], [])
+    sort.reverse() #Top down approach
     print(sort)

From b5a25700d1176b36db8be0fa6dde3d17528b9eaa Mon Sep 17 00:00:00 2001
From: VarshiniShreeV <varshinishreevelumani@gmail.com>
Date: Sun, 27 Oct 2024 09:23:57 +0530
Subject: [PATCH 2/6] Fixed

---
 sorts/topological_sort.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index 7613d07b2d6c..e5bdc75e9e78 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -19,4 +19,4 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
 if __name__ == "__main__":
     sort = topological_sort("a", [], [])
     sort.reverse() #Top down approach
-    print(sort)
+    print(sort)
\ No newline at end of file

From 6a6876fc1048b315e9f8ae0859d1a931cf390563 Mon Sep 17 00:00:00 2001
From: VarshiniShreeV <varshinishreevelumani@gmail.com>
Date: Sun, 27 Oct 2024 10:06:56 +0530
Subject: [PATCH 3/6] Fixed

---
 sorts/topological_sort.py | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index e5bdc75e9e78..74fdd25ec366 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -1,3 +1,11 @@
+"""Topological Sort on Directed Acyclic Graph(DAG)"""
+
+#     a
+#    / \
+#   b   c
+#  / \
+# d   e
+
 edges: dict[str, list[str]] = {
     "a": ["c", "b"],
     "b": ["d", "e"],
@@ -5,18 +13,40 @@
     "d": [],
     "e": [],
 }
+
 vertices: list[str] = ["a", "b", "c", "d", "e"]
 
+"""Perform topological sort on a directed acyclic graph(DAG) starting from the specified node"""
 def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
-    visited.append(start)
     current = start
-    for neighbor in edges[start]:
+    # Mark the current node as visited
+    visited.append(current)
+    # List of all neighbours of current node
+    neighbors = edges[current]
+
+    # Traverse all neighbours of the current node
+    for neighbor in neighbors:
+        # Recursively visit each unvisited neighbour
         if neighbor not in visited:
-            topological_sort(neighbor, visited, sort)
+            sort = topological_sort(neighbor, visited, sort)
+
+    # After visiting all neigbours, add the current node to the sorted list
     sort.append(current)
+
+    # If there are some nodes that were not visited (disconnected components)
+    if len(visited) != len(vertices):
+        for vertice in vertices:
+            if vertice not in visited:
+                sort = topological_sort(vertice, visited, sort)
+
+    # Return sorted list
     return sort
 
 if __name__ == "__main__":
+    # Topological Sorting from node "a" (Returns the order in bottom up approach)
     sort = topological_sort("a", [], [])
-    sort.reverse() #Top down approach
+
+    # Reversing the list to get the correct topological order (Top down approach)
+    sort.reverse()  
+
     print(sort)
\ No newline at end of file

From d0e0ea1d47bc4f1b2c463ae002256ba32e30d917 Mon Sep 17 00:00:00 2001
From: VarshiniShreeV <varshinishreevelumani@gmail.com>
Date: Sun, 27 Oct 2024 10:14:27 +0530
Subject: [PATCH 4/6] Fixed

---
 sorts/topological_sort.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index 74fdd25ec366..74c3432bcd64 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -16,7 +16,7 @@
 
 vertices: list[str] = ["a", "b", "c", "d", "e"]
 
-"""Perform topological sort on a directed acyclic graph(DAG) starting from the specified node"""
+# Perform topological sort on a DAG starting from the specified node
 def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
     current = start
     # Mark the current node as visited

From 3d3d5aad65e36769009a9be3b4396d30aff5e430 Mon Sep 17 00:00:00 2001
From: VarshiniShreeV <varshinishreevelumani@gmail.com>
Date: Sun, 27 Oct 2024 10:29:55 +0530
Subject: [PATCH 5/6] Fixed

---
 sorts/topological_sort.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index 74c3432bcd64..2cb003d769e2 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -21,16 +21,16 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
     current = start
     # Mark the current node as visited
     visited.append(current)
-    # List of all neighbours of current node
+    # List of all neighbors of current node
     neighbors = edges[current]
 
-    # Traverse all neighbours of the current node
+    # Traverse all neighbors of the current node
     for neighbor in neighbors:
-        # Recursively visit each unvisited neighbour
+        # Recursively visit each unvisited neighbor
         if neighbor not in visited:
             sort = topological_sort(neighbor, visited, sort)
 
-    # After visiting all neigbours, add the current node to the sorted list
+    # After visiting all neigbors, add the current node to the sorted list
     sort.append(current)
 
     # If there are some nodes that were not visited (disconnected components)

From 1186d214a0a543ad36eb6162341b44e79343f397 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Sun, 27 Oct 2024 05:04:40 +0000
Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 sorts/topological_sort.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py
index 2cb003d769e2..c0749e355124 100644
--- a/sorts/topological_sort.py
+++ b/sorts/topological_sort.py
@@ -16,6 +16,7 @@
 
 vertices: list[str] = ["a", "b", "c", "d", "e"]
 
+
 # Perform topological sort on a DAG starting from the specified node
 def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]:
     current = start
@@ -42,11 +43,12 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
     # Return sorted list
     return sort
 
+
 if __name__ == "__main__":
     # Topological Sorting from node "a" (Returns the order in bottom up approach)
     sort = topological_sort("a", [], [])
 
     # Reversing the list to get the correct topological order (Top down approach)
-    sort.reverse()  
+    sort.reverse()
 
-    print(sort)
\ No newline at end of file
+    print(sort)