Perform Brzozowski Algebraic Method

def _brzozowski_algebraic_method_solve(self):
        """Perform Brzozowski Algebraic Method"""
        orderedstates = self._bfs_sort(
            sorted(
                self.mma.states,
                key=attrgetter('initial'),
                reverse=True)[0])
        for n in range(len(orderedstates) - 1, 0, -1):
            # print "n:" + repr(n)
            if self.A[
                    orderedstates[n].stateid,
                    orderedstates[n].stateid] != self.empty:
                # B[n] := star(A[n,n]) . B[n]
                if self.B[orderedstates[n].stateid] != self.empty:
                    self.B[orderedstates[n].stateid] = \
                        self.star(self.A[orderedstates[n].stateid, orderedstates[n].stateid]) \
                        + self.B[orderedstates[n].stateid]
                else:
                    self.B[orderedstates[n].stateid] = self.star(
                        self.A[orderedstates[n].stateid, orderedstates[n].stateid])
                for j in range(0, n):
                    # A[n,j] := star(A[n,n]) . A[n,j]
                    if self.A[
                            orderedstates[n].stateid,
                            orderedstates[j].stateid] != self.empty:
                        self.A[
                            orderedstates[n].stateid,
                            orderedstates[j].stateid] = \
                            self.star(self.A[orderedstates[n].stateid, orderedstates[n].stateid]) \
                            + self.A[orderedstates[n].stateid, orderedstates[j].stateid]
                    else:
                        self.A[orderedstates[n].stateid, orderedstates[j].stateid] = self.star(
                            self.A[orderedstates[n].stateid, orderedstates[n].stateid])
            for i in range(0, n):
                # B[i] += A[i,n] . B[n]
                newnode = None
                if self.A[orderedstates[i].stateid, orderedstates[n].stateid] != self.empty \
                        and self.B[orderedstates[n].stateid] != self.empty:
                    newnode = self.A[orderedstates[i].stateid, orderedstates[
                        n].stateid] + self.B[orderedstates[n].stateid]
                elif self.A[orderedstates[i].stateid, orderedstates[n].stateid] != self.empty:
                    newnode = self.A[
                        orderedstates[i].stateid,
                        orderedstates[n].stateid]
                elif self.B[orderedstates[n].stateid] != self.empty:
                    newnode = self.B[orderedstates[n].stateid]
                if self.B[orderedstates[i].stateid] != self.empty:
                    if newnode is not None:
                        self.B[orderedstates[i].stateid] += newnode
                else:
                    self.B[orderedstates[i].stateid] = newnode
                for j in range(0, n):
                    # A[i,j] += A[i,n] . A[n,j]
                    newnode = None
                    if self.A[
                            orderedstates[i].stateid,
                            orderedstates[n].stateid] != self.empty \
                            and self.A[orderedstates[n].stateid, orderedstates[j].stateid] \
                                    != self.empty:
                        newnode = self.A[orderedstates[i].stateid, orderedstates[
                            n].stateid] + self.A[orderedstates[n].stateid, orderedstates[j].stateid]
                    elif self.A[orderedstates[i].stateid, orderedstates[n].stateid] != self.empty:
                        newnode = self.A[
                            orderedstates[i].stateid,
                            orderedstates[n].stateid]
                    elif self.A[orderedstates[n].stateid, orderedstates[j].stateid] != self.empty:
                        newnode = self.A[
                            orderedstates[n].stateid,
                            orderedstates[j].stateid]
                    if self.A[
                            orderedstates[i].stateid,
                            orderedstates[j].stateid] != self.empty:
                        if newnode is not None:
                            self.A[
                                orderedstates[i].stateid,
                                orderedstates[j].stateid] += newnode
                    else:
                        self.A[
                            orderedstates[i].stateid,
                            orderedstates[j].stateid] = newnode