Beispiel #1
0
def make_graph(G):
    g = G(10)
    for u in vertices(g):
        for v in vertices(g):
            if u < v < u + 3:
                add_edge(u, v, g)
    return g
Beispiel #2
0
def test_graph_to_html_with_weird_chars():
    from pybgl.automaton import Automaton, add_edge
    g = Automaton(2)
    add_edge(0, 1, WEIRD_CHARS, g)
    graph_to_html(g)
    if in_ipynb():
        ipynb_display_graph(g)
Beispiel #3
0
def moore_determination(nfa: Nfa,
                        dfa: Automaton = None,
                        complete: bool = True) -> Automaton:
    """
    Converts the input NFA into a DFA.
    The output DFA has a state for every *reachable* subset of states in the input NFA.
    In the worst case, there will be an exponential increase in the number of states.
    Args:
        nfa: An `Nfa` instance.
        dfa: Pass `None` or a reference to an empty `Automaton` instance.
        complete: Pass `True` to build the complete automaton (original algorithm).
          Pass `False` to build a smaller automaton (this save the "trash" state
          and its corresponding input transitions).
    Returns:
        The corresponding `Automaton` instance.
    """
    def dfa_add_state(qs):
        q = map_qs_q[qs] = add_vertex(dfa)
        if any(is_final(_, nfa) for _ in qs):
            set_final(q, dfa)
        return q

    full_sigma = alphabet(nfa)
    if dfa is None:
        dfa = Automaton()
    map_qs_q = dict(
    )  # Maps subset of states of nfa with the corresponding dfa state.

    q0s = frozenset(nfa.delta_epsilon(initials(nfa)))
    unprocessed_qs = set(
    )  # Keeps track of qs for which delta is not yet installed in dfa
    unprocessed_qs.add(q0s)
    q0 = dfa_add_state(q0s)

    while unprocessed_qs:
        qs = unprocessed_qs.pop()
        q = map_qs_q[qs]
        sigma_ = (full_sigma if complete else set.union(
            *[sigma(q, nfa) for q in qs]) if qs else set())
        for a in sigma_:
            rs = (frozenset(set.union(*[delta(q, a, nfa)
                                        for q in qs])) if qs else frozenset())
            r = map_qs_q.get(rs)
            if r is None:
                r = dfa_add_state(rs)
                unprocessed_qs.add(rs)
            add_edge(q, r, a, dfa)
    return dfa
Beispiel #4
0
def test_graph_to_html_with_html_sequences():
    from collections import defaultdict
    from pybgl.property_map import make_assoc_property_map

    g = DirectedGraph(2)
    (e, _) = add_edge(0, 1, g)
    pmap_vlabel = make_assoc_property_map(defaultdict(str))
    pmap_elabel = make_assoc_property_map(defaultdict(str))
    gdp = GraphDp(g, dpv={"label": pmap_vlabel}, dpe={"label": pmap_elabel})

    for label in [
            "<b>foo</b>",
            "<foo>",
            "<",
            ">",
            "<b>foo</b><bar>",
            "<bar><b>foo</b>",
            "<font color='red'><b>foo</b></font>",
            # NB: foo.png must exists + graphviz imposes <img/> not <img>
            #"<table><tr><td><img src='foo.png'/></td></tr></table>",
    ]:
        print(f"{label} --> {graphviz_escape_html(label)}")
        pmap_vlabel[0] = pmap_vlabel[1] = pmap_elabel[e] = label
        shtml = graph_to_html(gdp)
        if in_ipynb():
            html(shtml)
            ipynb_display_graph(gdp)
Beispiel #5
0
 def examine_relevant_edge(self, e: EdgeDescriptor, g: Automaton):
     u = source(e, g)
     v = target(e, g)
     a = label(e, g)
     u_dup = self.m_pmap_vertices[u]
     v_dup = self.m_pmap_vertices[
         v] if v in self.m_dup_vertices else self.dup_vertex(v, g)
     (e_dup, _) = add_edge(u_dup, v_dup, a, self.m_g_dup)
     if self.m_pmap_edges:
         self.m_pmap_edges[e] = e_dup
     if self.m_callback_dup_edge:
         self.m_callback_dup_edge(e, g, e_dup, self.m_g_dup)
Beispiel #6
0
    def add_product_edge(self, e1 :EdgeDescriptor, g1 :Automaton, e2 :EdgeDescriptor, g2 :Automaton):
        if e1:
            q1 = source(e1, g1)
            r1 = target(e1, g1)
            a = label(e1, g1)
        else:
            q1 = r1 = BOTTOM

        if e2:
            q2 = source(e2, g2)
            r2 = target(e2, g2)
            a = label(e2, g2)
        else:
            q2 = r2 = BOTTOM

        q12 = self.get_or_create_product_vertex(q1, g1, q2, g2)
        r12 = self.get_or_create_product_vertex(r1, g1, r2, g2)
        return add_edge(q12, r12, a, self.g12)
Beispiel #7
0
def test_graph_to_html():
    g = DirectedGraph(2)
    (e, _) = add_edge(0, 1, g)
    shtml = graph_to_html(g)
    if in_ipynb():
        ipynb_display_graph(g)