From 1e91ba5ba64306765973e8b4324533b034ea1600 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Fri, 26 Jan 2024 18:16:56 -0400 Subject: [PATCH] Finish a2 --- Assignment 2.ipynb | 210 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 167 insertions(+), 43 deletions(-) diff --git a/Assignment 2.ipynb b/Assignment 2.ipynb index 006593c..8a2ddc4 100644 --- a/Assignment 2.ipynb +++ b/Assignment 2.ipynb @@ -1,46 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "outputs": [ - { - "data": { - "image/svg+xml": "\n\n\n\n\n\n\n\n\n5c4499d3-b700-445f-93e5-0c749b5c16e1\n\n\n\n\n\n\n\nq0\n\nq0\n\n\n\n5c4499d3-b700-445f-93e5-0c749b5c16e1->q0\n\n\n\n\n\n\n\n\nq0->q0\n\n\n0\n\n\n\nq1\n\n\nq1\n\n\n\nq0->q1\n\n\n1\n\n\n\nq1->q0\n\n\n0\n\n\n\nq2\n\nq2\n\n\n\nq1->q2\n\n\n1\n\n\n\nq2->q1\n\n\n1\n\n\n\nq2->q2\n\n\n0\n\n\n\n", - "text/plain": ">" - }, - "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": [ + "
" + ], + "metadata": { + "collapsed": false + }, + "id": "ec5269e5b4749d08" + }, { "cell_type": "markdown", "source": [ @@ -82,6 +51,16 @@ }, "id": "ead9052998c5edf6" }, + { + "cell_type": "markdown", + "source": [ + "
" + ], + "metadata": { + "collapsed": false + }, + "id": "f27520443479cce9" + }, { "cell_type": "markdown", "source": [ @@ -109,12 +88,22 @@ }, "id": "874c8dbcc345edad" }, + { + "cell_type": "markdown", + "source": [ + "
" + ], + "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": [ + "
" + ], + "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": {