{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Question 15\n",
"\n",
"Draw a DFA that accepts the language:\n",
"\n",
"$$L = \\{ w \\in \\{0,1\\}^* | n_0(w) \\text{ and } n_1(w) \\text { are both odd } \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "47055e509be0224e"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q15 = DFA(\n",
"\tstates={'A', 'B', 'C', 'D'},\n",
"\tinput_symbols={'0', '1'},\n",
"\ttransitions={\n",
"\t\t'A': {'0': 'C', '1': 'B'},\n",
"\t\t'B': {'0': 'D', '1': 'A'},\n",
"\t\t'C': {'0': 'A', '1': 'D'},\n",
"\t\t'D': {'0': 'B', '1': 'C'}\n",
"\t},\n",
"\tinitial_state='A',\n",
"\tfinal_states={'D'}\n",
")\n",
"t = [\n",
"\tq15.accepts_input(\"010101\"),\n",
"\tq15.accepts_input(\"000111\"),\n",
"\tq15.accepts_input(\"01\"),\n",
"]\n",
"f = [\n",
"\tq15.accepts_input(\"\"),\n",
"\tq15.accepts_input(\"010\"),\n",
"\tq15.accepts_input(\"0011\")\n",
"]\n",
"\n",
"# print(t)\n",
"# print(f)\n",
"q15.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.684624Z",
"start_time": "2024-02-10T22:40:20.444666600Z"
}
},
"id": "42f912166acd276e",
"execution_count": 1
},
{
"cell_type": "markdown",
"source": [
"\n",
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "cf53db9d4fcae2c9"
},
{
"cell_type": "markdown",
"source": [
"# Question 16\n",
"\n",
"Draw a state diagram with the given formal definition the finite automata"
],
"metadata": {
"collapsed": false
},
"id": "bfa5270ba2970a9"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q16 = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},\n",
"\tinput_symbols={'a', 'b', 'c'},\n",
"\ttransitions={\n",
"\t\t'q0': {'a': 'q0', 'b': 'q1', 'c': 'q0'},\n",
"\t\t'q1': {'a': 'q1', 'b': 'q2', 'c': 'q1'},\n",
"\t\t'q2': {'a': 'q2', 'b': 'q3', 'c': 'q2'},\n",
"\t\t'q3': {'a': 'q3', 'b': 'q4', 'c': 'q3'},\n",
"\t\t'q4': {'a': 'q4', 'b': 'q5', 'c': 'q4'},\n",
"\t\t'q5': {'a': 'q5', 'b': 'q5', 'c': 'q5'},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q2','q4'}\n",
")\n",
"\n",
"t = [\n",
"\tq16.accepts_input(\"abbccccc\"),\n",
"\tq16.accepts_input(\"abbcccbbcc\"),\n",
"\tq16.accepts_input(\"abcccbcc\"),\n",
"]\n",
"f = [\n",
"\tq16.accepts_input(\"abc\"),\n",
"\tq16.accepts_input(\"\"),\n",
"\tq16.accepts_input(\"abbccbbcccb\")\n",
"]\n",
"# print(t)\n",
"# print(f)\n",
"q16.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.685624300Z",
"start_time": "2024-02-10T22:40:20.684624Z"
}
},
"id": "4050347d7a21fc36",
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"The language accepted by this DFA is:\n",
"\n",
"$$L = \\{ w \\in \\{a,b,c\\}^* | n_b(w) = 2 \\text{ or } n_b(w) = 4 \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "3e0bab1534a434b6"
},
{
"cell_type": "markdown",
"source": [
"\n",
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "e2fc5655fb27610e"
},
{
"cell_type": "markdown",
"source": [
"# Question 17\n",
"\n",
"(c) $L = \\{ w \\in \\{a,b\\}^* | w \\text{ contains the substring ba exactly once } \\}$"
],
"metadata": {
"collapsed": false
},
"id": "3ff27392d5e09f18"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q17c = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},\n",
"\tinput_symbols={'a', 'b'},\n",
"\ttransitions={\n",
"\t\t'q0': {'a': 'q0', 'b': 'q1'},\n",
"\t\t'q1': {'a': 'q2', 'b': 'q1'},\n",
"\t\t'q2': {'a': 'q3', 'b': 'q4'},\n",
"\t\t'q3': {'a': 'q3', 'b': 'q4'},\n",
"\t\t'q4': {'a': 'q5', 'b': 'q4'},\n",
"\t\t'q5': {'a': 'q5', 'b': 'q5'},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q2', 'q3', 'q4'}\n",
")\n",
"t = [\n",
"\tq17c.accepts_input(\"ba\"),\n",
"\tq17c.accepts_input(\"aaaaaba\"),\n",
"\tq17c.accepts_input(\"baaaaab\"),\n",
"\tq17c.accepts_input(\"baaaaa\"),\n",
"\tq17c.accepts_input(\"aaaabaaaaa\"),\n",
"\tq17c.accepts_input(\"bab\"),\n",
"\tq17c.accepts_input(\"bbbbbbbbbbbba\"),\n",
"\tq17c.accepts_input(\"bbbbbbbbbbbbbab\"),\n",
"]\n",
"f = [\n",
"\tq17c.accepts_input(\"baaaaaba\"),\n",
"\tq17c.accepts_input(\"baba\"),\n",
"\tq17c.accepts_input(\"aababa\"),\n",
"\tq17c.accepts_input(\"baaabaab\"),\n",
"\tq17c.accepts_input(\"bbbbbbbbbbbbaba\"),\n",
"]\n",
"# print(t)\n",
"# print(f)\n",
"q17c.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.689625400Z",
"start_time": "2024-02-10T22:40:20.684624Z"
}
},
"id": "373a99c58af8b2b6",
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"(d) $L = \\{ ab^ma^n | m,n \\in \\mathbb{Z}, m \\geq 2, n \\geq 1 \\}$"
],
"metadata": {
"collapsed": false
},
"id": "5e343e46b83de317"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q17d = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},\n",
"\tinput_symbols={'a', 'b'},\n",
"\ttransitions={\n",
"\t\t'q0': {'a': 'q1', 'b': 'q5'},\n",
"\t\t'q1': {'a': 'q5', 'b': 'q2'},\n",
"\t\t'q2': {'a': 'q5', 'b': 'q3'},\n",
"\t\t'q3': {'a': 'q4', 'b': 'q3'},\n",
"\t\t'q4': {'a': 'q4', 'b': 'q5'},\n",
"\t\t'q5': {'a': 'q5', 'b': 'q5'},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q4'}\n",
")\n",
"t = [\n",
"\tq17d.accepts_input(\"abba\"),\n",
"\tq17d.accepts_input(\"abbbba\"),\n",
"\tq17d.accepts_input(\"abbaaaaaa\"),\n",
"\tq17d.accepts_input(\"abbbbbaaaaa\"),\n",
"]\n",
"f = [\n",
"\tq17d.accepts_input(\"\"),\n",
"\tq17d.accepts_input(\"bba\"),\n",
"\tq17d.accepts_input(\"a\"),\n",
"\tq17d.accepts_input(\"aba\"),\n",
"\tq17d.accepts_input(\"abbab\"),\n",
"\tq17d.accepts_input(\"abbbbaaab\"),\n",
"\tq17d.accepts_input(\"abb\"),\n",
"]\n",
"# print(t)\n",
"# print(f)\n",
"q17d.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.722624Z",
"start_time": "2024-02-10T22:40:20.694623600Z"
}
},
"id": "435890c731b913c1",
"execution_count": 4
},
{
"cell_type": "markdown",
"source": [
"\n",
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "8b04d737ea5c7587"
},
{
"cell_type": "markdown",
"source": [
"# Question 18\n",
"\n",
"$$L = \\{ w \\in \\{0,1\\}^* | n_0(w) \\text{ is not a multiple of 5 } \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "be6ef3b25e4d501a"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q18 = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},\n",
"\tinput_symbols={'0', '1'},\n",
"\ttransitions={\n",
"\t\t'q0': {'0': 'q1', '1': 'q0'},\n",
"\t\t'q1': {'0': 'q2', '1': 'q1'},\n",
"\t\t'q2': {'0': 'q3', '1': 'q2'},\n",
"\t\t'q3': {'0': 'q4', '1': 'q3'},\n",
"\t\t'q4': {'0': 'q5', '1': 'q4'},\n",
"\t\t'q5': {'0': 'q1', '1': 'q5'},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q1', 'q2', 'q3', 'q4'}\n",
")\n",
"t = [\n",
"\tq18.accepts_input(\"0\"),\n",
"\tq18.accepts_input(\"00\"),\n",
"\tq18.accepts_input(\"000\"),\n",
"\tq18.accepts_input(\"0000\"),\n",
"\tq18.accepts_input(\"000000\"),\n",
"\tq18.accepts_input(\"0000100\"),\n",
"\tq18.accepts_input(\"10000001\"),\n",
"\tq18.accepts_input(\"010\"),\n",
"\tq18.accepts_input(\"01\"),\n",
"]\n",
"f = [\n",
"\tq18.accepts_input(\"\"),\n",
"\tq18.accepts_input(\"1\"),\n",
"\tq18.accepts_input(\"00000\"),\n",
"\tq18.accepts_input(\"1000001\"),\n",
"\tq18.accepts_input(\"001000\"),\n",
"\tq18.accepts_input(\"0000000000\"),\n",
"]\n",
"# print(t)\n",
"# print(f)\n",
"q18.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.776624700Z",
"start_time": "2024-02-10T22:40:20.724626600Z"
}
},
"id": "e0757dc4f0eae75b",
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"\n",
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "5298da4937a41c"
},
{
"cell_type": "markdown",
"source": [
"# Question 20\n",
"\n",
"$$L = \\{ w \\in \\{a,b\\}^* | n_a(w) \\leq 1 \\text{ or } n_b(w) \\geq 2 \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "82014e661a375124"
},
{
"cell_type": "code",
"outputs": [
{
"data": {
"image/svg+xml": "\n\n\n\n\n",
"text/plain": ">"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"q20 = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8'},\n",
"\tinput_symbols={'0', '1'},\n",
"\ttransitions={\n",
"\t\t'q0': {'0': 'q1', '1': 'q3'},\n",
"\t\t'q1': {'0': 'q2', '1': 'q4'},\n",
"\t\t'q2': {'0': 'q2', '1': 'q5'},\n",
"\t\t'q3': {'0': 'q4', '1': 'q6'},\n",
"\t\t'q4': {'0': 'q5', '1': 'q7'},\n",
"\t\t'q5': {'0': 'q5', '1': 'q8'},\n",
"\t\t'q6': {'0': 'q7', '1': 'q6'},\n",
"\t\t'q7': {'0': 'q8', '1': 'q7'},\n",
"\t\t'q8': {'0': 'q8', '1': 'q8'},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q8'}\n",
")\n",
"t = [\n",
"\tq20.accepts_input(\"\"),\n",
"\tq20.accepts_input(\"1\"),\n",
"\tq20.accepts_input(\"0\"),\n",
"\tq20.accepts_input(\"1100\"),\n",
"\tq20.accepts_input(\"00\"),\n",
"\tq20.accepts_input(\"000000\"),\n",
"\tq20.accepts_input(\"0000001\"),\n",
"\tq20.accepts_input(\"00000011\"),\n",
"\tq20.accepts_input(\"1010\"),\n",
"\tq20.accepts_input(\"0101\"),\n",
"]\n",
"f = [\n",
"\tq20.accepts_input(\"11\"),\n",
"\tq20.accepts_input(\"11111\"),\n",
"\tq20.accepts_input(\"1110\"),\n",
"\tq20.accepts_input(\"1011\"),\n",
"\tq20.accepts_input(\"0111\"),\n",
"\tq20.accepts_input(\"011\"),\n",
"\tq20.accepts_input(\"110\"),\n",
"]\n",
"# print(t)\n",
"# print(f)\n",
"q20.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.825624600Z",
"start_time": "2024-02-10T22:40:20.774622800Z"
}
},
"id": "d38eeb22a7a3122d",
"execution_count": 6
},
{
"cell_type": "markdown",
"source": [
"\n",
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "f3714817ea911d3"
},
{
"cell_type": "markdown",
"source": [
"# Question 21\n",
"\n",
"To make it easier to identify the language, it can be helpful to look for patterns in strings that are in the accepted language\n",
"\n",
"(c)\n",
"\n",
"Example Accepted Strings:\n",
"10100,\n",
"10111100,\n",
"101111010100,\n",
"10100100\n",
"\n",
"Looking at these strings for patterns and the DFA, I can see that the things in common are that all strings must start with $101$ and end with $00$\n",
"\n",
"The formal language definition for this would be:\n",
"\n",
"$$L = \\{ w \\in \\{0,1\\}^* | w \\text{ starts with } 101 \\text{ and } w \\text{ ends with } 00 \\}$$\n",
"\n",
"(d)\n",
"\n",
"Example Accepted Strings:\n",
"abb,\n",
"bab,\n",
"bba\n",
"\n",
"Looking at these strings and looking at the DFA, I can see that the DFA only accepts strings with 1 $a$ and 2 $b$\n",
"\n",
"The formal language definition for this would be:\n",
"\n",
"$$L = \\{ w \\in \\{a,b\\}^* | n_a(w) = 1 \\text{ and } n_b(w) = 2 \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "7b9207cb562c37c6"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}