Implementing a directed graph