Finish a2
This commit is contained in:
parent
0b83fe9bfb
commit
1e91ba5ba6
@ -1,46 +1,5 @@
|
||||
{
|
||||
"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-b700-445f-93e5-0c749b5c16e1 -->\n<g id=\"node1\" class=\"node\">\n<title>5c4499d3-b700-445f-93e5-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-b700-445f-93e5-0c749b5c16e1->q0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>5c4499d3-b700-445f-93e5-0c749b5c16e1->q0</title>\n<g id=\"a_edge1\"><a xlink:title=\"->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->q0 -->\n<g id=\"edge2\" class=\"edge\">\n<title>q0->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->q1 -->\n<g id=\"edge3\" class=\"edge\">\n<title>q0->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->q0 -->\n<g id=\"edge4\" class=\"edge\">\n<title>q1->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->q2 -->\n<g id=\"edge5\" class=\"edge\">\n<title>q1->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->q1 -->\n<g id=\"edge6\" class=\"edge\">\n<title>q2->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->q2 -->\n<g id=\"edge7\" class=\"edge\">\n<title>q2->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": [
|
||||
@ -61,6 +20,16 @@
|
||||
},
|
||||
"id": "b67364155fcb1072"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"<div style=\"page-break-after: always;\"></div>"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ec5269e5b4749d08"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
@ -82,6 +51,16 @@
|
||||
},
|
||||
"id": "ead9052998c5edf6"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"<div style=\"page-break-after: always;\"></div>"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "f27520443479cce9"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
@ -109,12 +88,22 @@
|
||||
},
|
||||
"id": "874c8dbcc345edad"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"<div style=\"page-break-after: always;\"></div>"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "ca477f12a96f397a"
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"$$\\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",
|
||||
@ -126,12 +115,147 @@
|
||||
"\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$"
|
||||
"$a + b = 2n$, and $b + c = 2p$, then $b = 2n - a$ and $b = 2p - c$\n",
|
||||
"$-2n + a = 2p - c$\n",
|
||||
"$a + c = 2p + 2n$\n",
|
||||
"Since the sum of a and c is the sum of two even numbers (numbers multiplied by 2 must be even), then the result must be even, so the result is divisible by 2\n",
|
||||
"$\\therefore$ The relationship is transitive"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "5fe823d05dd08b12"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"<div style=\"page-break-after: always;\"></div>"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "889a2a7359618ee7"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Question 14\n",
|
||||
"There are 7 equivalence classes as the results are grouped by the integer returned by $n_0(x) - n_1(x)$/$n_0(y) - n_1(y)$ for any relation pair, the results are that being set of differences $\\{-3, -2, -1, 0, 1, 2, 3\\}$"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "fd69c73a15e8e6a0"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{-3: [('111', '111')],\n",
|
||||
" -2: [('11', '11')],\n",
|
||||
" -1: [('1', '1'),\n",
|
||||
" ('1', '011'),\n",
|
||||
" ('1', '101'),\n",
|
||||
" ('1', '110'),\n",
|
||||
" ('011', '1'),\n",
|
||||
" ('011', '011'),\n",
|
||||
" ('011', '101'),\n",
|
||||
" ('011', '110'),\n",
|
||||
" ('101', '1'),\n",
|
||||
" ('101', '011'),\n",
|
||||
" ('101', '101'),\n",
|
||||
" ('101', '110'),\n",
|
||||
" ('110', '1'),\n",
|
||||
" ('110', '011'),\n",
|
||||
" ('110', '101'),\n",
|
||||
" ('110', '110')],\n",
|
||||
" 0: [('', ''),\n",
|
||||
" ('', '01'),\n",
|
||||
" ('', '10'),\n",
|
||||
" ('01', ''),\n",
|
||||
" ('01', '01'),\n",
|
||||
" ('01', '10'),\n",
|
||||
" ('10', ''),\n",
|
||||
" ('10', '01'),\n",
|
||||
" ('10', '10')],\n",
|
||||
" 1: [('0', '0'),\n",
|
||||
" ('0', '001'),\n",
|
||||
" ('0', '010'),\n",
|
||||
" ('0', '100'),\n",
|
||||
" ('001', '0'),\n",
|
||||
" ('001', '001'),\n",
|
||||
" ('001', '010'),\n",
|
||||
" ('001', '100'),\n",
|
||||
" ('010', '0'),\n",
|
||||
" ('010', '001'),\n",
|
||||
" ('010', '010'),\n",
|
||||
" ('010', '100'),\n",
|
||||
" ('100', '0'),\n",
|
||||
" ('100', '001'),\n",
|
||||
" ('100', '010'),\n",
|
||||
" ('100', '100')],\n",
|
||||
" 2: [('00', '00')],\n",
|
||||
" 3: [('000', '000')]}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Set of all strings containing 0 and 1 up to length 3\n",
|
||||
"def get_all_strings_with_a_given_alphabet_and_length(_alphabet, _length):\n",
|
||||
"\t_strings = ['']\n",
|
||||
"\tfor i in range(_length):\n",
|
||||
"\t\ts = [s + c for s in _strings for c in _alphabet]\n",
|
||||
"\t\t_strings += s\n",
|
||||
"\n",
|
||||
"\t# Remove duplicates (set() isn't as nice as it doesn't preserve order, at least for verification purposes)\n",
|
||||
"\t_strings = list(dict.fromkeys(_strings))\n",
|
||||
"\treturn _strings\n",
|
||||
"\n",
|
||||
"alphabet = ['0', '1']\n",
|
||||
"strings = get_all_strings_with_a_given_alphabet_and_length(alphabet, 3)\n",
|
||||
"\n",
|
||||
"# The relation is valid if the number of zeros from string x minus the number of ones from string x is equal to the number of zeros from string y minus the number of ones from string y\n",
|
||||
"def is_valid_relation(_x, _y):\n",
|
||||
"\t# Count the number of zeros and ones in each string\n",
|
||||
"\tx_zeros = _x.count('0')\n",
|
||||
"\tx_ones = _x.count('1')\n",
|
||||
"\ty_zeros = _y.count('0')\n",
|
||||
"\ty_ones = _y.count('1')\n",
|
||||
"\n",
|
||||
"\t# Return true if the difference between the number of zeros and ones is equal\n",
|
||||
"\treturn (x_zeros - x_ones) == (y_zeros - y_ones)\n",
|
||||
"\n",
|
||||
"from collections import defaultdict\n",
|
||||
"\n",
|
||||
"# Print all valid relations\n",
|
||||
"equivalence_classes = defaultdict(list)\n",
|
||||
"for x in strings:\n",
|
||||
"\tfor y in strings:\n",
|
||||
"\t\tif is_valid_relation(x, y):\n",
|
||||
"\t\t\t# create a tuple of the two strings\n",
|
||||
"\t\t\tt = (x, y)\n",
|
||||
"\t\t\t# calculate the difference between the number of zeros and ones\n",
|
||||
"\t\t\td = x.count('0') - x.count('1')\n",
|
||||
"\t\t\t# add the tuple a list under the key of the difference\n",
|
||||
"\t\t\tequivalence_classes[d].append(t)\n",
|
||||
"\t\t\t\t\n",
|
||||
"# Print the dict\n",
|
||||
"equivalence_classes = dict(equivalence_classes)\n",
|
||||
"from pprint import pprint\n",
|
||||
"pprint(equivalence_classes)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-01-26T22:03:01.411977200Z",
|
||||
"start_time": "2024-01-26T22:03:01.364977700Z"
|
||||
}
|
||||
},
|
||||
"id": "94712ae197e8639c",
|
||||
"execution_count": 5
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
Loading…
Reference in New Issue
Block a user