Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaroby committed Oct 8, 2015
1 parent 95a68ef commit bf437c0
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 3 deletions.
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pycharm
.idea/
10 changes: 10 additions & 0 deletions A1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"initial": 0,
"final": [0],
"alpha": ["a", "b"],
"transitions": [
[1, 2],
[2, 0],
[0, 1]
]
}
11 changes: 11 additions & 0 deletions A2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"initial": 0,
"final": [2],
"alpha": ["a", "b"],
"transitions": [
[1, 2],
[3, 2],
[1, 2],
[3, 3]
]
}
6 changes: 3 additions & 3 deletions DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def pretty_print(self):
"""
print ""
print "This DFA has %s states" % len(self.states)
print "States:", self.states
print "Alphabet:", self.alphabet
print "States:", sorted(self.states)
print "Alphabet:", sorted(self.alphabet)
print "Starting state:", self.start
print "Accepting states:", self.accepts
print "Accepting states:", sorted(self.accepts)
print "Transition function:"
print "\t","\t".join(map(str, sorted(self.states)))
for c in self.alphabet:
Expand Down
75 changes: 75 additions & 0 deletions union.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from graphviz import Digraph
from DFA import DFA, union

A1 = {
"initial": 0,
"final": [0],
"alpha": ["a", "b"],
"transitions": [
[1, 2],
[2, 0],
[0, 1]
]
}
A2 = {
"initial": 0,
"final": [2],
"alpha": ["a", "b"],
"transitions": [
[1, 2],
[3, 2],
[1, 2],
[3, 3]
]
}


def delta_from_table(jdfa):
res = {}
alpha = jdfa['alpha']
for i, transition in enumerate(jdfa['transitions']):
for j, tgt in enumerate(transition):
res[(i, alpha[j])] = tgt

return lambda s, a: res[(s, a)]


def dfa_from_jdfa(jdfa):
states = range(len(jdfa['transitions']))
delta = delta_from_table(jdfa)

return DFA(states, jdfa['alpha'], delta, jdfa['initial'], jdfa['final'])


def plot(dfa):
dot = Digraph(comment='')

for s in dfa.states:
attrs = {'shape': 'circle'}
if s == dfa.start:
attrs['fillcolor'] = 'yellow'
if s in dfa.accepts:
attrs['shape'] = 'doublecircle'

dot.node(str(s), **attrs)

for s in dfa.states:
for sym in dfa.alphabet:
r = dfa.delta(s, sym)
dot.edge(str(s), str(r), label=sym)

return dot


def main():
da1 = dfa_from_jdfa(A1)
da2 = dfa_from_jdfa(A2)
da3 = union(da1, da2)

dot = plot(da3)
print dot.source
dot.render(view=True)


if __name__ == '__main__':
main()

0 comments on commit bf437c0

Please sign in to comment.