Resolving dependencies in a directed acyclic graph with a topological sort
In this recipe, we will show an application of a well-known graph algorithm: topological sorting. Let's consider a directed graph describing dependencies between items. For example, in a package manager, before we can install a given package P, we may need to install dependent packages.
The set of dependencies forms a directed graph. With topological sorting, the package manager can resolve the dependencies and find the right installation order of the packages.
Topological sorting has many other applications. Here, we will illustrate this notion on real data from the JavaScript package manager npm
. We will find the installation order of the required packages for the react
JavaScript package.
How to do it...
- We import a few packages:
>>> import io import json import requests import numpy as np import networkx as nx import matplotlib.pyplot as plt %matplotlib inline
- We download the dataset...