'Topological sort.'

def Sort(self):

    node_list = []
    S = [node for node in self.layer if (not node.incoming_neighbour)]
    while S:
        n = S.pop()
        node_list.append(n)
        for m in n.outgoing_edge:
            if (m.marker == 0):
                m.marker = 1
                if reduce((lambda a, edge: (a and (edge.marker == 1))), m.node2.incoming_edge, True):
                    S.append(m.node2)
    if reduce((lambda a, edge: (a and (edge.marker == 1))), self.edge, True):
        if self.verbose:
            print 'Fprop    Order:'
            for node in node_list:
                print node.name
    else:
        raise Exception('Invalid    net    for    backprop.    Cycle    exists.')
    return node_list
