-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HA-#2.0] Add initial solution for 2nd ht
- Loading branch information
Showing
4 changed files
with
42 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pyformlang.regular_expression import Regex | ||
from pyformlang.finite_automaton import NondeterministicFiniteAutomaton, State, Symbol | ||
from networkx import MultiDiGraph | ||
|
||
|
||
def regex_to_dfa(regex): | ||
return Regex(regex).to_epsilon_nfa().minimize() | ||
|
||
|
||
def graph_to_nfa(graph: MultiDiGraph, start_states: set, final_states: set): | ||
nDfa = NondeterministicFiniteAutomaton() | ||
graphNodes = set(graph.nodes()) | ||
|
||
for cur in start_states.union(graphNodes): | ||
nDfa.add_start_state(State(cur)) | ||
for cur in final_states.union(graphNodes): | ||
nDfa.add_final_state(State(cur)) | ||
|
||
for start, end, label in graph.edges.data("label"): | ||
nDfa.add_transition(State(start), Symbol(label), State(end)) | ||
return nDfa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters