Application 1: Topological Sort
Explore how to use depth-first search to compute a topological sort, enabling you to order tasks based on dependencies in acyclic graphs. Understand the algorithm, implementation details, and runtime complexity to solve task scheduling problems effectively.
In this lesson, we’ll apply the DFS algorithm to solve a task ordering problem.
The topological sorting problem
Our goal is to order a set of tasks in a way that respects the dependencies between them. For example, let’s assume that you’re planning your activities for the day. Unfortunately, there is a collection of household chores that you’ll need to complete first, such as washing the dishes, withdrawing cash, grocery shopping, and cooking. Some of these activities may depend on each other. For instance, you need to shop for groceries before you can cook dinner. And you need to withdraw cash before you can shop for groceries.
These tasks and dependencies can be modeled in a task graph. Each task is a node of the graph, and there is an edge whenever task directly depends on task . The following figure shows an example task graph.
Whenever there is a path in the task graph, the task (directly or indirectly) depends on the task . This means that we have to perform task before we can perform task ...