CS2333/Assignment 4.ipynb

496 lines
55 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.367829600Z",
"start_time": "2024-02-17T00:51:01.209832800Z"
}
},
"outputs": [],
"source": [
"# Helpers and imports\n",
"from automata.fa.nfa import NFA\n",
"\n",
"debug = False\n",
"\n",
"def nfa_test(nfa: NFA, str_arr: list[str]):\n",
" for s in str_arr:\n",
" if s == \"\":\n",
" print(f'ε is accepted: {nfa.accepts_input(s)}')\n",
" else:\n",
" print(f'{s} is accepted: {nfa.accepts_input(s)}')"
]
},
{
"cell_type": "markdown",
"source": [
"# Q22\n",
"## (a)"
],
"metadata": {
"collapsed": false
},
"id": "2a3d28941b3eac7d"
},
{
"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=\"655pt\" height=\"94pt\"\n viewBox=\"0.00 0.00 654.82 94.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 90.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-90.25 650.82,-90.25 650.82,4 -4,4\"/>\n<!-- ef361926&#45;1987&#45;4988&#45;9833&#45;4dc84f84fd2e -->\n<g id=\"node1\" class=\"node\">\n<title>ef361926&#45;1987&#45;4988&#45;9833&#45;4dc84f84fd2e</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<!-- ef361926&#45;1987&#45;4988&#45;9833&#45;4dc84f84fd2e&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>ef361926&#45;1987&#45;4988&#45;9833&#45;4dc84f84fd2e&#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<!-- 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=\"edge2\" 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\">0</text>\n</g>\n<!-- q2 -->\n<g id=\"node4\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"246.34\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"246.34\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M177.97,-30.5C189.02,-30.5 202.58,-30.5 214.62,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"214.97,-33.48 223.47,-30.5 214.97,-27.53 214.97,-33.48\"/>\n<text text-anchor=\"middle\" x=\"200.59\" y=\"-34.3\" 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=\"337.84\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"337.84\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q2&#45;&gt;q3 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q2&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M269.46,-30.5C280.52,-30.5 294.07,-30.5 306.12,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"306.47,-33.48 314.97,-30.5 306.47,-27.53 306.47,-33.48\"/>\n<text text-anchor=\"middle\" x=\"292.09\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M329.79,-51.88C328.79,-62.17 331.48,-71.25 337.84,-71.25 342.31,-71.25 344.96,-66.76 345.8,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"348.78,-60.41 345.88,-51.88 342.83,-60.36 348.78,-60.41\"/>\n<text text-anchor=\"middle\" x=\"337.84\" y=\"-75.05\" font-family=\"Times-Roman\" font-size=\"14.00\">0,1</text>\n</g>\n<!-- q4 -->\n<g id=\"node6\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"429.33\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"429.33\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</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=\"M360.96,-30.5C372.01,-30.5 385.57,-30.5 397.61,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"397.96,-33.48 406.46,-30.5 397.96,-27.53 397.96,-33.48\"/>\n<text text-anchor=\"middle\" x=\"383.58\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q5 -->\n<g id=\"node7\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"520.83\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"520.83\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q5</text>\n</g>\n<!-- q4&#45;&gt;q5 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q4&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M452.45,-30.5C463.51,-30.5 477.06,-30.5 489.11,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"489.46,-33.48 497.96,-30.5 489.46,-27.53 489.46,-33.48\"/>\n<text text-anchor=\"middle\" x=\"475.08\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q6 -->\n<g id=\"node8\" class=\"node\">\n<title>q6</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"616.32\" cy=\"-30.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"616.32\" cy=\"-30.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"616.32\" y=\"-26.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q6</text>\n</g>\n<!-- q5&#45;&gt;q6 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q5&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M543.99,-30.5C554.94,-30.5 568.4,-30.5 580.67,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"580.85,-33.48 589.35,-30.5 580.85,-27.53 580.85,-33.48\"/>\n<text text-anchor=\"middle\" x=\"566.58\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7f462467f9c0>>"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q22a = NFA(\n",
" states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6'},\n",
" input_symbols={'0', '1'},\n",
" transitions={\n",
" 'q0': {'0': {'q1'}},\n",
" 'q1': {'1': {'q2'}},\n",
" 'q2': {'1': {'q3'}},\n",
" 'q3': {'0': {'q3', 'q4'}, '1': {'q3'}},\n",
" 'q4': {'0': {'q5'}},\n",
" 'q5': {'0': {'q6'}}\n",
" },\n",
" initial_state='q0',\n",
" final_states={'q6'}\n",
")\n",
"# Should be true\n",
"t = [\n",
" \"011000\",\n",
" \"0110000\",\n",
" \"011010000\",\n",
" \"011011010000\"\n",
"]\n",
"# Should be false\n",
"f = [\n",
" \"\",\n",
" \"0\",\n",
" \"1\",\n",
" \"0110\",\n",
" \"0110001\",\n",
" \"011010001\",\n",
" \"011011010001\"\n",
"]\n",
"if debug:\n",
" nfa_test(q22a, t)\n",
" print('-'*20)\n",
" nfa_test(q22a, f)\n",
"q22a.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.419829300Z",
"start_time": "2024-02-17T00:51:01.419829300Z"
}
},
"id": "8ec96b76671bd51f",
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"## (b)"
],
"metadata": {
"collapsed": false
},
"id": "4c318b8332558fe6"
},
{
"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=\"289pt\" height=\"278pt\"\n viewBox=\"0.00 0.00 288.84 278.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 274.25)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-274.25 284.84,-274.25 284.84,4 -4,4\"/>\n<!-- b36964c3&#45;7cac&#45;44c2&#45;80ec&#45;5a57927f24cb -->\n<g id=\"node1\" class=\"node\">\n<title>b36964c3&#45;7cac&#45;44c2&#45;80ec&#45;5a57927f24cb</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-118.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=\"-118.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-114.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- b36964c3&#45;7cac&#45;44c2&#45;80ec&#45;5a57927f24cb&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>b36964c3&#45;7cac&#45;44c2&#45;80ec&#45;5a57927f24cb&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-118.5C7.36,-118.5 19.4,-118.5 31.65,-118.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-121.48 40.48,-118.5 31.98,-115.53 31.98,-121.48\"/>\n</a>\n</g>\n</g>\n<!-- q3 -->\n<g id=\"node3\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"154.84\" cy=\"-214.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-210.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q0&#45;&gt;q3 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M79.53,-134.78C94.06,-150.37 116.11,-174.03 132.43,-191.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"130.62,-193.94 138.59,-198.13 134.97,-189.89 130.62,-193.94\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-172.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q1 -->\n<g id=\"node4\" class=\"node\">\n<title>q1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"154.84\" cy=\"-118.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-114.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,-118.5C97.53,-118.5 111.08,-118.5 123.13,-118.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"123.48,-121.48 131.98,-118.5 123.48,-115.53 123.48,-121.48\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-122.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=\"154.84\" cy=\"-22.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-18.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q0&#45;&gt;q2 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q0&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M79.53,-102.21C94.06,-86.63 116.11,-62.97 132.43,-45.47\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"134.97,-47.11 138.59,-38.86 130.62,-43.05 134.97,-47.11\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-76.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q3&#45;&gt;q3 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q3&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M146.8,-235.88C145.8,-246.17 148.48,-255.25 154.84,-255.25 159.32,-255.25 161.97,-250.76 162.81,-244.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"165.79,-244.41 162.89,-235.88 159.84,-244.35 165.79,-244.41\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-259.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c</text>\n</g>\n<!-- q4 -->\n<g id=\"node6\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"250.34\" cy=\"-118.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"250.34\" cy=\"-118.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"250.34\" y=\"-114.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</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=\"M171.28,-198.65C185.88,-183.66 208.01,-160.93 224.99,-143.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"227.19,-145.51 230.99,-137.34 222.93,-141.36 227.19,-145.51\"/>\n<text text-anchor=\"middle\" x=\"200.59\" y=\"-175.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q1&#45;&gt;q1 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q1&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M146.8,-139.88C145.8,-150.17 148.48,-159.25 154.84,-159.25 159.32,-159.25 161.97,-154.76 162.81,-148.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"165.79,-148.41 162.89,-139.88 159.84,-148.35 165.79,-148.41\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-163.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c</text>\n</g>\n<!-- q1&#45;&gt;q4 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q1&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M178,-118.5C188.95,-118.5 202.42,-118.5 214.69,-118.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"214.86,-121.48 223.36,-118.5 214.86,-115.53 214.86,-121.48\"/>\n<text text-anchor=\"middle\" x=\"200.59\" y=\"-122.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q2&#45;&gt;q2 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q2&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M146.8,-43.88C145.8,-54.17 148.48,-63.25 154.84,-63.25 159.32,-63.25 161.97,-58.76 162.81,-52.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"165.79,-52.41 162.89,-43.88 159.84,-52.36 165.79,-52.41\"/>\n<text text-anchor=\"middle\" x=\"154.84\" y=\"-67.05\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c</text>\n</g>\n<!-- q2&#45;&gt;q4 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q2&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M171.28,-38.35C185.88,-53.34 208.01,-76.07 224.99,-93.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"222.93,-95.64 230.99,-99.66 227.19,-91.49 222.93,-95.64\"/>\n<text text-anchor=\"middle\" x=\"200.59\" y=\"-76.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7f462469b300>>"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q22b = NFA(\n",
" states={'q0', 'q1', 'q2', 'q3', 'q4'},\n",
" input_symbols={'a', 'b', 'c'},\n",
" transitions={\n",
" 'q0': {'a': {'q1'}, 'b': {'q2'}, 'c': {'q3'}},\n",
" 'q1': {'a': {'q1', 'q4'}, 'b': {'q1'}, 'c': {'q1'}},\n",
" 'q2': {'a': {'q2'}, 'b': {'q2', 'q4'}, 'c': {'q2'}},\n",
" 'q3': {'a': {'q3'}, 'b': {'q3'}, 'c': {'q3', 'q4'}},\n",
" 'q4': {}\n",
" },\n",
" initial_state='q0',\n",
" final_states={'q4'}\n",
")\n",
"\n",
"# Should be true\n",
"t = [\n",
" \"aa\",\n",
" \"bb\",\n",
" \"cc\",\n",
" \"aba\",\n",
" \"bab\",\n",
" \"cac\",\n",
" \"aabca\",\n",
" \"bcbab\",\n",
" \"ccccc\",\n",
"]\n",
"f = [\n",
" \"\",\n",
" \"a\",\n",
" \"b\",\n",
" \"c\",\n",
" \"abc\",\n",
" \"ab\",\n",
" \"bc\",\n",
" \"ca\",\n",
" \"ac\",\n",
" \"ba\",\n",
" \"abbbbbc\",\n",
" \"aaaaaaaaaaaabbbbbbbbbbbbcccccccccccccccc\",\n",
" \"abcabac\",\n",
" \"bbbbbbbbbbba\",\n",
"]\n",
"if debug:\n",
" nfa_test(q22b, t)\n",
" print('-'*20)\n",
" nfa_test(q22b, f)\n",
"q22b.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.425828900Z",
"start_time": "2024-02-17T00:51:01.419829300Z"
}
},
"id": "b36d3c2c58fe93b7",
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "5a3996c26ce8cfbc"
},
{
"cell_type": "markdown",
"source": [
"# Q23"
],
"metadata": {
"collapsed": false
},
"id": "9551c6ce7a0990"
},
{
"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=\"404pt\" height=\"214pt\"\n viewBox=\"0.00 0.00 404.34 213.68\" 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 209.68)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-209.68 400.34,-209.68 400.34,4 -4,4\"/>\n<!-- c6536698&#45;09e6&#45;4b0c&#45;b9c0&#45;932845697f5e -->\n<g id=\"node1\" class=\"node\">\n<title>c6536698&#45;09e6&#45;4b0c&#45;b9c0&#45;932845697f5e</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-88.19\" 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=\"-88.19\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"63.35\" y=\"-84.49\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- c6536698&#45;09e6&#45;4b0c&#45;b9c0&#45;932845697f5e&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>c6536698&#45;09e6&#45;4b0c&#45;b9c0&#45;932845697f5e&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.83,-88.19C7.36,-88.19 19.4,-88.19 31.65,-88.19\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.98,-91.16 40.48,-88.19 31.98,-85.21 31.98,-91.16\"/>\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=\"152.84\" cy=\"-120.18\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"152.84\" y=\"-116.48\" 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=\"M85.08,-95.77C96.42,-99.91 110.63,-105.11 123.02,-109.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"122.26,-112.53 131.26,-112.65 124.3,-106.94 122.26,-112.53\"/>\n<text text-anchor=\"middle\" x=\"108.1\" y=\"-107.98\" font-family=\"Times-Roman\" font-size=\"14.00\">ε</text>\n</g>\n<!-- q5 -->\n<g id=\"node4\" class=\"node\">\n<title>q5</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"152.84\" cy=\"-57.19\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"152.84\" y=\"-53.49\" 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.08,-80.84C96.42,-76.82 110.63,-71.79 123.02,-67.4\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"124.24,-70.12 131.26,-64.48 122.25,-64.51 124.24,-70.12\"/>\n<text text-anchor=\"middle\" x=\"108.1\" y=\"-76.99\" font-family=\"Times-Roman\" font-size=\"14.00\">ε</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=\"M144.8,-141.57C143.8,-151.85 146.48,-160.93 152.84,-160.93 157.32,-160.93 159.97,-156.44 160.81,-150.2\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"163.79,-150.1 160.89,-141.57 157.84,-150.05 163.79,-150.1\"/>\n<text text-anchor=\"middle\" x=\"152.84\" y=\"-164.73\" font-family=\"Times-Roman\" font-size=\"14.00\">0,1</text>\n</g>\n<!-- q2 -->\n<g id=\"node5\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257.34\" cy=\"-183.18\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"257.34\" y=\"-179.48\" 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=\"M172.64,-131.74C188.83,-141.68 212.34,-156.14 230.33,-167.19\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"228.86,-169.78 237.66,-171.7 231.98,-164.72 228.86,-169.78\"/>\n<text text-anchor=\"middle\" x=\"205.09\" y=\"-160.98\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q3 -->\n<g id=\"node6\" class=\"node\">\n<title>q3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257.34\" cy=\"-120.18\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"257.34\" y=\"-116.48\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q1&#45;&gt;q3 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q1&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M175.83,-120.18C190.42,-120.18 209.77,-120.18 225.85,-120.18\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.89,-123.16 234.39,-120.18 225.89,-117.21 225.89,-123.16\"/>\n<text text-anchor=\"middle\" x=\"205.09\" y=\"-123.98\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q6 -->\n<g id=\"node7\" class=\"node\">\n<title>q6</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"257.34\" cy=\"-57.19\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"257.34\" y=\"-53.49\" font-family=\"Times-Roman\" font-size=\"14.00\">q6</text>\n</g>\n<!-- q5&#45;&gt;q6 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q5&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M175.83,-57.19C190.42,-57.19 209.77,-57.19 225.85,-57.19\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"225.89,-60.16 234.39,-57.19 225.89,-54.21 225.89,-60.16\"/>\n<text text-anchor=\"middle\" x=\"205.09\" y=\"-60.99\" font-family=\"Times-Roman\" font-size=\"14.00\">0,1</text>\n</g>\n<!-- q4 -->\n<g id=\"node8\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-140.18\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-140.18\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-136.48\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q2&#45;&gt;q4 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q2&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M278.87,-174.89C294.04,-168.77 314.99,-160.31 332.41,-153.27\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"333.87,-155.89 340.64,-149.95 331.64,-150.38 333.87,-155.89\"/>\n<text text-anchor=\"middle\" x=\"309.59\" y=\"-169.98\" font-family=\"Times-Roman\" font-size=\"14.00\">0</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M279.89,-124.23C294.49,-126.97 314.08,-130.65 330.77,-133.78\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"330.29,-136.72 339.19,-135.37 331.39,-130.87 330.29,-136.72\"/>\n<text text-anchor=\"middle\" x=\"309.59\" y=\"-134.98\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q7 -->\n<g id=\"node9\" class=\"node\">\n<title>q7</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-45.19\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"365.84\" cy=\"-45.19\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"365.84\" y=\"-41.49\" font-family=\"Times-Roman\" font-size=\"14.00\">q7</text>\n</g>\n<!-- q6&#45;&gt;q7 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q6&#45;&gt;q7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M280.15,-54.73C294.52,-53.11 313.66,-50.95 330.11,-49.1\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"330.81,-52.02 338.92,-48.11 330.14,-46.1 330.81,-52.02\"/>\n<text text-anchor=\"middle\" x=\"309.59\" y=\"-55.99\" font-family=\"Times-Roman\" font-size=\"14.00\">0,1</text>\n</g>\n<!-- q7&#45;&gt;q5 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q7&#45;&gt;q5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M342.24,-31.74C316.27,-18 272.34,0 234.59,-10.19 214.14,-15.71 193.53,-27.83 178.26,-38.38\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"176.41,-36.05 171.21,-43.4 179.86,-40.89 176.41,-36.05\"/>\n<text text-anchor=\"middle\" x=\"257.34\" y=\"-13.99\" 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 0x7f461c387e10>>"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q23 = NFA(\n",
" states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7'},\n",
" input_symbols={'0', '1'},\n",
" transitions={\n",
" 'q0': {'': {'q1', 'q5'}},\n",
" 'q1': {'0': {'q1', 'q2'}, '1': {'q1', 'q3'}},\n",
" 'q2': {'0': {'q4'}},\n",
" 'q3': {'1': {'q4'}},\n",
" 'q5': {'0': {'q6'}, '1': {'q6'}},\n",
" 'q6': {'0': {'q7'}, '1': {'q7'}},\n",
" 'q7': {'0': {'q5'}, '1': {'q5'}}\n",
" },\n",
" initial_state='q0',\n",
" final_states={'q4', 'q7'}\n",
")\n",
"t = [\n",
" '00',\n",
" '11',\n",
" '01',\n",
" '00000',\n",
" '11111',\n",
" '01010',\n",
" '01111100',\n",
" '10010111',\n",
" '011100',\n",
" '10010',\n",
" '10001011',\n",
"]\n",
"f = [\n",
" '',\n",
" '0',\n",
" '1',\n",
" '101',\n",
" '010',\n",
" '0001',\n",
" '1110',\n",
" '0101',\n",
" '0111110',\n",
"]\n",
"if debug:\n",
" nfa_test(q23, t)\n",
" print('-'*20)\n",
" nfa_test(q23, f)\n",
"q23.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.456829Z",
"start_time": "2024-02-17T00:51:01.428830800Z"
}
},
"id": "7055f3d7e013c8bf",
"execution_count": 4
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "35ed9908c8809b00"
},
{
"cell_type": "markdown",
"source": [
"# Q24\n",
"## (a) \n",
"The language for this graph is:\n",
"$$L = \\{ w \\in \\{ 0, 1, 2 \\}^* \\ | \\text{ The fourth last symbol in } w \\text{ is } 0 \\text { and the second last symbol in } w \\text{ is } 2 \\}$$\n",
"\n",
"## (b)\n",
"The language for this graph is:\n",
"$$L = \\{ w \\in \\{ a, b, c \\}^* \\ | \\ w \\text{ contains the substring } ab \\text { or } w \\text{ contains the substring } ac \\}$$"
],
"metadata": {
"collapsed": false
},
"id": "d359a31d044ce735"
},
{
"cell_type": "markdown",
"source": [
"$\\pagebreak$"
],
"metadata": {
"collapsed": false
},
"id": "144bcb36426add4a"
},
{
"cell_type": "markdown",
"source": [
"# Q26\n",
"## (a)"
],
"metadata": {
"collapsed": false
},
"id": "233aa8019cdbe103"
},
{
"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=\"530pt\" height=\"290pt\"\n viewBox=\"0.00 0.00 530.48 290.00\" 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 286)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-286 526.48,-286 526.48,4 -4,4\"/>\n<!-- 395dc4d0&#45;bf07&#45;4c32&#45;b931&#45;cf10ac1006e5 -->\n<g id=\"node1\" class=\"node\">\n<title>395dc4d0&#45;bf07&#45;4c32&#45;b931&#45;cf10ac1006e5</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-140.5\" rx=\"1.8\" ry=\"1.8\"/>\n</a>\n</g>\n</g>\n<!-- q00 -->\n<g id=\"node2\" class=\"node\">\n<title>q00</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"69.2\" cy=\"-140.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"69.2\" y=\"-136.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q00</text>\n</g>\n<!-- 395dc4d0&#45;bf07&#45;4c32&#45;b931&#45;cf10ac1006e5&#45;&gt;q00 -->\n<g id=\"edge1\" class=\"edge\">\n<title>395dc4d0&#45;bf07&#45;4c32&#45;b931&#45;cf10ac1006e5&#45;&gt;q00</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q00\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.94,-140.5C7.55,-140.5 19.26,-140.5 31.72,-140.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.87,-143.48 40.37,-140.5 31.87,-137.53 31.87,-143.48\"/>\n</a>\n</g>\n</g>\n<!-- q00&#45;&gt;q00 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q00&#45;&gt;q00</title>\n<path fill=\"none\" stroke=\"black\" d=\"M60.61,-167.78C60.22,-178.33 63.09,-187.09 69.2,-187.09 73.49,-187.09 76.18,-182.76 77.27,-176.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"80.26,-176.44 77.79,-167.78 74.32,-176.09 80.26,-176.44\"/>\n<text text-anchor=\"middle\" x=\"69.2\" y=\"-190.89\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c,d</text>\n</g>\n<!-- q01 -->\n<g id=\"node3\" class=\"node\">\n<title>q01</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"172.39\" cy=\"-253.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"172.39\" y=\"-249.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q01</text>\n</g>\n<!-- q00&#45;&gt;q01 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q00&#45;&gt;q01</title>\n<path fill=\"none\" stroke=\"black\" d=\"M88.99,-161.49C105.32,-179.72 129.09,-206.26 146.9,-226.15\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"144.81,-228.28 152.7,-232.62 149.24,-224.31 144.81,-228.28\"/>\n<text text-anchor=\"middle\" x=\"120.79\" y=\"-203.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q02 -->\n<g id=\"node4\" class=\"node\">\n<title>q02</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"172.39\" cy=\"-178.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"172.39\" y=\"-174.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q02</text>\n</g>\n<!-- q00&#45;&gt;q02 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q00&#45;&gt;q02</title>\n<path fill=\"none\" stroke=\"black\" d=\"M96.26,-150.28C108.77,-154.98 123.92,-160.67 137.33,-165.71\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"136.42,-168.55 145.42,-168.74 138.5,-162.98 136.42,-168.55\"/>\n<text text-anchor=\"middle\" x=\"120.79\" y=\"-164.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q03 -->\n<g id=\"node5\" class=\"node\">\n<title>q03</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"172.39\" cy=\"-103.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"172.39\" y=\"-99.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q03</text>\n</g>\n<!-- q00&#45;&gt;q03 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q00&#45;&gt;q03</title>\n<path fill=\"none\" stroke=\"black\" d=\"M96.26,-130.97C108.67,-126.43 123.69,-120.94 137.02,-116.06\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"138.11,-118.83 145.07,-113.12 136.07,-113.24 138.11,-118.83\"/>\n<text text-anchor=\"middle\" x=\"120.79\" y=\"-126.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q04 -->\n<g id=\"node6\" class=\"node\">\n<title>q04</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"172.39\" cy=\"-28.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"172.39\" y=\"-24.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q04</text>\n</g>\n<!-- q00&#45;&gt;q04 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q00&#45;&gt;q04</title>\n<path fill=\"none\" stroke=\"black\" d=\"M88.99,-119.69C105.22,-101.73 128.79,-75.65 146.57,-55.98\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"148.86,-57.88 152.35,-49.57 144.45,-53.89 148.86,-57.88\"/>\n<text text-anchor=\"middle\" x=\"120.79\" y=\"-91.3\" font-family=\"Times-Roman\" font-size=\"14.00\">d</text>\n</g>\n<!-- q05 -->\n<g id=\"node7\" class=\"node\">\n<title>q05</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"275.59\" cy=\"-253.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"275.59\" y=\"-249.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q05</text>\n</g>\n<!-- q01&#45;&gt;q05 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q01&#45;&gt;q05</title>\n<path fill=\"none\" stroke=\"black\" d=\"M201.04,-253.5C212.53,-253.5 226.03,-253.5 238.3,-253.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"238.47,-256.48 246.97,-253.5 238.47,-250.53 238.47,-256.48\"/>\n<text text-anchor=\"middle\" x=\"223.99\" y=\"-257.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q06 -->\n<g id=\"node8\" class=\"node\">\n<title>q06</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"275.59\" cy=\"-178.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"275.59\" y=\"-174.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q06</text>\n</g>\n<!-- q02&#45;&gt;q06 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q02&#45;&gt;q06</title>\n<path fill=\"none\" stroke=\"black\" d=\"M201.04,-178.5C212.53,-178.5 226.03,-178.5 238.3,-178.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"238.47,-181.48 246.97,-178.5 238.47,-175.53 238.47,-181.48\"/>\n<text text-anchor=\"middle\" x=\"223.99\" y=\"-182.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q07 -->\n<g id=\"node9\" class=\"node\">\n<title>q07</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"275.59\" cy=\"-103.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"275.59\" y=\"-99.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q07</text>\n</g>\n<!-- q03&#45;&gt;q07 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q03&#45;&gt;q07</title>\n<path fill=\"none\" stroke=\"black\" d=\"M201.04,-103.5C212.53,-103.5 226.03,-103.5 238.3,-103.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"238.47,-106.48 246.97,-103.5 238.47,-100.53 238.47,-106.48\"/>\n<text text-anchor=\"middle\" x=\"223.99\" y=\"-107.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q08 -->\n<g id=\"node10\" class=\"node\">\n<title>q08</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"275.59\" cy=\"-28.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"275.59\" y=\"-24.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q08</text>\n</g>\n<!-- q04&#45;&gt;q08 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q04&#45;&gt;q08</title>\n<path fill=\"none\" stroke=\"black\" d=\"M201.04,-28.5C212.53,-28.5 226.03,-28.5 238.3,-28.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"238.47,-31.48 246.97,-28.5 238.47,-25.53 238.47,-31.48\"/>\n<text text-anchor=\"middle\" x=\"223.99\" y=\"-32.3\" font-family=\"Times-Roman\" font-size=\"14.00\">d</text>\n</g>\n<!-- q09 -->\n<g id=\"node11\" class=\"node\">\n<title>q09</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"378.78\" cy=\"-253.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"378.78\" y=\"-249.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q09</text>\n</g>\n<!-- q05&#45;&gt;q09 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q05&#45;&gt;q09</title>\n<path fill=\"none\" stroke=\"black\" d=\"M304.24,-253.5C315.73,-253.5 329.22,-253.5 341.49,-253.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"341.66,-256.48 350.16,-253.5 341.66,-250.53 341.66,-256.48\"/>\n<text text-anchor=\"middle\" x=\"327.18\" y=\"-257.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q10 -->\n<g id=\"node12\" class=\"node\">\n<title>q10</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"378.78\" cy=\"-178.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"378.78\" y=\"-174.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q10</text>\n</g>\n<!-- q06&#45;&gt;q10 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q06&#45;&gt;q10</title>\n<path fill=\"none\" stroke=\"black\" d=\"M304.24,-178.5C315.73,-178.5 329.22,-178.5 341.49,-178.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"341.66,-181.48 350.16,-178.5 341.66,-175.53 341.66,-181.48\"/>\n<text text-anchor=\"middle\" x=\"327.18\" y=\"-182.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q11 -->\n<g id=\"node13\" class=\"node\">\n<title>q11</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"378.78\" cy=\"-103.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"378.78\" y=\"-99.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q11</text>\n</g>\n<!-- q07&#45;&gt;q11 -->\n<g id=\"edge13\" class=\"edge\">\n<title>q07&#45;&gt;q11</title>\n<path fill=\"none\" stroke=\"black\" d=\"M304.24,-103.5C315.73,-103.5 329.22,-103.5 341.49,-103.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"341.66,-106.48 350.16,-103.5 341.66,-100.53 341.66,-106.48\"/>\n<text text-anchor=\"middle\" x=\"327.18\" y=\"-107.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q12 -->\n<g id=\"node14\" class=\"node\">\n<title>q12</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"378.78\" cy=\"-28.5\" rx=\"28.5\" ry=\"28.5\"/>\n<text text-anchor=\"middle\" x=\"378.78\" y=\"-24.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q12</text>\n</g>\n<!-- q08&#45;&gt;q12 -->\n<g id=\"edge14\" class=\"edge\">\n<title>q08&#45;&gt;q12</title>\n<path fill=\"none\" stroke=\"black\" d=\"M304.24,-28.5C315.73,-28.5 329.22,-28.5 341.49,-28.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"341.66,-31.48 350.16,-28.5 341.66,-25.53 341.66,-31.48\"/>\n<text text-anchor=\"middle\" x=\"327.18\" y=\"-32.3\" font-family=\"Times-Roman\" font-size=\"14.00\">d</text>\n</g>\n<!-- q13 -->\n<g id=\"node15\" class=\"node\">\n<title>q13</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"485.98\" cy=\"-140.5\" rx=\"32.5\" ry=\"32.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"485.98\" cy=\"-140.5\" rx=\"36.5\" ry=\"36.5\"/>\n<text text-anchor=\"middle\" x=\"485.98\" y=\"-136.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q13</text>\n</g>\n<!-- q09&#45;&gt;q13 -->\n<g id=\"edge15\" class=\"edge\">\n<title>q09&#45;&gt;q13</title>\n<path fill=\"none\" stroke=\"black\" d=\"M398.83,-233.03C415.11,-215.55 438.74,-190.16 457.14,-170.4\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"459.55,-172.18 463.16,-163.93 455.19,-168.13 459.55,-172.18\"/>\n<text text-anchor=\"middle\" x=\"430.38\" y=\"-207.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q10&#45;&gt;q13 -->\n<g id=\"edge17\" class=\"edge\">\n<title>q10&#45;&gt;q13</title>\n<path fill=\"none\" stroke=\"black\" d=\"M406.06,-169.01C418.35,-164.56 433.24,-159.19 446.75,-154.31\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"447.97,-157.03 454.95,-151.34 445.94,-151.44 447.97,-157.03\"/>\n<text text-anchor=\"middle\" x=\"430.38\" y=\"-165.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q11&#45;&gt;q13 -->\n<g id=\"edge18\" class=\"edge\">\n<title>q11&#45;&gt;q13</title>\n<path fill=\"none\" stroke=\"black\" d=\"M406.06,-112.74C418.35,-117.06 433.24,-122.3 446.75,-127.05\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"445.94,-129.92 454.95,-129.94 447.92,-124.31 445.94,-129.92\"/>\n<text text-anchor=\"middle\" x=\"430.38\" y=\"-125.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q12&#45;&gt;q13 -->\n<g id=\"edge19\" class=\"edge\">\n<title>q12&#45;&gt;q13</title>\n<path fill=\"none\" stroke=\"black\" d=\"M399.08,-49.04C415.28,-66.29 438.64,-91.16 456.91,-110.61\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"454.9,-112.82 462.89,-116.98 459.24,-108.75 454.9,-112.82\"/>\n<text text-anchor=\"middle\" x=\"430.38\" y=\"-88.3\" font-family=\"Times-Roman\" font-size=\"14.00\">d</text>\n</g>\n<!-- q13&#45;&gt;q13 -->\n<g id=\"edge16\" class=\"edge\">\n<title>q13&#45;&gt;q13</title>\n<path fill=\"none\" stroke=\"black\" d=\"M475.85,-171.63C475.75,-182.4 479.12,-191.09 485.98,-191.09 490.8,-191.09 493.9,-186.79 495.27,-180.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"498.27,-180.37 496.1,-171.63 492.35,-179.82 498.27,-180.37\"/>\n<text text-anchor=\"middle\" x=\"485.98\" y=\"-194.89\" font-family=\"Times-Roman\" font-size=\"14.00\">a,b,c,d</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7f461c3a7a20>>"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q26a = NFA(\n",
"\tstates={'q00', 'q01', 'q02', 'q03', 'q04', 'q05', 'q06', 'q07', 'q08', 'q09', 'q10', 'q11', 'q12', 'q13'},\n",
" input_symbols={'a', 'b', 'c', 'd'},\n",
" transitions={\n",
" 'q00': {'a': {'q00', 'q01'}, 'b': {'q00', 'q02'}, 'c': {'q00', 'q03'}, 'd': {'q00', 'q04'}},\n",
" 'q01': {'a': {'q05'}},\n",
" 'q02': {'b': {'q06'}},\n",
" 'q03': {'c': {'q07'}},\n",
" 'q04': {'d': {'q08'}},\n",
"\t 'q05': {'a': {'q09'}},\n",
" 'q06': {'b': {'q10'}},\n",
" 'q07': {'c': {'q11'}},\n",
" 'q08': {'d': {'q12'}},\n",
" 'q09': {'a': {'q13'}},\n",
" 'q10': {'b': {'q13'}},\n",
" 'q11': {'c': {'q13'}},\n",
" 'q12': {'d': {'q13'}},\n",
" 'q13': {'a': {'q13'}, 'b': {'q13'}, 'c': {'q13'}, 'd': {'q13'}},\n",
" },\n",
" initial_state='q00',\n",
" final_states={'q13'},\n",
")\n",
"t = [\n",
" 'aaaa',\n",
" 'bbbb',\n",
" 'cccc',\n",
" 'dddd',\n",
" 'abbbba',\n",
" 'acccca',\n",
" 'adddda',\n",
" 'abbbbabbbba',\n",
" 'accccacccca',\n",
" 'addddadddda',\n",
" 'aaabbbcccdddd',\n",
"]\n",
"f = [\n",
" 'abcd',\n",
" 'abdc',\n",
" 'acbd',\n",
" 'acdb',\n",
" 'aaabb',\n",
" 'abbbc',\n",
" 'abbbd',\n",
"]\n",
"if debug:\n",
" nfa_test(q26a, t)\n",
" print('-'*20)\n",
" nfa_test(q26a, f)\n",
"q26a.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.510831200Z",
"start_time": "2024-02-17T00:51:01.457831400Z"
}
},
"id": "ba55249fa215010e",
"execution_count": 5
},
{
"cell_type": "markdown",
"source": [
"## (b)"
],
"metadata": {
"collapsed": false
},
"id": "c55b1fd818f8da4e"
},
{
"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=\"494pt\" height=\"226pt\"\n viewBox=\"0.00 0.00 493.83 226.00\" 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 222)\">\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-222 489.83,-222 489.83,4 -4,4\"/>\n<!-- 9a93236f&#45;ac2b&#45;4384&#45;8c2a&#45;d9686587f8ad -->\n<g id=\"node1\" class=\"node\">\n<title>9a93236f&#45;ac2b&#45;4384&#45;8c2a&#45;d9686587f8ad</title>\n<g id=\"a_node1\"><a xlink:title=\".\">\n<ellipse fill=\"black\" stroke=\"black\" cx=\"1.8\" cy=\"-110.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=\"-110.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"67.35\" cy=\"-110.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"67.35\" y=\"-106.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q0</text>\n</g>\n<!-- 9a93236f&#45;ac2b&#45;4384&#45;8c2a&#45;d9686587f8ad&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>9a93236f&#45;ac2b&#45;4384&#45;8c2a&#45;d9686587f8ad&#45;&gt;q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"&#45;&gt;q0\">\n<path fill=\"none\" stroke=\"black\" d=\"M3.9,-110.5C7.49,-110.5 19.32,-110.5 31.74,-110.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"31.83,-113.48 40.33,-110.5 31.83,-107.53 31.83,-113.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=\"-161.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"166.84\" y=\"-157.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=\"M91.45,-122.58C105.52,-129.94 123.56,-139.38 138.36,-147.12\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"137.28,-149.91 146.19,-151.22 140.04,-144.64 137.28,-149.91\"/>\n<text text-anchor=\"middle\" x=\"117.1\" y=\"-141.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=\"166.84\" cy=\"-60.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"166.84\" cy=\"-60.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"166.84\" y=\"-56.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=\"M91.45,-98.65C104.35,-92.04 120.6,-83.7 134.61,-76.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"136.36,-78.96 142.57,-72.44 133.65,-73.67 136.36,-78.96\"/>\n<text text-anchor=\"middle\" x=\"117.1\" y=\"-90.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q2 -->\n<g id=\"node5\" class=\"node\">\n<title>q2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-195.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-191.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q2</text>\n</g>\n<!-- q1&#45;&gt;q2 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M188.72,-169.27C195.95,-171.92 204.11,-174.88 211.59,-177.5 219.66,-180.32 228.43,-183.3 236.51,-186.01\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"235.62,-188.85 244.62,-188.71 237.49,-183.2 235.62,-188.85\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-184.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=\"266.34\" cy=\"-124.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"266.34\" cy=\"-124.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-120.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q3</text>\n</g>\n<!-- q1&#45;&gt;q3 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M187.59,-151.2C195,-147.56 203.58,-143.59 211.59,-140.5 218.04,-138.01 225.05,-135.67 231.81,-133.59\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"232.88,-136.38 240.18,-131.1 231.18,-130.67 232.88,-136.38\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-144.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=\"M157.92,-85.8C157.31,-96.33 160.28,-105.25 166.84,-105.25 171.46,-105.25 174.3,-100.84 175.36,-94.56\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"178.34,-94.43 175.76,-85.8 172.4,-94.16 178.34,-94.43\"/>\n<text text-anchor=\"middle\" x=\"166.84\" y=\"-109.05\" font-family=\"Times-Roman\" font-size=\"14.00\">b</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=\"-57.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"266.34\" y=\"-53.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q6</text>\n</g>\n<!-- q5&#45;&gt;q6 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q5&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M193.72,-59.7C206.27,-59.32 221.45,-58.85 234.6,-58.45\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"234.92,-61.41 243.32,-58.18 234.73,-55.47 234.92,-61.41\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-62.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q2&#45;&gt;q1 -->\n<g id=\"edge8\" class=\"edge\">\n<title>q2&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M249.76,-179.92C241.98,-173.21 232.02,-166.1 221.59,-162.5 214.22,-159.95 205.94,-158.96 198.12,-158.78\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"198.03,-155.81 189.57,-158.89 198.11,-161.76 198.03,-155.81\"/>\n<text text-anchor=\"middle\" x=\"216.59\" y=\"-166.3\" font-family=\"Times-Roman\" font-size=\"14.00\">a</text>\n</g>\n<!-- q4 -->\n<g id=\"node8\" class=\"node\">\n<title>q4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"361.84\" cy=\"-147.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"361.84\" y=\"-143.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q4</text>\n</g>\n<!-- q3&#45;&gt;q4 -->\n<g id=\"edge9\" class=\"edge\">\n<title>q3&#45;&gt;q4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M292.78,-130.94C298.78,-132.45 305.16,-134.04 311.09,-135.5 317.49,-137.08 324.37,-138.75 330.9,-140.33\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"330.56,-143.31 339.52,-142.4 331.95,-137.52 330.56,-143.31\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-141.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q7 -->\n<g id=\"node9\" class=\"node\">\n<title>q7</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"361.84\" cy=\"-22.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"361.84\" y=\"-18.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q7</text>\n</g>\n<!-- q6&#45;&gt;q7 -->\n<g id=\"edge10\" class=\"edge\">\n<title>q6&#45;&gt;q7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M288.09,-49.73C301.04,-44.88 317.9,-38.57 332.12,-33.25\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"333.22,-36.01 340.14,-30.25 331.14,-30.44 333.22,-36.01\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-45.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q4&#45;&gt;q3 -->\n<g id=\"edge13\" class=\"edge\">\n<title>q4&#45;&gt;q3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M344.21,-132.94C337.5,-127.98 329.38,-123.05 321.09,-120.5 314.88,-118.59 308.08,-118.01 301.48,-118.18\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"300.92,-115.24 292.66,-118.82 301.35,-121.17 300.92,-115.24\"/>\n<text text-anchor=\"middle\" x=\"316.09\" y=\"-124.3\" font-family=\"Times-Roman\" font-size=\"14.00\">b</text>\n</g>\n<!-- q8 -->\n<g id=\"node10\" class=\"node\">\n<title>q8</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"455.33\" cy=\"-52.5\" rx=\"26.5\" ry=\"26.5\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"455.33\" cy=\"-52.5\" rx=\"30.5\" ry=\"30.5\"/>\n<text text-anchor=\"middle\" x=\"455.33\" y=\"-48.8\" font-family=\"Times-Roman\" font-size=\"14.00\">q8</text>\n</g>\n<!-- q7&#45;&gt;q8 -->\n<g id=\"edge11\" class=\"edge\">\n<title>q7&#45;&gt;q8</title>\n<path fill=\"none\" stroke=\"black\" d=\"M383.6,-29.31C394.84,-33 408.99,-37.63 421.68,-41.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"420.76,-44.62 429.76,-44.44 422.61,-38.97 420.76,-44.62\"/>\n<text text-anchor=\"middle\" x=\"406.58\" y=\"-41.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n<!-- q8&#45;&gt;q6 -->\n<g id=\"edge12\" class=\"edge\">\n<title>q8&#45;&gt;q6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M428.73,-55.32C400.08,-58.15 352.29,-61.91 311.09,-60.5 306.73,-60.35 302.13,-60.12 297.62,-59.85\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"297.81,-56.88 289.13,-59.29 297.42,-62.82 297.81,-56.88\"/>\n<text text-anchor=\"middle\" x=\"361.84\" y=\"-64.3\" font-family=\"Times-Roman\" font-size=\"14.00\">c</text>\n</g>\n</g>\n</svg>\n",
"text/plain": "<AGraph <Swig Object of type 'Agraph_t *' at 0x7f461c387c90>>"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q26b = NFA(\n",
"\tstates={'q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8'},\n",
"\tinput_symbols={'a', 'b', 'c'},\n",
"\ttransitions={\n",
"\t\t'q0': {'a': {'q1'}, 'b': {'q5'}},\n",
"\t\t'q1': {'a': {'q2'}, 'b': {'q3'}},\n",
"\t\t'q2': {'a': {'q1'}},\n",
"\t\t'q3': {'b': {'q4'}},\n",
"\t\t'q4': {'b': {'q3'}},\n",
"\t\t'q5': {'b': {'q5'}, 'c': {'q6'}},\n",
"\t\t'q6': {'c': {'q7'}},\n",
"\t\t'q7': {'c': {'q8'}},\n",
"\t\t'q8': {'c': {'q6'}},\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q0', 'q3', 'q5', 'q8'},\n",
")\n",
"t = [\n",
" 'ab',\n",
" 'aaabbb',\n",
" 'aaaaabbbbb',\n",
" 'b',\n",
" 'bb',\n",
" 'bbccc',\n",
" 'bbcccccc',\n",
"]\n",
"f = [\n",
" 'a',\n",
" 'aaa',\n",
" 'aaaa',\n",
" 'bba',\n",
" 'ca',\n",
" 'cb',\n",
" 'cc',\n",
" 'ccca',\n",
" 'bbba',\n",
" 'bbbbbc',\n",
"]\n",
"if debug:\n",
" nfa_test(q26b, t)\n",
" print('-'*20)\n",
" nfa_test(q26b, f)\n",
"\n",
"q26b.show_diagram()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-02-17T00:51:01.566831900Z",
"start_time": "2024-02-17T00:51:01.509831400Z"
}
},
"id": "b44d21890b7c5fe6",
"execution_count": 6
}
],
"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
}