graph algorithms
stable sort
topological sorting
computational theory
data structures

Stable topological sort

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction to Stable Topological Sort

In the study of computer science, particularly in the field of graph theory, a topological sort of a directed acyclic graph (DAG) is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. However, there might be multiple valid topological sorts for a given graph. A stable topological sort is a refinement of this concept, where tied vertices (vertices that can appear in multiple valid positions) maintain their relative order from the original input list of vertices.

Technical Explanation

Understanding Basic Topological Sort

A topological sort is performed on a directed acyclic graph, ensuring that no vertex is visited before any of its predecessors. The most common methods of achieving a topological sort are:

  1. Kahn's Algorithm: It repeatedly selects vertices with no incoming edges. It maintains a set of "in-degree" counts for each vertex, and iteratively removes vertices with zero in-degree.
  2. Depth-First Search (DFS)-based Approach: As each vertex is finished, it’s added to the front of a list, thereby producing the reverse of a topological ordering.

Stable Topological Sorting

While a standard topological sort guarantees the correct sequence regarding dependencies, it doesn't ensure the stability of order for unrelated vertices — those with no directed edges connecting them. A stable topological sort aims to maintain the pre-existing relative order of these vertices throughout the sorting process.

Algorithmic Implementation

A stable topological sort can be implemented with a slight modification to standard algorithms. Below, we utilize a modified version of Kahn's Algorithm to achieve stability.

Kahn's Algorithm - Stable Variant:

Consider an initial list of vertices:

  • Project Management: Ensuring tasks maintain a sub-order within the same priority.
  • Course Scheduling: Ordering courses while respecting different pathways students can take.
  • Data Processing Pipelines: Maintaining order of operations where multiple configurations might arise.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.