Github user hakanmemisoglu commented on a diff in the pull request: https://github.com/apache/incubator-quickstep/pull/233#discussion_r112067899 --- Diff: utility/DAG.hpp --- @@ -489,6 +495,40 @@ bool DAG<T, LinkMetadataT>::hasCycleHelper(const typename DAG<T, LinkMetadataT>: return false; } +template <class T, class LinkMetadataT> +std::vector<typename DAG<T, LinkMetadataT>::size_type_nodes> DAG<T, LinkMetadataT>::getTopologicalSorting() const { + // We implement "Kahn's algorithm" for the sorting. + DCHECK(!hasCycle()); + std::unique_ptr<std::vector<typename DAG<T, LinkMetadataT>::size_type_nodes>> + sorted_list(new std::vector<size_type_nodes>()); + sorted_list->reserve(this->size()); + // Key = node ID, value = # incoming edges for this node. + // NOTE(harshad) - We modify the "values" in this map as we go along. + std::unordered_map<typename DAG<T, LinkMetadataT>::size_type_nodes, std::size_t> num_incoming_edges; + std::queue<typename DAG<T, LinkMetadataT>::size_type_nodes> nodes_with_no_children; + for (auto node_id = 0u; node_id < this->size(); ++node_id) { + if (nodes_[node_id].getDependencies().empty()) { + nodes_with_no_children.emplace(node_id); + } + num_incoming_edges[node_id] = nodes_[node_id].getDependencies().size(); + } + while (!nodes_with_no_children.empty()) { --- End diff -- I think this is the place where Kahn's algorithm starts to work. Can you separate methods into logical pieces with comments or by refactoring?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---