# Topological Sort

A topological sort, a.k.a Topo-sort, is performed on DAG ([Directed Acyclic Graph](https://cs-notes.gitbook.io/algorithm-notes/outline/overview-3/broken-reference)) to compute a **linear ordering** of all the vertices in graph. It satisfies the following condition: if a graph *G* contains an edge (*u*, *v*), then the vertex *u* must appear before the vertex *v* in the sorted ordering.

Unlike the general discussions in [sorting](https://cs-notes.gitbook.io/algorithm-notes/outline/overview-3/broken-reference) section, Topo-sort tries to layout a graph structure into a flatten distribution of vertices. This method is widely adopted in the industry, e.g. a manufacturing process of specific products might have numerous small procedures combined together and to compute a topological ordering of such procedures are critical for pipelining the manufacturing process as to reduce time & money cost.

There are many algorithms that can achieve the goal, here a version of Kahn's algorithm is discussed.

## Kahn's Algorithm

The idea of this algorithm takes advantage of [BFS](https://cs-notes.gitbook.io/algorithm-notes/outline/overview-5/graph-search) and using in-degree of nodes and it is the easiest method to understand.

1. Compute the in-degree for each of the nodes within the graph, initialize a count for *visited nodes* to 0.
2. Enqueue all the nodes with in-degree of 0 to *queue*.
3. Dequeue a node from *queue*, increase the count for *visited nodes* by 1, decrease the in-degree by 1 for all its neighboring nodes; If any of these nodes happen to have in-degree of 0, enqueue them to *queue*.
4. Repeat step 3 until *queue* is empty.
5. If the count of *visited nodes* is not equal to the number of nodes in the graph, then it is not [DAG](https://cs-notes.gitbook.io/algorithm-notes/outline/overview-3/broken-reference) and topo-sort is impossible to complete.

Unlike traversing the solution using [DFS](https://cs-notes.gitbook.io/algorithm-notes/outline/overview-5/graph-search) that revisit some of the nodes that share the same routes from the source to the destination, this algorithm takes Ο(n) time to visit all nodes, which is more favorable and easier to design.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cs-notes.gitbook.io/algorithm-notes/outline/overview-3/topological-sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
