From 9c9442315ea02342fdbfb990d45511b6c25c8264 Mon Sep 17 00:00:00 2001
From: Vivek <103368320+Vivek09Chahal@users.noreply.github.com>
Date: Thu, 31 Oct 2024 09:10:16 +0530
Subject: [PATCH 1/2] Fix topological sort order

Fixes #12192
---
 graphs/basic_graphs.py | 45 ++++++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py
index 25c8045b3d2b..22b30ad8ff38 100644
--- a/graphs/basic_graphs.py
+++ b/graphs/basic_graphs.py
@@ -155,10 +155,30 @@ def dijk(g, s):
 
 
 def topo(g, ind=None, q=None):
+    """
+    Perform topological sorting on a directed acyclic graph (DAG).
+
+    Args:
+        g (dict): Dictionary of edges representing the graph.
+        ind (list, optional): List of in-degrees of nodes. Defaults to None.
+        q (deque, optional): Queue for processing nodes. Defaults to None.
+
+    Returns:
+        list: List of nodes in topologically sorted order.
+
+    Note:
+        The function returns the list in the correct order from top to bottom.
+        The order is reversed before returning to ensure the correct topological sort.
+
+    Example:
+    >>> graph = {1: [2, 3], 2: [4], 3: [4], 4: []}
+    >>> topo(graph)
+    [1, 2, 3, 4]
+    """
     if q is None:
         q = [1]
     if ind is None:
-        ind = [0] * (len(g) + 1)  # SInce oth Index is ignored
+        ind = [0] * (len(g) + 1)  # Since 0th Index is ignored
         for u in g:
             for v in g[u]:
                 ind[v] += 1
@@ -166,15 +186,15 @@ def topo(g, ind=None, q=None):
         for i in g:
             if ind[i] == 0:
                 q.append(i)
-    if len(q) == 0:
-        return
-    v = q.popleft()
-    print(v)
-    for w in g[v]:
-        ind[w] -= 1
-        if ind[w] == 0:
-            q.append(w)
-    topo(g, ind, q)
+    result = []
+    while q:
+        v = q.popleft()
+        result.append(v)
+        for w in g[v]:
+            ind[w] -= 1
+            if ind[w] == 0:
+                q.append(w)
+    return result[::-1]  # Reverse the result list before returning
 
 
 """
@@ -375,3 +395,8 @@ def find_isolated_nodes(graph):
         if not graph[node]:
             isolated.append(node)
     return isolated
+
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()

From 8151db7cc9e28e5a0f4ba70f4fb11149a6af3176 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
 <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Thu, 31 Oct 2024 03:40:38 +0000
Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
---
 graphs/basic_graphs.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py
index 22b30ad8ff38..132f1c55f88a 100644
--- a/graphs/basic_graphs.py
+++ b/graphs/basic_graphs.py
@@ -399,4 +399,5 @@ def find_isolated_nodes(graph):
 
 if __name__ == "__main__":
     import doctest
+
     doctest.testmod()