Kahn's Algorithm
Kahn's Algorithm is a method used to perform a topological sort on a directed acyclic graph (DAG). It works by identifying nodes with no incoming edges, also known as "sources," and removing them from the graph. This process continues iteratively, allowing the algorithm to build a sorted list of nodes.
The algorithm maintains a count of incoming edges for each node. When a source is removed, the incoming edge count for its neighboring nodes is updated. If any of these neighbors become sources, they are added to the list for processing. This continues until all nodes are sorted or no sources remain.