टोपोलॉजिकल सॉर्ट कैलकुलेटर
DAG · काह्न एल्गोरिथम · DFS · चक्र का पता लगाना · सभी व्यवस्थाएँ
एक निर्देशित अचक्रीय ग्राफ (DAG) की एक वैध रैखिक व्यवस्था खोजें। काह्न के BFS एल्गोरिथम और DFS दृष्टिकोण का समर्थन करता है। चक्रों का पता लगाता है और छोटे ग्राफ के लिए सभी वैध टोपोलॉजिकल क्रमों को सूचीबद्ध करता है।
त्वरित उदाहरण
स्वीकृत प्रारूप: अल्पविराम या नई पंक्ति द्वारा अलग किए गए A→B A->B A-B A,B।
रो नोड से कॉलम नोड तक एज जोड़ने/हटाने के लिए सेल पर क्लिक करें।
चक्र का पता चला — टोपोलॉजिकल सॉर्ट असंभव
एक टोपोलॉजिकल व्यवस्था केवल निर्देशित अचक्रीय ग्राफ (DAG) के लिए मौजूद होती है। इस ग्राफ में चक्र है, इसलिए कोई भी वैध क्रम मौजूद नहीं है।
टोपोलॉजिकल व्यवस्था (काह्न एल्गोरिथम)
DAG विज़ुअलाइज़ेशन
नोड्स टोपोलॉजिकल स्थिति (हल्के से गहरे हरे) के अनुसार रंगे गए हैं। संख्याएँ क्रम की स्थिति दिखाती हैं।
चरण-दर-चरण ट्रेस — काह्न एल्गोरिथम
प्रत्येक चरण: इन-डिग्री 0 वाले नोड को कतार से हटाएं, परिणाम में जोड़ें, पड़ोसियों की इन-डिग्री घटाएं।
| चरण | कतार से निकाला गया (Dequeued) | चक्र के बाद कतार | इन-डिग्री (बदली हुई) | अब तक का परिणाम |
|---|
DFS फिनिश समय (DFS Finish Times)
नोड्स को घटते हुए DFS फ़िनिश समय के अनुसार व्यवस्थित करने पर टोपोलॉजिकल क्रम प्राप्त होता है।
| नोड (Node) | खोज समय (Discover Time) | फिनिश समय (Finish Time) | टोपोलॉजिकल स्थिति |
|---|
सभी वैध टोपोलॉजिकल व्यवस्थाएं (All Valid Orderings)
सभी वैध टोपोलॉजिकल क्रम नीचे सूचीबद्ध हैं। इनमें से कोई भी एक सही उत्तर है।
टोपोलॉजिकल सॉर्ट (Topological Sort) क्या है?
Topological sort (also called topological ordering) is a linear ordering of the vertices of a directed acyclic graph (DAG) such that for every directed edge u → v, vertex u appears before vertex v in the ordering. Unlike standard sorting, topological sort is about dependency order, not numerical or alphabetical comparison.
Topological sort only exists when the graph has no directed cycles. If a cycle exists — say A → B → C → A — there is no consistent ordering because A must precede B, B must precede C, and C must precede A, which is impossible simultaneously.
निर्देशित अचक्रीय ग्राफ (DAG)
A directed acyclic graph (DAG) is a graph in which every edge has a direction and there are no directed cycles. DAGs naturally arise whenever you model dependencies: task A must complete before task B can start. The edges represent "must come before" relationships.
Common examples include: course prerequisites (Calculus before Differential Equations), software build dependencies (compile library before linking executable), recipe steps (chop ingredients before cooking), and spreadsheet cell formulas (compute D1 = A1 + B1 before E1 = D1 * 2).
काह्न एल्गोरिथम (Kahn's Algorithm)
Kahn's algorithm, published by Arthur B. Kahn in 1962, is the most intuitive approach. It works by repeatedly removing nodes that have no dependencies:
- Compute in-degree for each node (number of incoming edges).
- Initialize queue with all nodes having in-degree 0 (no dependencies).
- While queue is not empty: dequeue node u, append to result, and for each neighbor v of u, decrement in-degree[v]. If in-degree[v] becomes 0, enqueue v.
- Cycle check: if result contains all nodes, the graph is a DAG. Otherwise, remaining nodes form a cycle.
Time complexity: O(V + E) where V is the number of vertices and E is the number of edges. This is optimal since every vertex and edge must be examined at least once.
DFS-आधारित टोपोलॉजिकल सॉर्ट
The DFS-based approach uses depth-first search and records each node's finish time — the moment DFS fully explores all descendants. Nodes with higher finish times come earlier in the topological order. A cycle is detected when DFS encounters a back edge — an edge pointing to an ancestor in the current DFS path (a "gray" node in the three-color marking scheme).
Both Kahn's and DFS-based approaches have the same O(V + E) complexity, but they produce different valid orderings for the same graph (when multiple orderings exist).
टोपोलॉजिकल सॉर्ट के अनुप्रयोग
| Domain | Application | DAG Meaning |
|---|---|---|
| Build Systems | GNU Make, Bazel, npm | Source file A must compile before linking B |
| Education | Course scheduling | Prerequisite course must be taken first |
| Spreadsheets | Formula recalculation | Cell must be computed before dependent cells |
| Compilers | Instruction scheduling | Instruction result needed by later instruction |
| Package Managers | Apt, pip, npm install | Dependency must install before dependents |
| Project Planning | Critical path method | Task must complete before successor can start |
कितने वैध क्रम मौजूद हैं?
A graph can have multiple valid topological orderings. For a chain A → B → C → D, there is exactly one valid ordering. But for a "diamond" A → B → D, A → C → D, both [A, B, C, D] and [A, C, B, D] are valid. The count of valid orderings depends on the graph structure and grows quickly — counting all orderings is a #P-complete problem. This calculator enumerates all orderings for small graphs (up to 7 nodes).
चक्र का पता लगाने का विवरण (Cycle Detection)
In Kahn's algorithm, a cycle is detected when the result contains fewer than V nodes after the queue empties. The remaining nodes are involved in cycles — each has at least one incoming edge from another cycled node, so no node in the cycle ever reaches in-degree 0. In DFS-based detection, encountering a "gray" node (currently being processed) during traversal signals a back edge and thus a cycle.