def __init__(self, D): # refine partition of states by reversed neighborhoods N = D.reverse() P = PartitionRefinement(D.states()) P.refine([s for s in D.states() if D.isfinal(s)]) unrefined = Sequence(P, key=id) while unrefined: part = arbitrary_item(unrefined) unrefined.remove(part) for symbol in D.alphabet: neighbors = set() for state in part: neighbors |= N.transition(state, symbol) for new, old in P.refine(neighbors): if old in unrefined or len(new) < len(old): unrefined.append(new) else: unrefined.append(old) # convert partition to DFA P.freeze() self.partition = P self.initial = P[D.initial] self.alphabet = D.alphabet self.DFA = D
def __init__(self,D): # refine partition of states by reversed neighborhoods N = D.reverse() P = PartitionRefinement(D.states()) P.refine([s for s in D.states() if D.isfinal(s)]) unrefined = Sequence(P,key=id) while unrefined: part = arbitrary_item(unrefined) unrefined.remove(part) for symbol in D.alphabet: neighbors = Set() for state in part: neighbors |= N.transition(state,symbol) for new,old in P.refine(neighbors): if old in unrefined or len(new) < len(old): unrefined.append(new) else: unrefined.append(old) # convert partition to DFA P.freeze() self.partition = P self.initial = P[D.initial] self.alphabet = D.alphabet self.DFA = D
def minimize(dfa): ''' Under construction :) Based on ... ''' # refine partition of states by reversed neighborhoods partition = PartitionRefinement(dfa.g.nodes_iter()) partition.refine(dfa.final) unrefined = dict([(id(p), p) for p in partition]) while unrefined: part = unrefined.pop(unrefined.iterkeys().next()) for symbol in dfa.alphabet: neighbors = set([ v for v, _, d in dfa.g.in_edges_iter(part, data=True) if symbol in d['input'] ]) for new, old in partition.refine(neighbors): if id(old) in unrefined or len(new) < len(old): unrefined[id(new)] = new else: unrefined[id(old)] = old print dfa.g.number_of_nodes() print len(list(partition)) # convert partition to DFA nx.condensation
def LexBFS(G): """Find lexicographic breadth-first-search traversal order of a graph. G should be represented in such a way that "for v in G" loops through the vertices, and "G[v]" produces a sequence of the neighbors of v; for instance, G may be a dictionary mapping each vertex to its neighbor set. Running time is O(n+m) and additional space usage over G is O(n). """ P = PartitionRefinement(G) S = Sequence(P, key=id) while S: set = S[0] v = arbitrary_item(set) yield v P.remove(v) if not set: S.remove(set) for new,old in P.refine(G[v]): S.insertBefore(old,new)