def sort_nodes(self):
        node_map = {id(node): i for i, node in enumerate(self.nodes)}
        node_deps = {}
        for node, inputs, _outputs in self.iterate_bfs():
            node_index = node_map[id(node)]
            deps = {node_map[id(x)] for x in inputs}
            node_deps[node_index] = deps
        node_order = toposort_flatten(node_deps)
        self.nodes = [self.nodes[x] for x in node_order]
        for root in self.roots:
            if root not in self.nodes[0:len(self.roots)]:
                raise NodeConnectionError("Inputs not sorted in the beginning", node=str(root), node_position=self.nodes.index(root))