CS2333/Assignment 3.ipynb

563 lines
64 KiB
Plaintext
Raw Normal View History

2024-01-26 18:17:03 -04:00
{
"cells": [
2024-02-10 18:41:16 -04:00
{
"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<p
"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
"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=
"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"
},
2024-01-26 18:17:03 -04:00
{
"cell_type": "code",
2024-02-10 18:41:16 -04:00
"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.
"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()"
],
2024-01-26 18:17:03 -04:00
"metadata": {
2024-02-10 18:41:16 -04:00
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-10T22:40:20.722624Z",
"start_time": "2024-02-10T22:40:20.694623600Z"
}
2024-01-26 18:17:03 -04:00
},
2024-02-10 18:41:16 -04:00
"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-Rom
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7fb3826f8fc0>>"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
2024-01-26 18:17:03 -04:00
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
2024-02-10 18:41:16 -04:00
"q18 = DFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},\n",
2024-01-26 18:17:03 -04:00
"\tinput_symbols={'0', '1'},\n",
"\ttransitions={\n",
2024-02-10 18:41:16 -04:00
"\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",
2024-01-26 18:17:03 -04:00
"\t},\n",
"\tinitial_state='q0',\n",
2024-02-10 18:41:16 -04:00
"\tfinal_states={'q1', 'q2', 'q3', 'q4'}\n",
2024-01-26 18:17:03 -04:00
")\n",
2024-02-10 18:41:16 -04:00
"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
"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"
2024-01-26 18:17:03 -04:00
}
],
"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
}