{ "metadata": { "name": "", "signature": "sha256:d6505774a4cce1c5726d5bf482120157569ff894ee6f05281095d53b18882117" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "3 Relations and Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 06:Page 118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Given a relation R on a set A={a,b,c,d} in the form of a graph, the transitive closure of R is to be found using Warshall's algorithm\"\n", "#Each element in A acts as a node\n", "nodes=input(\"Enter the number of nodes\")#This is the total number of elements in the given set A\n", "w0=[[0 for i in range (nodes)]for j in range(nodes)]#Matrix elements are initialised to zero\n", "#Gets the initial value for the matrix elements\n", "print \"Enter the elements of W0. (Enter only 0 or 1)\"# The value is 1 if there is a path between i and j in the given graph, else it is 0\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print \"Enter value for w\",i,j\n", " w0[i][j]=input()\n", "#To print the given matrix\n", "print \"The given matrix W0=MR(Matrix relation on R) is\"\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print w0[i][j],\n", " print \"\\n\"\n", "for k in range(0,nodes):#To find the transitive relation matrix using Warshall's algorithm\n", " for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " w0[i][j]=w0[i][j] or (w0[i][k] and w0[k][j])\n", "#To print the transitive relation matrix\n", "print \"The required transitive relation matrix is\"\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print w0[i][j],\n", " print \"\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Given a relation R on a set A={a,b,c,d} in the form of a graph, the transitive closure of R is to be found using Warshall's algorithm\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of nodes4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the elements of W0. (Enter only 0 or 1)\n", "Enter value for w 0 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The given matrix W0=MR(Matrix relation on R) is\n", "0 1 0 0 \n", "\n", "1 0 1 0 \n", "\n", "0 0 0 1 \n", "\n", "1 0 0 0 \n", "\n", "The required transitive relation matrix is\n", "1 1 1 1 \n", "\n", "1 1 1 1 \n", "\n", "1 1 1 1 \n", "\n", "1 1 1 1 \n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 07:Page 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Given a relation R on a set A={a,b,c,d,e} in the form of a graph, the transitive closure of R is to be found using Warshall's algorithm\"\n", "print \"Given:R={(b,c),(b,e),(c,e),(d,a),(c,b),(e,c)}\"\n", "#Each element in A acts as a node\n", "nodes=input(\"Enter the number of nodes\")#This is the total number of elements in the given set A\n", "w0=[[0 for i in range (nodes)]for j in range(nodes)]#Matrix elements are initialised to zero\n", "#Gets the initial value for the matrix elements\n", "print \"Enter the elements of W0. (Enter only 0 or 1)\"# The value is 1 if there is a path between i and j in the given graph, else it is 0\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print \"Enter value for w\",i,j\n", " w0[i][j]=input()\n", "#To print the given matrix\n", "print \"The given matrix W0=MR(Matrix relation on R) is\"\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print w0[i][j],\n", " print \"\\n\"\n", "for k in range(0,nodes):#To find the transitive relation matrix using Warshall's algorithm\n", " for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " w0[i][j]=w0[i][j] or (w0[i][k] and w0[k][j])\n", "#To print the transitive relation matrix\n", "print \"The required transitive relation matrix is\"\n", "for i in range(0,nodes):\n", " for j in range(0,nodes):\n", " print w0[i][j],\n", " print \"\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Given a relation R on a set A={a,b,c,d,e} in the form of a graph, the transitive closure of R is to be found using Warshall's algorithm\n", "Given:R={(b,c),(b,e),(c,e),(d,a),(c,b),(e,c)}\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of nodes5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the elements of W0. (Enter only 0 or 1)\n", "Enter value for w 0 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 0 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 1 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 2 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 3 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 4 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 4 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 4 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 4 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter value for w 4 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The given matrix W0=MR(Matrix relation on R) is\n", "0 0 0 0 0 \n", "\n", "0 0 1 0 1 \n", "\n", "0 0 0 0 1 \n", "\n", "1 0 0 0 0 \n", "\n", "0 1 1 0 0 \n", "\n", "The required transitive relation matrix is\n", "0 0 0 0 0 \n", "\n", "0 1 1 0 1 \n", "\n", "0 1 1 0 1 \n", "\n", "1 0 0 0 0 \n", "\n", "0 1 1 0 1 \n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 41:Page 154" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Given function definition is as follows\n", "print \"RECURSIVE FUNCTIONS\"\n", "def f(x):\n", " if x==0:\n", " return 1\n", " else:\n", " return 3*f(x-1)+1\n", "print \"f(1)=\",f(1)\n", "print \"f(2)=\",f(2)\n", "print \"f(3)=\",f(3)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "RECURSIVE FUNCTIONS\n", "f(1)= 4\n", "f(2)= 13\n", "f(3)= 40\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 43:Page 156" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"A recursive function to calculate the GCD of two numbers\"\n", "def GCD(x,y):\n", " if x>y:\n", " x=x-y\n", " return GCD(x,y)\n", " elif x