public function getSortedNodeList()
    {
        $list = array();

        while ( $leave = array_pop( $this->leaves ) )
        {
            unset( $this->nodes[$leave] );
            $list[] = $leave;

            if ( !isset( $this->edges[$leave] ) )
            {
                continue;
            }

            foreach ( $this->edges[$leave] as $node )
            {
                $count = -1;
                // Count the incoming edges for each node which had an incoming
                // connection from the removed leave.
                foreach ( $this->edges as $src => $dsts )
                {
                    foreach ( $dsts as $nr => $dst )
                    {
                        $count += ( $dst === $node );

                        // Also remove all edges which point to the leave
                        if ( $dst === $leave )
                        {
                            unset( $this->edges[$src][$nr] );
                        }
                    }
                }

                // Add all new leaves to the leave array.
                if ( $count <= 0 )
                {
                    array_push( $this->leaves, $node );
                }
            }

            // Remove all outgoing edges from leave
            unset( $this->edges[$leave] );
        }

        if ( count( $this->edges ) )
        {
            var_dump( $this->edges );
            throw new Exception( 'Cycle found in graph. This should not happen.' );
        }

        return $list;
    }