CS2333/Assignment 2.ipynb
2024-01-26 15:05:29 -04:00

159 lines
10 KiB
Plaintext

{
"cells": [
{
"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=\"285pt\" height=\"94pt\"\n viewBox=\"0.00 0.00 284.84 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 280.84,-90.25 280.84,4 -4,4\"/>\n<!-- 5c4499d3&#45;b700&#45;445f&#45;93e5&#45;0c749b5c16e1 -->\n<g id=\"node1\" class=\"node\">\n<title>5c4499d3&#45;b700&#45;445f&#45;93e5&#45;0c749b5c16e1</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<!-- 5c4499d3&#45;b700&#45;445f&#45;93e5&#45;0c749b5c16e1&#45;&gt;q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>5c4499d3&#45;b700&#45;445f&#45;93e5&#45;0c749b5c16e1&#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\">0</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\">1</text>\n</g>\n<!-- q1&#45;&gt;q0 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1&#45;&gt;q0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M135.7,-16.66C126.13,-12.26 114.71,-9.09 104.1,-11.5 99.82,-12.47 95.47,-13.95 91.29,-15.68\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"90.03,-12.98 83.53,-19.21 92.5,-18.4 90.03,-12.98\"/>\n<text text-anchor=\"middle\" x=\"109.1\" y=\"-15.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=\"254.34\" cy=\"-30.5\" rx=\"22.5\" ry=\"22.5\"/>\n<text text-anchor=\"middle\" x=\"254.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=\"M185.89,-30.5C197.34,-30.5 210.87,-30.5 222.8,-30.5\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"223.04,-33.48 231.54,-30.5 223.04,-27.53 223.04,-33.48\"/>\n<text text-anchor=\"middle\" x=\"208.59\" y=\"-34.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q2&#45;&gt;q1 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q2&#45;&gt;q1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M234.1,-19.8C227.76,-16.83 220.55,-14 213.59,-12.5 206.06,-10.87 198.1,-11.93 190.68,-14.17\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"189.23,-11.54 182.29,-17.27 191.29,-17.12 189.23,-11.54\"/>\n<text text-anchor=\"middle\" x=\"208.59\" y=\"-16.3\" font-family=\"Times-Roman\" font-size=\"14.00\">1</text>\n</g>\n<!-- q2&#45;&gt;q2 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2&#45;&gt;q2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M246.29,-51.88C245.3,-62.17 247.98,-71.25 254.34,-71.25 258.81,-71.25 261.47,-66.76 262.3,-60.52\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"265.28,-60.41 262.39,-51.88 259.33,-60.35 265.28,-60.41\"/>\n<text text-anchor=\"middle\" x=\"254.34\" y=\"-75.05\" 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 0x7f4d007b8a20>>"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from automata.fa.dfa import DFA\n",
"\n",
"# DFA which matches all binary strings ending in an odd number of '1's\n",
"my_dfa = DFA(\n",
"\tstates={'q0', 'q1', 'q2'},\n",
"\tinput_symbols={'0', '1'},\n",
"\ttransitions={\n",
"\t\t'q0': {'0': 'q0', '1': 'q1'},\n",
"\t\t'q1': {'0': 'q0', '1': 'q2'},\n",
"\t\t'q2': {'0': 'q2', '1': 'q1'}\n",
"\t},\n",
"\tinitial_state='q0',\n",
"\tfinal_states={'q1'}\n",
")\n",
"\n",
"my_dfa.show_diagram()\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-01-26T19:01:38.983537200Z",
"start_time": "2024-01-26T19:01:38.652214500Z"
}
},
"id": "ad7a031539dffdd7",
"execution_count": 1
},
{
"cell_type": "markdown",
"source": [
"# Question 6\n",
"Let $S = \\{0,1\\}^*$ be the set of all strings of zero and ones, which includes the empty string $\\epsilon$.\n",
"Let $h : S \\rightarrow \\mathbb{Z}^*$ be the function defined by $h(x)$ equal the number of zeros in $x$ multiplied by the number of ones in $x$\n",
"For example, $h(00100011) = 5 \\times 3 = 15, and h(111) = 0 \\times 3 = 0$\n",
"\n",
"(a) Is $h$ one-to-one? No, because both strings $001$ and $110$ map to the same value, $2$\n",
"$2 \\times 1 = 2$ and $1 \\times 2 = 2$\n",
"\n",
"(b) Is $h$ onto? Yes, because you can find every non-negative integer by multiplying any number of ones by one zero $(1 \\times 1), (1 \\times 2), ...$\n",
"Let the number of zeros be exactly 1, and n be the number of ones, and m be any non-negative integer\n",
"$h(n) = m, 1n = m, n = m$ "
],
"metadata": {
"collapsed": false
},
"id": "b67364155fcb1072"
},
{
"cell_type": "markdown",
"source": [
"# Question 8\n",
"(a)\n",
"This function is not one-to-one (111110, 1111100 are both 5)\n",
"This function is onto (10, 110, 1110, 11...0 is all non-negative integers)\n",
"\n",
"(b)\n",
"This function is not one-to-one (11111, 01111 are both 1)\n",
"This function is onto (All strings are mapped to, as you can simply pad any character with any 4 characters first)\n",
"\n",
"(c)\n",
"This function is not one-to-one (Quebec, Yukon both are 0)\n",
"This function is not onto (None of the provinces contain 3 or 5 a's"
],
"metadata": {
"collapsed": false
},
"id": "ead9052998c5edf6"
},
{
"cell_type": "markdown",
"source": [
"# Question 9\n",
"Consider the relation $R$ defined on the set $\\mathbb{Z}$ as follows:\n",
"$$R = \\{(m,n) | m,n \\in \\mathbb{Z}, mn < 0\\}$$\n",
"\n",
"(a) Is the relation reflexive? No\n",
"For every integer $x$, $x \\times x = x^2$, and by the definition of squares, can never be less than zero\n",
"\n",
"(b) Is the relation symmetric? Yes\n",
"Let $x, y$ be a pair of integers in the relation and $z$ be the product of $x and y$, so $x \\times y = -z$\n",
"Since multiplication is commutative, the position of $x$ and $y$ do not matter, so $(x,y)$ and $(y,x)$ are in the relation\n",
"(Both $x \\times y$ and $y \\times x$ equal $-z$)\n",
"\n",
"(c) Is the relation transitive? No\n",
"Let $a = 1, b = -2, c = 3$\n",
"$1 \\times -2 = -2$ and $-2 \\times 3 = -6$, however $1 \\times 3 = 3$, which is $> 0$\n",
"$\\therefore$ this relation is not transitive\n",
"\n",
"(d) Is this an equivalence relation? No, because these three conditions are not met"
],
"metadata": {
"collapsed": false
},
"id": "874c8dbcc345edad"
},
{
"cell_type": "markdown",
"source": [
"# Question 11\n",
"Consider the relation $R$ defined on the set $\\mathbb{Z}$ as follows:\n",
"$$\\forall m,n \\in \\mathbb{Z}, (m,n \\in R \\text{ if and only if } m + n = 2k \\text{ for some integer } k$$\n",
"\n",
"(a) Is this relation reflexive? Yes\n",
"For every integer $x$, $x + x = 2x$, therefore it is reflexive\n",
"\n",
"(b) Is this relation symmetric? Yes\n",
"Let $x, y$ be a pair of integers in the relation and $z$ be the sum of these integers, so $x + y = 2z$\n",
"Since addition is commutative, the position of $x$ and $y$ do not matter, so $(x,y) and $(y,x) are in the relation\n",
"(Both $x + y$ and $y + x$ equal $2z$)\n",
"\n",
"(c) Is this relation transitive? Yes\n",
"Let $(a,b)$ and $(b,c)$ be valid pairs of integers of the relation $R$\n",
"$a + b = 2n$, and $b + c = 2p$"
],
"metadata": {
"collapsed": false
},
"id": "5fe823d05dd08b12"
}
],
"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
}