DSTL Lab Experiment File
DSTL Lab Experiment File
(KCS – 301)
INDEX
S.no. Name of Experiment Date of Signature Remark
Experiment of Faculty
with Date
1. Write a C program to create two sets
and perform the Union operation on
sets.
2. Write a program in C to create two
sets and perform the Intersection
operation on sets.
3. Write a program in C to create two
sets and perform the Difference
operation on sets.
4. Write a program in C to create two
sets and perform the Symmetric
difference operation.
5. Write a program in C to perform
power set operation on a set.
6. Write a program in C to display the
Boolean Truth Table for
AND,OR,NOT.
7. Write a C program to perform
Cartesian product of two sets.
8. Write a program in C for minimum
cost spanning tree.
9. Write a program in C for finding
shortest path in graph.
10. Working of Computation Software,
basic Operations.
11. Discover a closed formula for a given
recursive sequence vice-versa.
12. Implementation of Recursive functions
with help of SageMath tool
13. Sets and Set operations using
SageMath tool.
14. Counting
15. Combinatorial Equivalence
16. Permutation and Combination
operations using SageMath tool.
17. Difference between structures,
permutations and sets.
18. Implementation of recursive counting
technique.
19. The birthday problem
EXPERIMENT 01
OBJECTIVE : Write a C program to create two sets and perform the Union operation on sets.
BRIEF THEORY : Union of two given sets is the smallest set which contains all the elements of both the sets.
The union of two given sets A and B is a set which consists of all the elements of A and all the elements of B such
that no element is repeated. The symbol for denoting union of sets is ‘∪’.
Let set A = {2, 4, 5, 6} and set B = {4, 6, 7, 8}. Taking every element of both the sets A and B, without repeating
any element, we get a new set = {2, 4, 5, 6, 7, 8}.
CODE :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3],b[3],c[6],i,j,k=0;
printf("Name- Ish Kumar , 1901920100129 \n");
printf("enter the elements of set A\n");
for(i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<3;i++)
{
c[i]=a[i];
}
for(j=0;j<3;j++)
{
c[3+j]=b[j];
}
printf("the union of two sets is\n");
for(i=0;i<6;i++)
{ k=0;
for(j=i+1;j<6;j++)
{
if(c[i]==c[j])
{
k=1;
break;
}
}
if(k==0)
{
printf("%d\t",c[i]);
}
}
getch();
return 0;
}
OUTPUT
EXPERIMENT 02
OBJECTIVE : Write a program in C to create two sets and perform the Intersection operation on sets.
BRIEF THEORY :Intersection of two given sets is the largest set which contains all the elements that are
common to both the sets.To find the intersection of two given sets A and B is a set which consists of all the
elements which are common to both A and B.The symbol for denoting intersection of sets is ‘∩‘.
Let set A = {2, 3, 4, 5, 6} and set B = {3, 5, 7, 9}. In this two sets, the elements 3 and 5 are common. The set
containing these common elements i.e., {3, 5} is the intersection of set A and B.
CODE :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],m,n,i,j;
int c[20],k=0,flag=0;
int ch;
printf("Enter the number of elements in first set:\n");
scanf("%d",&m);
printf("Enter the elements:\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("\nElement of First set:\n");
for(i=0;i<m;i++)
{
printf("%d\t",a[i]);
}
printf("\nEnter the number of elements in second set:\n");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("\nElement of Second set:\n");
for(i=0;i<n;i++)
{
printf("%d\t",b[i]);
}
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==1)
{
c[k]=a[i];
k++;
}
}
if(k==0)
{
printf("\n\nResultant set is null set!\n");
}
else
{
printf("\nElement of resultant set\n");
for(i=0;i<k;i++)
{
printf("%d\t",c[i]);
}
}
}
OUTPUT :
EXPERIMENT 03
OBJECTIVE : Write a program in C to create two sets and perform the Difference operation on sets.
BRIEF THEORY: The difference of two sets, written A - B is the set of all elements of A that are not
elements of B. The difference operation, along with union and intersection, is an important and
fundamental set theory operation.
let's consider the sets A = {1, 2, 3, 4, 5} and B = {3, 4, 5, 6, 7, 8}. To find the difference A - B of these two sets, we
begin by writing all of the elements of A, and then take away every element of A that is also an element of B.
Since A shares the elements 3, 4 and 5 with B, this gives us the set difference A - B = {1, 2}.
CODE :
#include<stdio.h>
#define max 100
int ifexists(int z[], int u, int v)
{
int i;
if (u==0) return 0;
for (i=0; i<=u;i++)
if (z[i]==v) return (1);
return (0);
}
void main()
{
int p[max], q[max], r[max];
int m,n;
int i,j,k;
printf("Enter length of first array:");
scanf("%d",&m);
printf("Enter %d elements of first array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second array:");
scanf("%d",&n);
printf("Enter %d elements of second array\n",n);
for(i=0;i<n;i++ )
scanf("%d",&q[i]);
k=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (p[i]==q[j])
{
break;
}
}
if(j==n)
{
if(!ifexists(r,k,p[i]))
{
r[k]=p[i];
k++;
}
}
}
printf("\nThe difference of the two array is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
}
OUTPUT :
EXPERIMENT 04
OBJECTIVE : Write a program in C to create two sets and perform the Symmetric difference operation.
BRIEF THEORY : Symmetric Difference basically contains all elements of two sets except common
elements. Symmetric difference of twosets is the all set elements of both set except the elements that are presents
in both set. Symmetric Difference = (set1 - set2) UNION (set2 –set1).
Let’s consider the example, a[ ] = {2,4,5,7,8,10,12,15} and b[]={5,8,11,12,14,15}; than Symmetric difference will
be {2,4,7,10,11,14}.
CODE :
#include<stdio.h>
void symmDiff(int arr1[], int arr2[], int n, int m)
{
int i = 0, j = 0;
while (i< n && j < m)
{
if (arr1[i] < arr2[j])
{
printf(" %d \t", arr1[i]);
i++;
}
else if (arr2[j] < arr1[i])
{
printf(" %d \t", arr2[j]);
j++;
}
else
{
i++;
j++;
}
}
}
int main()
{
printf("Name - Ish Kumar ,190192010029\n");
int arr1[] = {2, 4, 5, 7, 8, 10, 12, 15};
int arr2[] = {5, 8, 11, 12, 14, 15};
int n = sizeof(arr1)/sizeof(arr1[0]);
int m = sizeof(arr2)/sizeof(arr2[0]);
symmDiff(arr1, arr2, n, m);
return 0;
}
OUTPUT :
EXPERIMENT 05
OBJECTIVE : Write a program in C to perform power set operation on a set.
BRIEF THEORY : In set theory, the power set (or powerset) of a Set A is defined as the set of all subsets of
the Set A including the Set itself and the null or empty set. It is denoted by P(A). Basically, this set is the
combination of all subsets including null set, of a given set.If the given set has n elements, then its Power Set will
contain 2n elements. It also represents the cardinality of powerset.
Let us say Set A = { a, b, c }, Number of elements: 3.
The power set P(A) = { { } , { a }, { b }, { c }, { a, b }, { b, c }, { c, d }, { a, b, c } }
CODE :
#include <stdio.h>
#include <math.h>
void printPowerSet(char *set, int set_size)
{ unsigned int pow_set_size = pow(2, set_size);
int counter, j;
for(counter = 0; counter < pow_set_size; counter++) {
for(j = 0; j < set_size; j++)
{ if(counter & (1<<j))
printf("%c", set[j]);
}
printf(" ");
}
} int main()
{
char set[] = {'a','b','c'};
printPowerSet(set, 3);
return 0;
}
OUTPUT :
EXPERIMENT 06
OBJECTIVE : Write a program in C to display the Boolean Truth Table for AND,OR,NOT.
BRIEF THEORY : A truth table is a mathematical table used in logic—specifically in connection
with Boolean algebra, Boolean functions, and propositional calculus—which sets out the functional values of
logical expressions on each of their functional arguments, that is, for each combination of values taken by their
logical variables. In particular, truth tables can be used to show whether a propositional expression is true for all
legitimate input values, that is, logically valid.
CODE :
#include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2];
int i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
a[i][j]=(i&&j);
b[i][j]=(i||j);
}
}
for(i=0;i<=1;i++)
{
c[i]=(!i);
}
OUTPUT :
EXPERIMENT 07
OBJECTIVE : Write a C program to perform Cartesian product of two sets.
BRIEF THEORY: In mathematics, specifically set theory, the Cartesian product of two sets A and B,
denoted A × B, is the set of all ordered pairs (a, b) where a is in A and b is in B. In terms of set-builder notation, that
is. A table can be created by taking the Cartesian product of a set of rows and a set of columns.
let's consider the sets A = {1, 2} and B = {3, 4}. To find the Cartesian product of these two sets, we begin by
writing with first element of A, and then one by one with all elements of B, then second element of A with all
elements of B and so on. A X B = {{1,3}, {1,4}, {2,3}, {2,4}}
CODE :
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],b[50],c[50],i,s1,s2,j,k;
printf("Name - Ish Kumar,1901920100129\n");
printf("Enter how many elements in set 1\n");
scanf("%d",&s1);
printf("Enter how many elements in set 2\n");
scanf("%d",&s2);
printf("Enter elements of set 1\n");
for(i=0;i<s1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter elements of set 2\n");
for(i=0;i<s2;i++)
{
scanf("%d",&b[i]);
}
printf("cartessian product=");
printf("{");
for(i=0;i<s1;i++)
{
for(j=0;j<s2;j++)
{
printf("(%d,%d)",a[i],b[j]);
printf(",");
}
}
}
OUTPUT :
EXPERIMENT 08
OBJECTIVE : Write a program in C for minimum cost spanning tree.
BRIEF THEORY: A Spanning Tree for G is a subgraph of G that it is a free tree connecting all vertices in V.
The cost of a spanning tree is the sum of costs on its edges. An MST of G is a spanning tree of G having a minimum
cost.
SOFTWARE USED: onlinegdb.com
CODE :
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10];
void main()
{
int min,mincost=0,cost[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n) {
for (i=1,min=999;i<=n;i++)
for (j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0) {
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
OUTPUT :
EXPERIMENT 09
OBJECTIVE : Write a program in C for finding shortest path in graph.
BRIEF THEORY: Dijkstra algorithm is also called single source shortest path algorithm. It is based on greedy
technique. The algorithm maintains a list visited[ ] of vertices, whose shortest distance from the source is already
known.
If visited[1], equals 1, then the shortest distance of vertex i is already known. Initially, visited[i] is marked as,
for source vertex.
CODE :
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra (int G[MAX][MAX],int n, int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}
while(j!=startnode);
}
}
OUTPUT :
EXPERIMENT 10
OBJECTIVE: Working of Computation Software, basic Operations.
BRIEF THEORY : SageMath is a free open-source mathematics software system licensed
under the GPL. It builds on top of many existing open-source
packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more. Access their combined
power through a common, Python-based language or directly via interfaces or wrappers.
In this Experiment I’ll demonstrate the basic operations that can be done in SageMath tool, like Addition,
Subtraction, Multiplication, Division, Integer Division, type function, defining functions.
Mission: Creating a viable free open-source alternative to Magma, Maple, Mathematica and Matlab. Since version
9.0 released in January 2020, SageMath is using Python 3.
SAGEMATH CONSOLE:
Fig-1
The fig-1 demonstrates some basic operations of SageMath tool such as addition, substraction, division,
multiplication.
(b)Using type() function(Fig-2)
Fig-2
The type function is mainly used to define or to represent the type of variable used by the user, the varible can be
integer, string or a real number etc. There are many other predefined functions in SageMath tool designed for the
user to work more efficiently.
A. Defining a function
Fig-3
The another most important property in sage tool is to define the functions, i.e., the user can make their own user
defined functions.
B. Using the functionality of ITERATION.
Fig-4
The above figure-4 represents the functionality of iteration and range function in SageMath tool. The range function
provides the values from 1 to n-1 provided in the function, you can also make the function to provide values after
some gaps.
C. Boolean values
Fig-5
D. Functionality of Matrix and its operations
Fig-6
We not only can form the matrix but can also perform most of the operations associated with it, such as matrix
multiplication, matrix addition, matrix subtraction, etc with ease.
EXPERIMENT 11
OBJECTIVE : Discover a closed formula for a given recursive sequence vice-versa.
SOFTWARE USED : SageMath Tool
BRIEF THEORY : Consider the sequence an = an-1 + (2n + 1) (for n >1) with a1 = 1.
Using a closed (explicit) formula — In this case, each term of the sequence is the value of a specific function
evaluated at n, that term's index or position in the sequence, and so we can write an = f(n), where n belongs to some
subset of the natural numbers. Some examples of this type of sequence include:
– The arithmetic sequence an = a1 + (n−1)⋅d where a1 is the first term and d is the common difference between each
of the terms.
– The geometric sequence an = a⋅rn−1 where a is the initial value and r is the common ratio.
– The Harmonic sequence an = 1/n.
• Using recursion — In this case, each consecutive term of the sequence is constructed using the preceding terms,
with enough initial conditions provided to apply this rule to specify the first recursive term. Some examples of this
type of sequence include:
– The arithmetic sequence an = an−1+d
– The geometric sequence an = an−1⋅r
– The Fibonacci sequence an = an−1 + an−2 with a0 = 0 and a1 = 1
OUTPUT :
EXPERIMENT 12
OBJECTIVE : Implementation of Recursive functions with help of SageMath tool. Factorial and Greatest
common divisor are demonstrated into this experiment using SageMath tool.
BRIEF THEORY : Recursion is the process of repeating items in a self-similar way. In programming
languages, if a program allows you to call a function inside the same function, then it is called a recursive call of
the function.
SAGEMATH CONSOLE:
1) Elements of a Set have no order. So, you cannot access elements by an index.
2) An element in a Set only appears once
SAGEMATH CONSOLE:
A) CREATING A SET:
>with(combinat);
Don't forget that you can ask for help on any of these commands to find out more about them.For example, we will
use the permute command,look at the help file by executing the following line.Once you have looked it over,select
"Close Help Topic" under the file menu, and you will be returned to where you left.
>?permute
So for example,if want to list all permutations of length 2 with entries taken from {a,b,c,d}, we should use the
following command:
>permute([a,b,c,d],z);
The command nops will count the number of items in any list for you.So the command nops(permute([a,b,c,d],z);
should return the number of permutations in the above list.
Problem1. Draw a "game tree" with two branchings that generate the above list.Can you see how the number of
branches is related the number of permutations generated? Test your hypothesis by thinking about the structure of
the game tree for permute([a,b,c,d,e,f,g,h,i,j],2), predicting the number of permutations that will be generated,and
testing your prediction using nops command.
Unfortunately Maple does not have a similar command that generates all ordered lists, so we have to write
Ans.Execute the block of code below to define the new command,ordered lists,which we will we next.
>orderedlists:=proc(s,k)
Local i,L:
L:=NULL:
L:=L,seq(s[0],j=1..k)
od:
RETURN (permute([L],k))
end;
>orderedLists([a,b,c,d],2);
CODE :
EXPERIMENT 15
OBEJECTIVE :Combinatorial Equivalence
SOFTWARE USED : SageMath Tool
BRIEF THEORY :
The Binomial Theorem: One of the important ideas throughout the course is the fact that there is great
benefit in understanding many different answers to problems because from that understanding comes
the ability to tell how different questions are related to one another.The distributive law of
multiplication over addition tells us that when we expand (a0 + a1)*(b0 + b1) we form terms by
choosing each possibility from the first factor and each possibility from the second factor and
multiplying them together. The terms so formed are then added together.
CODE :
> expand( (a0 + a1)*(b0 + b1));
sage: var('a0','a1','b0','b1')
sage: eqn=((a0+a1)*(b0+b1))
sage: eqn.expand()
sage: var('a0','a1','b0','b1','c0','c1')
sage: eqn=((a0+a1)*(b0+b1)*(c0+c1))
sage: eqn.expand()
sage: x=var('x')
sage: eqn=((x^0+x^1)^3)
sage: eqn.expand()
How about in the expansion of (x0 +x1) (x0 +x1) (x0 +x1) (x0 +x1) ?
Answer:sage: var('x0','x1')
(x0, x1)
sage: eqn=(x0+x1)(x0+x1)(x0+x1)(x0+x1)
sage: eqn.expand()
x0 + 4*x1
If we formally make an analogy to binary sequences in the comparable expansions we will have explained. The
Binomial Theorem. The coefficient of xk in the expansion of (1 +x)n is C(n, k).
ANSWER:
sage: S=Subsets([1,2,3,4,5],3,submultiset=true);
sage: S.list()
[[1, 2, 3],
[1, 2, 4],
[1, 2, 5],
[1, 3, 4],
[1, 3, 5],
[1, 4, 5],
[2, 3, 4],
[2, 3, 5],
[2, 4, 5],
[3, 4, 5]]
sage: S=Subsets([1,2,3,4,5],2,submultiset=true);
sage: S.list()
[[1, 2],
[1, 3],
[1, 4],
[1, 5],
[2, 3],
[2, 4],
[2, 5],
[3, 4],
[3, 5],
[4, 5]]
> choose([1,2,3,4,5],n);
Example 3: How many 4-element subsets of the set {1,2,3,4,5,6} are there?
S=Subsets([1,2,3,4,5,6],4,submultiset=true);
sage: S.list()
[[1, 2, 3, 4],
[1, 2, 3, 5],
[1, 2, 3, 6],
[1, 2, 4, 5],
[1, 2, 4, 6],
[1, 2, 5, 6],
[1, 3, 4, 5],
[1, 3, 4, 6],
[1, 3, 5, 6],
[1, 4, 5, 6],
[2, 3, 4, 5],
[2, 3, 4, 6],
[2, 3, 5, 6],
[2, 4, 5, 6],
[3, 4, 5, 6]]
Claim: The following two questions have the same answer.
o How many ways are there to flip 3 heads in 5 tosses of a
coin?
ANSWER:
S=Subsets([1,2,3,4,5],3,submultiset=true);
sage: S.list()
[[1, 2, 3],
[1, 2, 4],
[1, 2, 5],
[1, 3, 4],
[1, 3, 5],
[1, 4, 5],
[2, 3, 4],
[2, 3, 5],
[2, 4, 5],
[3, 4, 5]]
command is
> permute([H,H,H,T,T],5);
ANSWER:
permute(H,H,H,T,T)
sage: Arrangements(["H","H","H","T","T"],5).list()
sage: mset=["H","H","H","T","T"]
sage: Arrangements(mset,5).cardinality()
OUTPUT :
EXPERIMENT 16
OBJECTIVE: Permutation and Combination operations using SageMath tool.
BRIEF THEORY : Permutation and combination are the ways to represent a group of objects by selecting
them in a set and forming subsets. It defines the various ways to arrange a certain group of data. When we select the
data or objects from a certain group, it is said to be permutations, whereas the order in which they are represented is
called combination. Both concepts are very important in Mathematics.
Permutation relates to the act of arranging all the members of a set into some sequence or order. In other words, if
the set is already ordered, then the rearranging of its elements is called the process of permuting.
Permutations occur, in more or less prominent ways, in almost every area of mathematics.The combination is a way
of selecting items from a collection, such that (unlike permutations) the order of selection does not matter. In
smaller cases, it is possible to count the number of combinations.
A) COMBINATIONS
Fig-1
B) PERMUTATION
Fig-2
EXPERIMENT -17
OBJECTIVE : Difference between structures, permutations and sets.
SOFTWARE USED : SageMath Tool
BRIEF THEORY : In this lab we will explore the difference between the structures permutations and sets. We
will look at formulas for counting each type of object, and then we will try some more sophisticated investigations
using the computers.
In this lab we will explore the difference between the structures permutations and sets. We will look at formulas for
counting each type of object, and then we will try some more sophisticated investigations using the computers. First
execute the following command in order to load some helpful Discrete Mathematics Maple commands. >
with(combinat); Now compare the output of the following commands. How much longer is the first output list than
the second? Think of how you would explain this precise difference to a classmate.
> permute(5,3);
> choose(5,3);
Technically, one of the above commands lists permutations and the other lists sets. Since Maple uses the square
brackets [ and ] for the output in both, it is not obvious which is which. Can you tell based on whether order matters
or not? In the text, we use the notation P(5,3) for the number of permutations of length 3 with entries taken from
{1,2,3,4,5}, and the notation C(5,3) for the number of sets of size 3 with entries taken from {1,2,3,4,5}. (In other
words, C(5,3) counts 3-element subsets of {1,2,3,4,5}.)
Using the nops command (remember how it tells you how many things are in a long list), compare P(7, 3) and C(7,
3). Is this what you expected? We can now write (as we do in the text) formulas for P and C. We have already
talked about the theoretical basis for these in the last class, but now we want to be sure we understand what P and C
are counting and how they are related. The following functions are not built into Maple so you will have to define
them before you ever use them.
CODE :
> P := proc(n, k)
RETURN(product(n-j, j=0..(k-1)))
end;
> C := proc(n, k) RETURN(P(n,k)/k!) end;
> P(5, 3);
> C(5, 3);
Example:
Pascal's Triangle
We can make a nice table of values by placing C(n, k) in Row n, Entry k of our table. Since we have to allow for
values like C(5, 0), we will think of rows starting with "Entry 0" instead of "Entry 1," and in the same spirit, we will
call the top row, Row 0 rather than Row 1. In this fashion, the values in the first three rows of our table will come
from the following:
> C(0,0);
> C(1,0);
> C(1,1);
> C(2,0);
> C(2,1);
> C(2,2);
These can be filled in our table in a triangular shape as shown below. This is the first part of what is known as
Pascal's Triangle.
1
1 1
1 2 1
EXPERIMENT 18
OBJECTIVE : Implementation of recursive counting technique.
SOFTWARE USED : SageMath Tool
BRIEF THEORY : In this lab we will look at some recursive counting techniques and implement them with
recursive maple programs.
2) Fill in the remaining k-1 entries with a permutation of length k-1 using entries from other n-1 elements of the set.
There are clearly n choices for the first step and by definition of p, there are p(n-1, k-1) way to do the second step.
Hence by the product rule, there are n*p(n-1,k-1)
Permutations generated by the algorithm. Therefore this is the same thing as p(n,k).
Notice that this relationship does not help if k=0, so it is also necessary to specify that p(n,0)=1 for this recursive
formula to make sense.
> p: proc(n,k)
Option remember:
If(k=0) then
Return(1)
If;
Return(n*p(n-1,k-1));
End;
A) Compare p(6,2) orp(10,3) with the result we would get using our regular formula from the text.
B) We saw before that numbers like p(2,5) make sense to ask about they simply have a value of 0 i.e. there are 0
permutations of length 5 with entries taken from {1,2}.
EXPERIMENT 19
OBJECTVE : The birthday problem.
Calculating the probability - the goal is to compute p(a) the probability that at least two people in the room have the
same birthday.
However, it is simpler to calculate p(a') the probability that no two people in the room have the same birthday.
P(a) = 1-p(a')
Now 23 is the minimum number of people necessary to have a p(a) that is greater than 50%
The event that all 23 people have different birthdays is the same as the event that person 2 does not have the same
birthday as person 1 and the person 3 does not have the same birthday as either person 1 or person 2 and so on. Let
these events respectively be called "event 2", "event 3" and so on (we can also add event1= person 1 having a
birthday i.e. probability is 1).
The probability of event 2= 364/365, (person 2 may have any birthday other than the birthday other than the
birthday of person 1).
=(1/365)23 x (365x364x363x…..x343)
P(a’) = 0.492703
= [365x364x……..x(365-n+1)]/365n
P(n)= 1-p’(n)
The following script produces a random set of k values from a set of n objects. We’ll use it to simulate the birthday
for each of the 32 students.
CODE :
Randlist:= proc(n,k)
Local i;
Randomize();
Return(map(rand(1…….n), [req(i,i=1………k)]))
End proc;
EXPERIMENT 20
OBJECTIVE : Baseball Binomial Probability
SOFTWARE USED : SageMath
BRIEF THEORY : During regular season, the Boston Red Sox and Cleveland Indians played against each
other 7 times. The Red Sox won 6 of these games.
Expectations are developed based on previous performance. Overlooking the other variables affecting baseball, the
wins/losses information may be used to develop probabilities associated with winning and losing.
1. Using the wins/losses information above, determine thefollowing:
a) p = P(Red Sox win)=