{ "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": "\n\n\n\n\n\n\n\n\nef361926-1987-4988-9833-4dc84f84fd2e\n\n\n\n\n\n\n\nq0\n\nq0\n\n\n\nef361926-1987-4988-9833-4dc84f84fd2e->q0\n\n\n\n\n\n\n\n\nq1\n\nq1\n\n\n\nq0->q1\n\n\n0\n\n\n\nq2\n\nq2\n\n\n\nq1->q2\n\n\n1\n\n\n\nq3\n\nq3\n\n\n\nq2->q3\n\n\n1\n\n\n\nq3->q3\n\n\n0,1\n\n\n\nq4\n\nq4\n\n\n\nq3->q4\n\n\n0\n\n\n\nq5\n\nq5\n\n\n\nq4->q5\n\n\n0\n\n\n\nq6\n\n\nq6\n\n\n\nq5->q6\n\n\n0\n\n\n\n", "text/plain": ">" }, "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": "\n\n\n\n\n\n\n\n\nb36964c3-7cac-44c2-80ec-5a57927f24cb\n\n\n\n\n\n\n\nq0\n\nq0\n\n\n\nb36964c3-7cac-44c2-80ec-5a57927f24cb->q0\n\n\n\n\n\n\n\n\nq3\n\nq3\n\n\n\nq0->q3\n\n\nc\n\n\n\nq1\n\nq1\n\n\n\nq0->q1\n\n\na\n\n\n\nq2\n\nq2\n\n\n\nq0->q2\n\n\nb\n\n\n\nq3->q3\n\n\na,b,c\n\n\n\nq4\n\n\nq4\n\n\n\nq3->q4\n\n\nc\n\n\n\nq1->q1\n\n\na,b,c\n\n\n\nq1->q4\n\n\na\n\n\n\nq2->q2\n\n\na,b,c\n\n\n\nq2->q4\n\n\nb\n\n\n\n", "text/plain": ">" }, "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": "\n\n\n\n\n\n\n\n\nc6536698-09e6-4b0c-b9c0-932845697f5e\n\n\n\n\n\n\n\nq0\n\nq0\n\n\n\nc6536698-09e6-4b0c-b9c0-932845697f5e->q0\n\n\n\n\n\n\n\n\nq1\n\nq1\n\n\n\nq0->q1\n\n\nε\n\n\n\nq5\n\nq5\n\n\n\nq0->q5\n\n\nε\n\n\n\nq1->q1\n\n\n0,1\n\n\n\nq2\n\nq2\n\n\n\nq1->q2\n\n\n0\n\n\n\nq3\n\nq3\n\n\n\nq1->q3\n\n\n1\n\n\n\nq6\n\nq6\n\n\n\nq5->q6\n\n\n0,1\n\n\n\nq4\n\n\nq4\n\n\n\nq2->q4\n\n\n0\n\n\n\nq3->q4\n\n\n1\n\n\n\nq7\n\n\nq7\n\n\n\nq6->q7\n\n\n0,1\n\n\n\nq7->q5\n\n\n0,1\n\n\n\n", "text/plain": ">" }, "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": "\n\n\n\n\n\n\n\n\n395dc4d0-bf07-4c32-b931-cf10ac1006e5\n\n\n\n\n\n\n\nq00\n\nq00\n\n\n\n395dc4d0-bf07-4c32-b931-cf10ac1006e5->q00\n\n\n\n\n\n\n\n\nq00->q00\n\n\na,b,c,d\n\n\n\nq01\n\nq01\n\n\n\nq00->q01\n\n\na\n\n\n\nq02\n\nq02\n\n\n\nq00->q02\n\n\nb\n\n\n\nq03\n\nq03\n\n\n\nq00->q03\n\n\nc\n\n\n\nq04\n\nq04\n\n\n\nq00->q04\n\n\nd\n\n\n\nq05\n\nq05\n\n\n\nq01->q05\n\n\na\n\n\n\nq06\n\nq06\n\n\n\nq02->q06\n\n\nb\n\n\n\nq07\n\nq07\n\n\n\nq03->q07\n\n\nc\n\n\n\nq08\n\nq08\n\n\n\nq04->q08\n\n\nd\n\n\n\nq09\n\nq09\n\n\n\nq05->q09\n\n\na\n\n\n\nq10\n\nq10\n\n\n\nq06->q10\n\n\nb\n\n\n\nq11\n\nq11\n\n\n\nq07->q11\n\n\nc\n\n\n\nq12\n\nq12\n\n\n\nq08->q12\n\n\nd\n\n\n\nq13\n\n\nq13\n\n\n\nq09->q13\n\n\na\n\n\n\nq10->q13\n\n\nb\n\n\n\nq11->q13\n\n\nc\n\n\n\nq12->q13\n\n\nd\n\n\n\nq13->q13\n\n\na,b,c,d\n\n\n\n", "text/plain": ">" }, "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": "\n\n\n\n\n\n\n\n\n9a93236f-ac2b-4384-8c2a-d9686587f8ad\n\n\n\n\n\n\n\nq0\n\n\nq0\n\n\n\n9a93236f-ac2b-4384-8c2a-d9686587f8ad->q0\n\n\n\n\n\n\n\n\nq1\n\nq1\n\n\n\nq0->q1\n\n\na\n\n\n\nq5\n\n\nq5\n\n\n\nq0->q5\n\n\nb\n\n\n\nq2\n\nq2\n\n\n\nq1->q2\n\n\na\n\n\n\nq3\n\n\nq3\n\n\n\nq1->q3\n\n\nb\n\n\n\nq5->q5\n\n\nb\n\n\n\nq6\n\nq6\n\n\n\nq5->q6\n\n\nc\n\n\n\nq2->q1\n\n\na\n\n\n\nq4\n\nq4\n\n\n\nq3->q4\n\n\nb\n\n\n\nq7\n\nq7\n\n\n\nq6->q7\n\n\nc\n\n\n\nq4->q3\n\n\nb\n\n\n\nq8\n\n\nq8\n\n\n\nq7->q8\n\n\nc\n\n\n\nq8->q6\n\n\nc\n\n\n\n", "text/plain": ">" }, "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 }