CS2333/Assignment 3.ipynb
2024-02-10 18:41:16 -04:00

563 lines
64 KiB
Plaintext

{
"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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"339pt\" height=\"100pt\"\n viewBox=\"0.00 0.00 338.60 100.46\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 96.46)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-96.46 334.6,-96.46 334.6,4 -4,4\"/>\n<!-- 66ce28c0&#45;3b81&#45;4457&#45;a1f2&#45;6585ac09c24e -->\n<g id=\"node1\" class=\"node\">\n<title>66ce28c0&#45;3b81&#45;4457&#45;a1f2&#45;6585ac09c24e</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-22.46\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- A -->\n<g id=\"node2\" class=\"node\">\n<title>A</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"58.6\" cy=\"-22.46\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"58.6\" y=\"-18.76\" font-family=\"Times-Roman\" font-size=\"14.00\">A</text>\n</g>\n<!-- 66ce28c0&#45;3b81&#45;4457&#45;a1f2&#45;6585ac09c24e&#45;&gt;A -->\n<g id=\"edge1\" class=\"edge\">\n<title>66ce28c0&#45;3b81&#45;4457&#45;a1f2&#45;6585ac09c24e&#45;&gt;A</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;A\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.75,-22.46C7.25,-22.46 19.84,-22.46 31.96,-22.46\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"32.08,-25.44 40.58,-22.46 32.07,-19.49 32.08,-25.44\"/>\n</a>\n</g>\n</g>\n<!-- B -->\n<g id=\"node3\" class=\"node\">\n<title>B</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"140.6\" cy=\"-64.46\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"140.6\" y=\"-60.76\" font-family=\"Times-Roman\" font-size=\"14.00\">B</text>\n</g>\n<!-- A&#45;&gt;B -->\n<g id=\"edge2\" class=\"edge\">\n<title>A&#45;&gt;B</title>\n<path fill=\"none\" stroke=\"black\" d=\"M70.58,-35.95C76.9,-42.82 85.4,-50.71 94.6,-55.46 100.54,-58.53 107.41,-60.55 113.98,-61.89\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"113.75,-64.86 122.62,-63.31 114.72,-58.99 113.75,-64.86\"/>\n<text text-anchor=\"middle\" x=\"99.6\" y=\"-62.26\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- C -->\n<g id=\"node4\" class=\"node\">\n<title>C</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"312.6\" cy=\"-22.46\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"312.6\" y=\"-18.76\" font-family=\"Times-Roman\" font-size=\"14.00\">C</text>\n</g>\n<!-- A&#45;&gt;C -->\n<g id=\"edge3\" class=\"edge\">\n<title>A&#45;&gt;C</title>\n<path fill=\"none\" stroke=\"black\" d=\"M76.92,-22.46C120.45,-22.46 234.62,-22.46 285.95,-22.46\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"285.99,-25.44 294.49,-22.46 285.99,-19.49 285.99,-25.44\"/>\n<text text-anchor=\"middle\" x=\"181.6\" y=\"-26.26\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- B&#45;&gt;A -->\n<g id=\"edge4\" class=\"edge\">\n<title>B&#45;&gt;A</title>\n<path fill=\"none\" stroke=\"black\" d=\"M126.8,-52.3C120.51,-46.84 112.57,-40.7 104.6,-36.46 98.54,-33.24 91.62,-30.63 85.05,-28.57\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"85.43,-25.59 76.44,-26.11 83.79,-31.31 85.43,-25.59\"/>\n<text text-anchor=\"middle\" x=\"99.6\" y=\"-40.26\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- D -->\n<g id=\"node5\" class=\"node\">\n<title>D</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"226.6\" cy=\"-66.46\" rx=\"22\" ry=\"22\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"226.6\" cy=\"-66.46\" rx=\"26\" ry=\"26\"/>\n<text text-anchor=\"middle\" x=\"226.6\" y=\"-62.76\" font-family=\"Times-Roman\" font-size=\"14.00\">D</text>\n</g>\n<!-- B&#45;&gt;D -->\n<g id=\"edge5\" class=\"edge\">\n<title>B&#45;&gt;D</title>\n<path fill=\"none\" stroke=\"black\" d=\"M158.68,-65.64C164.38,-65.97 170.76,-66.3 176.6,-66.46 182.66,-66.64 189.13,-66.71 195.33,-66.73\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"195.63,-69.71 204.13,-66.73 195.63,-63.76 195.63,-69.71\"/>\n<text text-anchor=\"middle\" x=\"181.6\" y=\"-70.26\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- C&#45;&gt;A -->\n<g id=\"edge6\" class=\"edge\">\n<title>C&#45;&gt;A</title>\n<path fill=\"none\" stroke=\"black\" d=\"M295.04,-17.75C269.66,-11.06 219.54,0 176.6,-3.46 144.77,-6.03 108.54,-12.47 84.94,-17.14\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"84.29,-14.23 76.55,-18.83 85.47,-20.07 84.29,-14.23\"/>\n<text text-anchor=\"middle\" x=\"181.6\" y=\"-7.26\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- C&#45;&gt;D -->\n<g id=\"edge7\" class=\"edge\">\n<title>C&#45;&gt;D</title>\n<path fill=\"none\" stroke=\"black\" d=\"M295.41,-28.01C286.68,-31.23 275.83,-35.61 266.6,-40.46 261.78,-43 256.81,-45.99 252.11,-49.02\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"250.41,-46.57 244.98,-53.75 253.71,-51.53 250.41,-46.57\"/>\n<text text-anchor=\"middle\" x=\"271.6\" y=\"-44.26\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- D&#45;&gt;B -->\n<g id=\"edge8\" class=\"edge\">\n<title>D&#45;&gt;B</title>\n<path fill=\"none\" stroke=\"black\" d=\"M207.2,-55.38C200.89,-52.17 193.65,-49.09 186.6,-47.46 179,-45.71 170.95,-47.5 163.79,-50.48\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"162.18,-47.96 155.88,-54.39 164.82,-53.29 162.18,-47.96\"/>\n<text text-anchor=\"middle\" x=\"181.6\" y=\"-51.26\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- D&#45;&gt;C -->\n<g id=\"edge9\" class=\"edge\">\n<title>D&#45;&gt;C</title>\n<path fill=\"none\" stroke=\"black\" d=\"M248.97,-64.2C257.89,-62.63 268.13,-59.96 276.6,-55.46 283.03,-52.05 289.14,-47.13 294.4,-42.13\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"296.73,-44.01 300.57,-35.86 292.49,-39.83 296.73,-44.01\"/>\n<text text-anchor=\"middle\" x=\"271.6\" y=\"-62.26\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb382690390>>"
},
"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": [
"<div style=\"page-break-after: always;\"></div>\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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"567pt\" height=\"98pt\"\n viewBox=\"0.00 0.00 567.33 98.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 94.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-94.25 563.33,-94.25 563.33,4 -4,4\"/>\n<!-- dba868d3&#45;1c04&#45;46ca&#45;b078&#45;ab22380f1c61 -->\n<g id=\"node1\" class=\"node\">\n<title>dba868d3&#45;1c04&#45;46ca&#45;b078&#45;ab22380f1c61</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-30.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q0 -->\n<g id=\"node2\" class=\"node\">\n<title>q0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"63.35\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- dba868d3&#45;1c04&#45;46ca&#45;b078&#45;ab22380f1c61&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>dba868d3&#45;1c04&#45;46ca&#45;b078&#45;ab22380f1c61&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-30.5C7.36,-30.5 19.4,-30.5 31.65,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-33.48 40.48,-30.5 31.98,-27.53 31.98,-33.48\"/>\n</a>\n</g>\n</g>\n<!-- q0&#45;&gt;q0 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M56,-52.3C55.19,-62.41 57.65,-71.25 63.35,-71.25 67.27,-71.25 69.65,-67.07 70.5,-61.17\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"73.48,-60.87 70.7,-52.3 67.53,-60.73 73.48,-60.87\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,c</text>\n</g>\n<!-- q1 -->\n<g id=\"node3\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"154.84\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q1</text>\n</g>\n<!-- q0&#45;&gt;q1 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M86.47,-30.5C97.53,-30.5 111.08,-30.5 123.13,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"123.48,-33.48 131.98,-30.5 123.48,-27.53 123.48,-33.48\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q1&#45;&gt;q1 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M146.8,-51.88C145.8,-62.17 148.48,-71.25 154.84,-71.25 159.32,-71.25 161.97,-66.76 162.81,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"165.79,-60.41 162.89,-51.88 159.84,-60.36 165.79,-60.41\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,c</text>\n</g>\n<!-- q2 -->\n<g id=\"node4\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"250.34\" cy=\"-30.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"250.34\" cy=\"-30.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"250.34\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M178,-30.5C188.95,-30.5 202.42,-30.5 214.69,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"214.86,-33.48 223.36,-30.5 214.86,-27.53 214.86,-33.48\"/>\n<text text-anchor=\"middle\" x=\"200.59\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q2&#45;&gt;q2 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q2&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M241.42,-55.8C240.8,-66.33 243.78,-75.25 250.34,-75.25 254.95,-75.25 257.79,-70.84 258.86,-64.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"261.84,-64.43 259.26,-55.8 255.9,-64.16 261.84,-64.43\"/>\n<text text-anchor=\"middle\" x=\"250.34\" y=\"-79.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,c</text>\n</g>\n<!-- q3 -->\n<g id=\"node5\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"345.84\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"345.84\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q2&#45;&gt;q3 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M277.38,-30.5C288.84,-30.5 302.37,-30.5 314.29,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"314.54,-33.48 323.04,-30.5 314.54,-27.53 314.54,-33.48\"/>\n<text text-anchor=\"middle\" x=\"300.09\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M337.79,-51.88C336.79,-62.17 339.48,-71.25 345.84,-71.25 350.31,-71.25 352.96,-66.76 353.8,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"356.78,-60.41 353.88,-51.88 350.83,-60.36 356.78,-60.41\"/>\n<text text-anchor=\"middle\" x=\"345.84\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,c</text>\n</g>\n<!-- q4 -->\n<g id=\"node7\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"441.33\" cy=\"-30.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"441.33\" cy=\"-30.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"441.33\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M368.99,-30.5C379.94,-30.5 393.41,-30.5 405.68,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"405.85,-33.48 414.35,-30.5 405.85,-27.53 405.85,-33.48\"/>\n<text text-anchor=\"middle\" x=\"391.58\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q5 -->\n<g id=\"node6\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"536.83\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"536.83\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q5&#45;&gt;q5 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q5&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M528.78,-51.88C527.79,-62.17 530.47,-71.25 536.83,-71.25 541.3,-71.25 543.95,-66.76 544.79,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"547.77,-60.41 544.88,-51.88 541.82,-60.35 547.77,-60.41\"/>\n<text text-anchor=\"middle\" x=\"536.83\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M468.38,-30.5C479.83,-30.5 493.36,-30.5 505.29,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"505.53,-33.48 514.03,-30.5 505.53,-27.53 505.53,-33.48\"/>\n<text text-anchor=\"middle\" x=\"491.08\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q4&#45;&gt;q4 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q4&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M432.41,-55.8C431.8,-66.33 434.77,-75.25 441.33,-75.25 445.95,-75.25 448.79,-70.84 449.85,-64.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"452.83,-64.43 450.25,-55.8 446.89,-64.16 452.83,-64.43\"/>\n<text text-anchor=\"middle\" x=\"441.33\" y=\"-79.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,c</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3a00cb360>>"
},
"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": [
"<div style=\"page-break-after: always;\"></div>\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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"572pt\" height=\"146pt\"\n viewBox=\"0.00 0.00 572.33 146.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 142.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-142.25 568.33,-142.25 568.33,4 -4,4\"/>\n<!-- a6cadb2b&#45;07b1&#45;4892&#45;b4cf&#45;723e983c33aa -->\n<g id=\"node1\" class=\"node\">\n<title>a6cadb2b&#45;07b1&#45;4892&#45;b4cf&#45;723e983c33aa</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-78.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q0 -->\n<g id=\"node2\" class=\"node\">\n<title>q0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"63.35\" cy=\"-78.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-74.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- a6cadb2b&#45;07b1&#45;4892&#45;b4cf&#45;723e983c33aa&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>a6cadb2b&#45;07b1&#45;4892&#45;b4cf&#45;723e983c33aa&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-78.5C7.36,-78.5 19.4,-78.5 31.65,-78.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-81.48 40.48,-78.5 31.98,-75.53 31.98,-81.48\"/>\n</a>\n</g>\n</g>\n<!-- q0&#45;&gt;q0 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M56,-100.3C55.19,-110.41 57.65,-119.25 63.35,-119.25 67.27,-119.25 69.65,-115.07 70.5,-109.17\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"73.48,-108.87 70.7,-100.3 67.53,-108.73 73.48,-108.87\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-123.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q1 -->\n<g id=\"node3\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"154.84\" cy=\"-78.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-74.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q1</text>\n</g>\n<!-- q0&#45;&gt;q1 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M86.47,-78.5C97.53,-78.5 111.08,-78.5 123.13,-78.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"123.48,-81.48 131.98,-78.5 123.48,-75.53 123.48,-81.48\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-82.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q1&#45;&gt;q1 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M146.8,-99.88C145.8,-110.17 148.48,-119.25 154.84,-119.25 159.32,-119.25 161.97,-114.76 162.81,-108.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"165.79,-108.41 162.89,-99.88 159.84,-108.36 165.79,-108.41\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-123.05\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q2 -->\n<g id=\"node4\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"249.34\" cy=\"-78.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"249.34\" cy=\"-78.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"249.34\" y=\"-74.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M177.76,-78.5C188.51,-78.5 201.71,-78.5 213.77,-78.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"213.8,-81.48 222.3,-78.5 213.8,-75.53 213.8,-81.48\"/>\n<text text-anchor=\"middle\" x=\"200.09\" y=\"-82.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q4 -->\n<g id=\"node5\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"447.33\" cy=\"-78.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"447.33\" cy=\"-78.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"447.33\" y=\"-74.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q2&#45;&gt;q4 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q2&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M274.51,-87.56C288,-92.14 305.24,-97.2 321.09,-99.5 344.62,-102.92 351.05,-102.87 374.58,-99.5 387.51,-97.65 401.35,-94.01 413.4,-90.27\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"414.63,-93 421.81,-87.56 412.81,-87.34 414.63,-93\"/>\n<text text-anchor=\"middle\" x=\"347.84\" y=\"-105.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q3 -->\n<g id=\"node6\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"347.84\" cy=\"-30.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"347.84\" cy=\"-30.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"347.84\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q2&#45;&gt;q3 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M273.7,-66.88C286.38,-60.57 302.22,-52.69 315.92,-45.88\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"317.41,-48.46 323.69,-42.01 314.75,-43.14 317.41,-48.46\"/>\n<text text-anchor=\"middle\" x=\"298.59\" y=\"-60.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q4&#45;&gt;q4 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q4&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M438.41,-103.8C437.8,-114.32 440.77,-123.25 447.33,-123.25 451.95,-123.25 454.79,-118.84 455.85,-112.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"458.83,-112.43 456.25,-103.8 452.89,-112.16 458.83,-112.43\"/>\n<text text-anchor=\"middle\" x=\"447.33\" y=\"-127.05\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q5 -->\n<g id=\"node7\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"541.83\" cy=\"-78.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"541.83\" y=\"-74.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M474.1,-78.5C485.34,-78.5 498.61,-78.5 510.35,-78.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"510.46,-81.48 518.96,-78.5 510.46,-75.53 510.46,-81.48\"/>\n<text text-anchor=\"middle\" x=\"496.58\" y=\"-82.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M372.44,-42.12C385.25,-48.43 401.26,-56.31 415.09,-63.12\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"414.01,-65.9 422.95,-66.99 416.64,-60.57 414.01,-65.9\"/>\n<text text-anchor=\"middle\" x=\"397.58\" y=\"-60.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M338.91,-55.8C338.3,-66.33 341.27,-75.25 347.84,-75.25 352.45,-75.25 355.29,-70.84 356.36,-64.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"359.34,-64.43 356.76,-55.8 353.4,-64.16 359.34,-64.43\"/>\n<text text-anchor=\"middle\" x=\"347.84\" y=\"-79.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q5&#45;&gt;q5 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q5&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M533.78,-99.88C532.79,-110.17 535.47,-119.25 541.83,-119.25 546.3,-119.25 548.95,-114.76 549.79,-108.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"552.77,-108.41 549.88,-99.88 546.82,-108.35 552.77,-108.41\"/>\n<text text-anchor=\"middle\" x=\"541.83\" y=\"-123.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3826ae660>>"
},
"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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"557pt\" height=\"219pt\"\n viewBox=\"0.00 0.00 557.33 218.50\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 214.5)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-214.5 553.33,-214.5 553.33,4 -4,4\"/>\n<!-- d6c8a5b1&#45;b4ac&#45;4dc0&#45;9418&#45;6ef0b18b35c4 -->\n<g id=\"node1\" class=\"node\">\n<title>d6c8a5b1&#45;b4ac&#45;4dc0&#45;9418&#45;6ef0b18b35c4</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-22.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q0 -->\n<g id=\"node2\" class=\"node\">\n<title>q0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"63.35\" cy=\"-22.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-18.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- d6c8a5b1&#45;b4ac&#45;4dc0&#45;9418&#45;6ef0b18b35c4&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>d6c8a5b1&#45;b4ac&#45;4dc0&#45;9418&#45;6ef0b18b35c4&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-22.5C7.36,-22.5 19.4,-22.5 31.65,-22.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-25.48 40.48,-22.5 31.98,-19.53 31.98,-25.48\"/>\n</a>\n</g>\n</g>\n<!-- q1 -->\n<g id=\"node3\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"153.84\" cy=\"-118.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"153.84\" y=\"-114.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q1</text>\n</g>\n<!-- q0&#45;&gt;q1 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M79.36,-38.78C93.73,-54.37 115.54,-78.03 131.67,-95.53\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"129.81,-97.9 137.76,-102.13 134.19,-93.87 129.81,-97.9\"/>\n<text text-anchor=\"middle\" x=\"108.6\" y=\"-77.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q5 -->\n<g id=\"node4\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"526.83\" cy=\"-127.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"526.83\" y=\"-123.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q0&#45;&gt;q5 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M85.61,-16.45C103.42,-11.92 129.58,-6.5 152.84,-6.5 152.84,-6.5 152.84,-6.5 432.33,-6.5 478.17,-6.5 504.71,-61.54 517.13,-97.38\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"514.35,-98.45 519.84,-105.59 520,-96.59 514.35,-98.45\"/>\n<text text-anchor=\"middle\" x=\"291.09\" y=\"-10.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q1&#45;&gt;q5 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M166.04,-137.93C180.8,-160.74 209.09,-195.5 244.34,-195.5 244.34,-195.5 244.34,-195.5 432.33,-195.5 462.67,-195.5 489.79,-171.78 506.9,-152.36\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"509.3,-154.13 512.53,-145.72 504.76,-150.28 509.3,-154.13\"/>\n<text text-anchor=\"middle\" x=\"336.84\" y=\"-199.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q2 -->\n<g id=\"node5\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"245.34\" cy=\"-118.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"245.34\" y=\"-114.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M176.97,-118.5C188.02,-118.5 201.58,-118.5 213.62,-118.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"213.97,-121.48 222.47,-118.5 213.97,-115.53 213.97,-121.48\"/>\n<text text-anchor=\"middle\" x=\"199.59\" y=\"-122.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q5&#45;&gt;q5 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q5&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M518.78,-148.88C517.79,-159.17 520.47,-168.25 526.83,-168.25 531.3,-168.25 533.95,-163.76 534.79,-157.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"537.77,-157.41 534.88,-148.88 531.82,-157.35 537.77,-157.41\"/>\n<text text-anchor=\"middle\" x=\"526.83\" y=\"-172.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b</text>\n</g>\n<!-- q2&#45;&gt;q5 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M267.9,-122.85C281.16,-125.31 298.54,-128.16 314.09,-129.5 378.44,-135.05 454.21,-132.04 495.4,-129.6\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"495.83,-132.56 504.13,-129.06 495.46,-126.62 495.83,-132.56\"/>\n<text text-anchor=\"middle\" x=\"382.08\" y=\"-136.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q3 -->\n<g id=\"node6\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"336.84\" cy=\"-64.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"336.84\" y=\"-60.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q2&#45;&gt;q3 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q2&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M265.32,-107.05C278.13,-99.32 295.23,-89 309.37,-80.47\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"311.14,-82.87 316.88,-75.94 308.07,-77.78 311.14,-82.87\"/>\n<text text-anchor=\"middle\" x=\"291.09\" y=\"-96.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M328.79,-85.88C327.79,-96.17 330.48,-105.25 336.84,-105.25 341.31,-105.25 343.96,-100.76 344.8,-94.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"347.78,-94.41 344.88,-85.88 341.83,-94.36 347.78,-94.41\"/>\n<text text-anchor=\"middle\" x=\"336.84\" y=\"-109.05\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q4 -->\n<g id=\"node7\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"431.33\" cy=\"-61.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"431.33\" cy=\"-61.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"431.33\" y=\"-57.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M359.76,-63.79C370.5,-63.44 383.7,-63.01 395.76,-62.62\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"395.9,-65.59 404.3,-62.34 395.71,-59.65 395.9,-65.59\"/>\n<text text-anchor=\"middle\" x=\"382.08\" y=\"-67.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M453.55,-76.48C467.5,-86.32 485.82,-99.26 500.52,-109.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"499.18,-112.34 507.84,-114.8 502.61,-107.47 499.18,-112.34\"/>\n<text text-anchor=\"middle\" x=\"481.08\" y=\"-101.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q4&#45;&gt;q4 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q4&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M422.41,-86.8C421.8,-97.33 424.77,-106.25 431.33,-106.25 435.95,-106.25 438.79,-101.84 439.85,-95.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"442.83,-95.43 440.25,-86.8 436.89,-95.16 442.83,-95.43\"/>\n<text text-anchor=\"middle\" x=\"431.33\" y=\"-110.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3a0967e40>>"
},
"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": [
"<div style=\"page-break-after: always;\"></div>\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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"583pt\" height=\"142pt\"\n viewBox=\"0.00 0.00 583.33 142.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 138.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-138.25 579.33,-138.25 579.33,4 -4,4\"/>\n<!-- 5a7b8517&#45;28a8&#45;430d&#45;9994&#45;0215c01dd1eb -->\n<g id=\"node1\" class=\"node\">\n<title>5a7b8517&#45;28a8&#45;430d&#45;9994&#45;0215c01dd1eb</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-30.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q0 -->\n<g id=\"node2\" class=\"node\">\n<title>q0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"63.35\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- 5a7b8517&#45;28a8&#45;430d&#45;9994&#45;0215c01dd1eb&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>5a7b8517&#45;28a8&#45;430d&#45;9994&#45;0215c01dd1eb&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-30.5C7.36,-30.5 19.4,-30.5 31.65,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-33.48 40.48,-30.5 31.98,-27.53 31.98,-33.48\"/>\n</a>\n</g>\n</g>\n<!-- q0&#45;&gt;q0 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M56,-52.3C55.19,-62.41 57.65,-71.25 63.35,-71.25 67.27,-71.25 69.65,-67.07 70.5,-61.17\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"73.48,-60.87 70.7,-52.3 67.53,-60.73 73.48,-60.87\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q1 -->\n<g id=\"node3\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"158.84\" cy=\"-30.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"158.84\" cy=\"-30.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"158.84\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q1</text>\n</g>\n<!-- q0&#45;&gt;q1 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M86.5,-30.5C97.45,-30.5 110.92,-30.5 123.19,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"123.37,-33.48 131.87,-30.5 123.37,-27.53 123.37,-33.48\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q1&#45;&gt;q1 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M149.92,-55.8C149.31,-66.33 152.28,-75.25 158.84,-75.25 163.46,-75.25 166.3,-70.84 167.36,-64.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"170.34,-64.43 167.76,-55.8 164.4,-64.16 170.34,-64.43\"/>\n<text text-anchor=\"middle\" x=\"158.84\" y=\"-79.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q2 -->\n<g id=\"node4\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"258.34\" cy=\"-71.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"258.34\" cy=\"-71.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"258.34\" y=\"-67.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M183.94,-40.64C196.46,-45.9 211.92,-52.4 225.41,-58.07\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"224.53,-60.93 233.52,-61.48 226.84,-55.45 224.53,-60.93\"/>\n<text text-anchor=\"middle\" x=\"208.59\" y=\"-55.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q2&#45;&gt;q2 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q2&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M249.42,-96.8C248.8,-107.32 251.78,-116.25 258.34,-116.25 262.95,-116.25 265.79,-111.84 266.86,-105.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"269.84,-105.43 267.26,-96.8 263.9,-105.16 269.84,-105.43\"/>\n<text text-anchor=\"middle\" x=\"258.34\" y=\"-120.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q3 -->\n<g id=\"node5\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"357.84\" cy=\"-74.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"357.84\" cy=\"-74.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"357.84\" y=\"-70.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q2&#45;&gt;q3 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M285.21,-72.3C296.56,-72.64 310.05,-73.06 322.24,-73.44\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"322.25,-76.41 330.84,-73.7 322.44,-70.47 322.25,-76.41\"/>\n<text text-anchor=\"middle\" x=\"308.09\" y=\"-76.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge13\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M348.91,-99.8C348.3,-110.32 351.27,-119.25 357.84,-119.25 362.45,-119.25 365.29,-114.84 366.36,-108.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"369.34,-108.43 366.76,-99.8 363.4,-108.16 369.34,-108.43\"/>\n<text text-anchor=\"middle\" x=\"357.84\" y=\"-123.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q4 -->\n<g id=\"node7\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"457.33\" cy=\"-71.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"457.33\" cy=\"-71.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"457.33\" y=\"-67.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M384.71,-73.7C396.06,-73.35 409.54,-72.94 421.74,-72.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"421.94,-75.53 430.34,-72.3 421.75,-69.59 421.94,-75.53\"/>\n<text text-anchor=\"middle\" x=\"407.58\" y=\"-76.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q5 -->\n<g id=\"node6\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"552.83\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"552.83\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q5&#45;&gt;q1 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q5&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M530.47,-24.72C511.63,-20.14 483.36,-14.5 458.33,-14.5 257.34,-14.5 257.34,-14.5 257.34,-14.5 235.87,-14.5 212.08,-18.32 193.38,-22.23\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"192.58,-19.36 184.91,-24.08 193.85,-25.18 192.58,-19.36\"/>\n<text text-anchor=\"middle\" x=\"357.84\" y=\"-18.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q5&#45;&gt;q5 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q5&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M544.78,-51.88C543.79,-62.17 546.47,-71.25 552.83,-71.25 557.3,-71.25 559.95,-66.76 560.79,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"563.77,-60.41 560.88,-51.88 557.82,-60.35 563.77,-60.41\"/>\n<text text-anchor=\"middle\" x=\"552.83\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M482.41,-60.94C495.04,-55.4 510.6,-48.58 523.77,-42.8\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"525.04,-45.49 531.63,-39.36 522.65,-40.04 525.04,-45.49\"/>\n<text text-anchor=\"middle\" x=\"507.08\" y=\"-55.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q4&#45;&gt;q4 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q4&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M448.41,-96.8C447.8,-107.32 450.77,-116.25 457.33,-116.25 461.95,-116.25 464.79,-111.84 465.85,-105.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"468.83,-105.43 466.25,-96.8 462.89,-105.16 468.83,-105.43\"/>\n<text text-anchor=\"middle\" x=\"457.33\" y=\"-120.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3826f8fc0>>"
},
"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": [
"<div style=\"page-break-after: always;\"></div>\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": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.44.0 (0)\n -->\n<!-- Pages: 1 -->\n<svg width=\"504pt\" height=\"261pt\"\n viewBox=\"0.00 0.00 503.83 261.25\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 257.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-257.25 499.83,-257.25 499.83,4 -4,4\"/>\n<!-- 6018d69b&#45;d8b3&#45;424d&#45;9738&#45;9d238e454881 -->\n<g id=\"node1\" class=\"node\">\n<title>6018d69b&#45;d8b3&#45;424d&#45;9738&#45;9d238e454881</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-115.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q0 -->\n<g id=\"node2\" class=\"node\">\n<title>q0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"67.35\" cy=\"-115.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"67.35\" cy=\"-115.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"67.35\" y=\"-111.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- 6018d69b&#45;d8b3&#45;424d&#45;9738&#45;9d238e454881&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>6018d69b&#45;d8b3&#45;424d&#45;9738&#45;9d238e454881&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.9,-115.5C7.49,-115.5 19.32,-115.5 31.74,-115.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.83,-118.48 40.33,-115.5 31.83,-112.53 31.83,-118.48\"/>\n</a>\n</g>\n</g>\n<!-- q1 -->\n<g id=\"node3\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"166.84\" cy=\"-151.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"166.84\" cy=\"-151.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"166.84\" y=\"-147.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q1</text>\n</g>\n<!-- q0&#45;&gt;q1 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M92.95,-124.58C105.17,-129.09 120.09,-134.6 133.24,-139.46\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"132.59,-142.39 141.59,-142.54 134.64,-136.81 132.59,-142.39\"/>\n<text text-anchor=\"middle\" x=\"117.1\" y=\"-138.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q3 -->\n<g id=\"node4\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"166.84\" cy=\"-80.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"166.84\" cy=\"-80.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"166.84\" y=\"-76.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q0&#45;&gt;q3 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M92.95,-106.67C105.07,-102.31 119.86,-97.01 132.95,-92.31\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"134.26,-95 141.25,-89.33 132.24,-89.4 134.26,-95\"/>\n<text text-anchor=\"middle\" x=\"117.1\" y=\"-102.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q4 -->\n<g id=\"node5\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-122.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-122.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-118.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q1&#45;&gt;q4 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M192.7,-144.11C204.73,-140.53 219.34,-136.18 232.3,-132.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"233.23,-135.16 240.53,-129.88 231.53,-129.46 233.23,-135.16\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-141.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q2 -->\n<g id=\"node6\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-193.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-193.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-189.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M191.94,-161.88C204.46,-167.27 219.92,-173.93 233.41,-179.74\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"232.54,-182.61 241.52,-183.24 234.89,-177.14 232.54,-182.61\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-177.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M191.94,-90.88C204.46,-96.27 219.92,-102.93 233.41,-108.74\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"232.54,-111.61 241.52,-112.24 234.89,-106.14 232.54,-111.61\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-106.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q6 -->\n<g id=\"node7\" class=\"node\">\n<title>q6</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-22.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-18.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q6</text>\n</g>\n<!-- q3&#45;&gt;q6 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q3&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M190.46,-67.05C204.92,-58.44 223.75,-47.24 238.91,-38.22\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"240.67,-40.64 246.46,-33.73 237.63,-35.52 240.67,-40.64\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-57.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q7 -->\n<g id=\"node8\" class=\"node\">\n<title>q7</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-51.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-47.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q7</text>\n</g>\n<!-- q4&#45;&gt;q7 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q4&#45;&gt;q7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M288.5,-107.09C303.65,-96.06 324.1,-81.16 340.03,-69.57\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"341.87,-71.91 346.99,-64.5 338.37,-67.1 341.87,-71.91\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-92.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q5 -->\n<g id=\"node9\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-151.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-151.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-147.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M292.19,-129.89C304.23,-133.47 318.84,-137.82 331.8,-141.67\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"331.03,-144.55 340.03,-144.12 332.73,-138.85 331.03,-144.55\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-141.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q2&#45;&gt;q2 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q2&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M257.42,-218.8C256.8,-229.32 259.78,-238.25 266.34,-238.25 270.95,-238.25 273.79,-233.84 274.86,-227.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"277.84,-227.43 275.26,-218.8 271.9,-227.16 277.84,-227.43\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-242.05\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q2&#45;&gt;q5 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q2&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M291.44,-183.12C303.95,-177.72 319.41,-171.07 332.91,-165.25\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"334.38,-167.86 341.01,-161.76 332.03,-162.39 334.38,-167.86\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-177.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q6&#45;&gt;q6 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q6&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M257.56,-43.88C256.48,-54.17 259.4,-63.25 266.34,-63.25 271.22,-63.25 274.11,-58.76 275.03,-52.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"278.01,-52.41 275.12,-43.88 272.06,-52.35 278.01,-52.41\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-67.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q6&#45;&gt;q7 -->\n<g id=\"edge13\" class=\"edge\">\n<title>q6&#45;&gt;q7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M288.5,-28.79C302.17,-32.86 320.15,-38.21 335.23,-42.69\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"334.72,-45.65 343.72,-45.22 336.42,-39.95 334.72,-45.65\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-42.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q7&#45;&gt;q7 -->\n<g id=\"edge14\" class=\"edge\">\n<title>q7&#45;&gt;q7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M357.06,-72.88C355.97,-83.17 358.9,-92.25 365.84,-92.25 370.71,-92.25 373.61,-87.76 374.52,-81.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"377.5,-81.42 374.62,-72.88 371.55,-81.35 377.5,-81.42\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-96.05\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q8 -->\n<g id=\"node10\" class=\"node\">\n<title>q8</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"465.33\" cy=\"-101.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"465.33\" cy=\"-101.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"465.33\" y=\"-97.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q8</text>\n</g>\n<!-- q7&#45;&gt;q8 -->\n<g id=\"edge15\" class=\"edge\">\n<title>q7&#45;&gt;q8</title>\n<path fill=\"none\" stroke=\"black\" d=\"M386.57,-61.62C400.02,-68.52 418.12,-77.8 433.47,-85.67\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"432.28,-88.4 441.2,-89.64 435,-83.11 432.28,-88.4\"/>\n<text text-anchor=\"middle\" x=\"415.58\" y=\"-82.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q5&#45;&gt;q5 -->\n<g id=\"edge18\" class=\"edge\">\n<title>q5&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M356.91,-176.8C356.3,-187.32 359.27,-196.25 365.84,-196.25 370.45,-196.25 373.29,-191.84 374.36,-185.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"377.34,-185.43 374.76,-176.8 371.4,-185.16 377.34,-185.43\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-200.05\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q5&#45;&gt;q8 -->\n<g id=\"edge17\" class=\"edge\">\n<title>q5&#45;&gt;q8</title>\n<path fill=\"none\" stroke=\"black\" d=\"M389.94,-139.65C402.84,-133.03 419.08,-124.7 433.1,-117.51\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"434.84,-119.96 441.05,-113.44 432.13,-114.67 434.84,-119.96\"/>\n<text text-anchor=\"middle\" x=\"415.58\" y=\"-131.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q8&#45;&gt;q8 -->\n<g id=\"edge16\" class=\"edge\">\n<title>q8&#45;&gt;q8</title>\n<path fill=\"none\" stroke=\"black\" d=\"M456.41,-126.8C455.8,-137.32 458.77,-146.25 465.33,-146.25 469.95,-146.25 472.79,-141.84 473.85,-135.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"476.83,-135.43 474.25,-126.8 470.89,-135.16 476.83,-135.43\"/>\n<text text-anchor=\"middle\" x=\"465.33\" y=\"-150.05\" font-family=\"Times-Roman\" font-size=\"14.00\">0,1</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3a096d510>>"
},
"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": [
"<div style=\"page-break-after: always;\"></div>\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
}