CS2333/Assignment 2.ipynb

145 lines
4.9 KiB
Plaintext
Raw Normal View History

2024-01-25 23:59:58 -04:00
{
"cells": [
{
"cell_type": "code",
"outputs": [],
"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
},
"id": "ad7a031539dffdd7",
"execution_count": null
},
{
"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
}