|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Topological Sort: Ordering tasks with dependencies. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <unordered_map>#include <unordered_set>#include <tpl_graph.H>#include <topological_sort.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Typedefs | |
| using | TaskNode = Graph_Node< string > |
| using | DependencyArc = Graph_Arc< int > |
| using | TaskGraph = List_Digraph< TaskNode, DependencyArc > |
Functions | |
| TaskGraph | build_project_graph () |
| Build a sample build system dependency graph. | |
| TaskGraph | build_courses_graph () |
| Build a course prerequisites graph. | |
| TaskGraph::Node * | find_task (TaskGraph &g, const string &name) |
| Find a node by name. | |
| void | print_graph (TaskGraph &g, const string &title) |
| Print the graph structure. | |
| void | demo_dfs_topological_sort (TaskGraph &g, bool verbose) |
| Demonstrate DFS-based topological sort. | |
| void | demo_bfs_topological_sort (TaskGraph &g, bool verbose) |
| Demonstrate BFS-based topological sort (Kahn's algorithm) | |
| void | demo_build_order () |
| Demonstrate practical application: build order. | |
| void | demo_course_scheduling () |
| Demonstrate practical application: course scheduling. | |
| int | main (int argc, char *argv[]) |
Topological Sort: Ordering tasks with dependencies.
This example demonstrates topological sorting of directed acyclic graphs (DAGs), a fundamental algorithm for ordering tasks with dependencies. Topological sort is essential whenever you need to process items in a valid dependency order.
A topological ordering of a DAG is a linear ordering of vertices such that for every directed edge (u → v), vertex u comes before v in the ordering.
Key properties:
Graph: A → B → D, A → C → D
Valid orderings:
Invalid: D, A, B, C (violates A → D dependency)
Strategy: Post-order DFS traversal
Algorithm:
Key insight: Add vertex to result AFTER processing all descendants
Time complexity: O(V + E) Space complexity: O(V) for recursion stack
Strategy: Iteratively remove sources (vertices with no incoming edges)
Algorithm:
Key insight: Process vertices with no dependencies first
Time complexity: O(V + E) Space complexity: O(V) for queue
| Aspect | DFS-based | Kahn's (BFS) |
|---|---|---|
| Approach | Post-order DFS | Remove sources |
| Cycles | Does not detect cycles; output may violate constraints | Can indicate cycles: if output size < V, graph is not a DAG |
| Order | Depth-first | Breadth-first |
| Implementation | Recursive | Iterative |
| Best for | General use | When cycle indication needed |
Important: Topological sort only works on DAGs!
If graph has cycles:
To detect cycles:
| Operation | Time | Space |
|---|---|---|
| Topological sort | O(V + E) | O(V) |
| Cycle/SCC detection (separate) | O(V + E) | O(V) |
Definition in file topological_sort_example.C.
| using DependencyArc = Graph_Arc<int> |
Definition at line 211 of file topological_sort_example.C.
| using TaskGraph = List_Digraph<TaskNode, DependencyArc> |
Definition at line 212 of file topological_sort_example.C.
| using TaskNode = Graph_Node<string> |
Definition at line 210 of file topological_sort_example.C.
| TaskGraph build_courses_graph | ( | ) |
Build a course prerequisites graph.
University course dependencies:
Math101 --> Math201 --> Math301 | | v v CS101 -—> CS201 -—> CS301 | | v v CS102 -------------—> CS302
Definition at line 340 of file topological_sort_example.C.
References Aleph::maps().
Referenced by demo_course_scheduling().
| TaskGraph build_project_graph | ( | ) |
Build a sample build system dependency graph.
Represents a typical C++ project build:
utils.h <– config.h | | v v utils.o parser.o <– lexer.o | | | +-—+--—+--—+--—+ | | v v main.o test.o | | v v program test_suite
Definition at line 292 of file topological_sort_example.C.
References Aleph::maps().
Referenced by demo_build_order(), and main().
| void demo_bfs_topological_sort | ( | TaskGraph & | g, |
| bool | verbose | ||
| ) |
Demonstrate BFS-based topological sort (Kahn's algorithm)
Definition at line 453 of file topological_sort_example.C.
References Aleph::DynList< T >::append(), LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), Aleph::maps(), Aleph::ranks(), and Aleph::verbose.
Referenced by main().
| void demo_build_order | ( | ) |
Demonstrate practical application: build order.
Definition at line 526 of file topological_sort_example.C.
References build_project_graph(), LocateFunctions< Container, Type >::get_it(), Aleph::maps(), and print_graph().
Referenced by main().
| void demo_course_scheduling | ( | ) |
Demonstrate practical application: course scheduling.
Definition at line 557 of file topological_sort_example.C.
References build_courses_graph(), LocateFunctions< Container, Type >::get_it(), Aleph::maps(), and print_graph().
Referenced by main().
| void demo_dfs_topological_sort | ( | TaskGraph & | g, |
| bool | verbose | ||
| ) |
Demonstrate DFS-based topological sort.
Definition at line 416 of file topological_sort_example.C.
References LocateFunctions< Container, Type >::get_it(), Aleph::maps(), and Aleph::verbose.
Referenced by main().
| TaskGraph::Node * find_task | ( | TaskGraph & | g, |
| const string & | name | ||
| ) |
Find a node by name.
Definition at line 373 of file topological_sort_example.C.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 590 of file topological_sort_example.C.
References build_project_graph(), demo_bfs_topological_sort(), demo_build_order(), demo_course_scheduling(), demo_dfs_topological_sort(), Aleph::maps(), and Aleph::verbose.
| void print_graph | ( | TaskGraph & | g, |
| const string & | title | ||
| ) |
Print the graph structure.
Definition at line 384 of file topological_sort_example.C.
References Aleph::maps().
Referenced by demo_build_order(), and demo_course_scheduling().