Professional Documents
Culture Documents
DS Practical File
DS Practical File
: 20/ CS/21
Discrete Structure Practical File
Program – 1
//Program - 1 : Write a program to accept a set, find its cardinality and find its
power set.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
char Set[50],element;
int i,j,k,counter=0;
cout<<"\n\n Enter the elements for the set (press '*' to stop) :\n";
i=-1;
do
{
i++;
cout<<" ";
cin>>Set[i];
}while(Set[i]!='*' && (i<50));
counter = i ;
for(i=0;i<counter;i++)
{
for(j=i+1;j<counter;j++)
{
if(Set[i]==Set[j])
{
for(k=j;k<counter;k++)
{
Set[k]=Set[k+1];
}
counter--;
j--;
}
}
}
cout<<"\n The set is : { ";
for(i=0;i<counter;i++)
{
cout<<Set[i]<<" , ";
}
cout<<"\b\b}";
cout<<"\n\n The cardinality of set is : "<<counter;
cout<<"\n\n Enter an element : ";
cin>>element;
if(check_member(Set,counter,element)==true)
cout<<"\n "<<element<<" is present in the set.\n\n";
else
cout<<"\n "<<element<<" is not present in the set.\n\n";
for(int j=0;j<counter;j++)
{
if(binary[j]==1)
cout<<s[j]<<",";
}
cout<<"\b";
cout<<"} , ";
}
cout<<"\b\b }";
}
Output
Program – 2
/* Program 2 : Create a class SET and take 2 sets as inputs from user to perform
the following set operations :
#include<iostream>
#include<conio.h>
using namespace std;
class SET
{
char ar[100];
int Size;
public:
void Accept();
void Display();
void Normalize();
void Union(SET s);
void Inter(SET s);
void Complement(SET U);
bool is_subset_of(SET s);
SET Set_diff(SET s);
void Cartesian(SET s);
};
void SET::Normalize()
{
int i,j,k;
for(i=0;i<Size;i++)
{
for(j=i+1;j<Size;j++)
{
if(ar[i]==ar[j])
{
for(k=j;k<Size;k++)
{
ar[k]=ar[k+1];
}
Size--;
j--;
}
}
}
}
void SET::Display()
{
cout<<"\n\n { ";
for(int i=0;i<Size;i++)
{
cout<<" "<<ar[i]<<" , ";
}
cout<<"\b\b } ";
}
void SET::Accept()
{
int i,j,k;
i=-1;
do
{
i++;
cout<<" ";
cin>>ar[i];
}while(ar[i]!='*' && (i<100));
Size = i ;
Normalize();
cout<<"\n\n Set entered is : ";
Display();
}
void SET::Union(SET s)
{
SET uni;
int i,j,k=0,l,f=1;
for(i=0;i<Size;i++)
{
uni.ar[k++]=ar[i];
}
for(j=0;j<s.Size;j++)
{
uni.ar[k++]=s.ar[j];
}
uni.Size=k;
uni.Normalize();
uni.Display();
}
void SET::Inter(SET s)
{
int i,j,k=0;
SET Inter;
for(i=0;i<Size;i++)
{
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
Inter.ar[k++]=ar[i];
}
}
Inter.Size=k;
Inter.Normalize();
Inter.Display();
}
bool SET::is_subset_of(SET s)
{
int i,j;
bool f;
for(i=0;i<Size;i++)
{
f=false;
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
{
f=true;
}
}
if(f==false)
break;
}
return f;
}
void SET::Complement(SET U)
{
SET comple;
int i,j,k=0;
bool f;
for(i=0;i<U.Size;i++)
{
f=true;
for(j=0;j<Size;j++)
{
if(U.ar[i]==ar[j])
f=false;
}
if(f==true)
comple.ar[k++]=U.ar[i];
}
comple.Size=k;
comple.Display();
}
SET SET::Set_diff(SET s)
{
SET diff;
int i,j,k=0;
bool f;
for(i=0;i<Size;i++)
{
f=true;
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
f=false;
}
if(f==true)
diff.ar[k++]=ar[i];
}
diff.Size=k;
return diff;
}
void SET::Cartesian(SET s)
{
int i,j;
cout<<"\n\n { ";
for(i=0;i<Size;i++)
{
for(j=0;j<s.Size;j++)
{
cout<<" ("<<ar[i]<<" , "<<s.ar[j]<<") , ";
}
}
cout<<"\b\b } ";
}
int main()
{
SET s1 , s2 , U , diff1 , diff2;
int opt;
cout<<"\n\n For set 1 : \n";
s1.Accept();
cout<<"\n\n For set 2 : \n";
s2.Accept();
cout<<"\n\n Enter universal set : \n";
U.Accept();
for(int i=0;i<80;i++)cout<<"\n";
do
{
cout<<"\n\n\t\t\t\t\tMENU\n\n\n";
cout<<"\n\n 1. Enter array again.\n\n 2. Display both the arrays.\n\n 3. Check for subset.\n\
n 4. Find union.";
cout<<"\n\n 5. Find intersection.\n\n 6. Find Set difference.\n\n 7. Find Symmetric
difference.\n\n 8. Find Complement.";
cout<<"\n\n 9. Find cartesian product. \n\n 10. Exit.\n\n Enter your option : ";
cin>>opt;
if(opt<1||opt>10)
cout<<"\n\n Invalid option.\n\n";
switch(opt)
{
case 1 :
cout<<"\n\n For set 1 : \n";
s1.Accept();
cout<<"\n\n For set 2 : \n";
s2.Accept();
cout<<"\n\n Enter universal set : \n";
U.Accept();
break;
case 2 :
cout<<"\n\n Set 1 is : ";
s1.Display();
cout<<"\n\n Set 2 is : ";
s2.Display();
break;
case 3 :
if(s1.is_subset_of(s2))
cout<<"\n\n Set 1 is a subset of Set 2. ";
else
cout<<"\n\n Set 1 is not a subset of Set 2. ";
if(s2.is_subset_of(s1))
cout<<"\n\n Set 2 is a subset of Set 1. ";
else
cout<<"\n\n Set 2 is not a subset of Set 1. ";
break;
case 4 :
cout<<"\n\n Union of both the sets is : ";
s1.Union(s2);
break;
case 5 :
cout<<"\n\n Intersection of both the sets is : ";
s1.Inter(s2);
break;
case 6 :
cout<<"\n\n Set 1 - Set 2 is : ";
diff1=s1.Set_diff(s2);
diff1.Display();
cout<<"\n\n Set 2 - Set 1 is : ";
diff2=s2.Set_diff(s1);
diff2.Display();
break;
case 7 :
cout<<"\n\n Symmetric difference of both the sets is : ";
diff1.Union(diff2);
break;
case 8 :
cout<<"\n\n Complement of Set 1 is : ";
s1.Complement(U);
cout<<"\n\n Complement of Set 2 is : ";
s2.Complement(U);
break;
case 9 :
cout<<"\n\n Cartesian product (i.e. s1 x s2 )of both the sets is : ";
s1.Cartesian(s2);
}
}while(opt!=10);
return 0;
}
Output
Program – 3 & 4
Program - 4 : Use the functions defined in Ques 3 to check whether the given
relation is :
a) Equivalent, or
b) Partial order relation, or
c) None. */
#include<iostream>
#include<conio.h>
using namespace std;
class RELATION
{
int R[100][100],n;
public:
void accept();
void display();
int check_reflexive();
int check_symmetric();
int check_anti_symmetric();
int check_transitive();
};
void RELATION::accept()
{
cout<<"\n Enter the number of elements in the set on which relation is defined : ";
cin>>n;
cout<<"\n\n Enter the relation in matrix notation (0/1) : ";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<"\n\n Enter element (0/1) : ";
cin>>R[i][j];
void RELATION::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<R[i][j];
}
}
int RELATION::check_reflexive()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==j)&&(R[i][j]!=1))
{
f=0;
break;
}
}
}
return f;
}
int RELATION::check_symmetric()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((R[i][j]==1)&&R[j][i]!=1)
{
f=0;
break;
}
}
}
return f;
}
int RELATION::check_anti_symmetric()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i!=j)&&(R[i][j]==1)&&(R[j][i]==1))
{
f=0;
break;
}
}
}
return f;
}
int RELATION::check_transitive()
{
int i,j,k,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if((R[i][j]==1)&&(R[j][k]==1)&&(R[i][k]!=1))
{
f=0;
break;
}
}
}
}
return f;
}
int main()
{
RELATION r;
r.accept();
if(r.check_reflexive()==1)
cout<<"\n\n Entered relation is reflexive !! " ;
else
cout<<"\n\n Entered relation is not reflexive !! " ;
if(r.check_symmetric()==1)
cout<<"\n\n Entered relation is symmetric !! " ;
else
cout<<"\n\n Entered relation is not symmetric !! " ;
if(r.check_anti_symmetric()==1)
cout<<"\n\n Entered relation is anti-symmetric !! " ;
else
cout<<"\n\n Entered relation is not anti-symmetric !! " ;
if(r.check_transitive()==1)
cout<<"\n\n Entered relation is transitive !! " ;
else
cout<<"\n\n Entered relation is not transitive !! " ;
if((r.check_reflexive()==1)&&(r.check_symmetric()==1)&&(r.check_transitive()==1))
cout<<"\n\n Entered relation is an equivalence relation !!";
else if((r.check_reflexive()==1)&&(r.check_anti_symmetric()==1)&&(r.check_transitive()==1))
cout<<"\n\n Entered relation is a partial ordering relation relation !!";
else
cout<<"\n\n Entered relation is neither equivalence nor partial ordering relation !!";
getch();
return 0;
}
Output
Program – 5
/* Program -5 : Write a program to implement bubble sort. Find the number of
comparisons during each pass and display the intermediate result. */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
bub_sort(ar,s);
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
bub_sort(ar,s);
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
insert_sort(ar,s);
getch();
return 0;
}
Output
Program – 7
/* Program - 7 : Write a program that generates all the permutations of a given set
of
digits, with or without repetition. */
#include<iostream>
#include<conio.h>
#include<ctype.h>
using namespace std;
int main()
{
char nums[100];
int i,n;
cout<<"\n\n Enter the number of digits whose permutations you want to find (less than 100) :
";
cin>>n;
while(n>100)
{
cout<<"\n\n Please enter a number less than 100 : ";
cin>>n;
}
for(i=0;i<n;i++)
{
cout<<"\n\n Enter digit "<<i+1<<" : ";
cin>>nums[i];
while(!isdigit(nums[i]))
{
cout<<"\n\n Please enter a digit : ";
cin>>nums[i];
}
}
nums[i]='\0';
cout<<"\n\n Entered set of digits is : { ";
for(i=0;i<n;i++)
cout<<nums[i]<<" , ";
cout<<"\b\b } ";
cout<<"\n\n All permutations of { ";
for(i=0;i<n;i++)
cout<<nums[i]<<" , ";
cout<<"\b\b } are :-\n\n";
permutation(nums,0,n-1);
getch();
return 0;
}
void permutation(char nums[],int l,int r) //l for left and r for right.
{
int i;
if(l==r)
cout<<nums<<"\n\n";
else
{
for(i=l ; i<=r ; i++)
{
swap_nos(nums[l],nums[i]);
permutation(nums,l+1,r);
swap_nos(nums[l],nums[i]);
}
}
}
Program – 8
/* Program - 8 : Write a program to calculate permutation and combination for an
input value n and r using recursive formula of
P(n,r) and C(n,r). */
#include<iostream>
#include<conio.h>
using namespace std;
double factorial(double a);
int main()
{
double n,r,p,c;
cout<<"\n\n Enter the value for n : ";
cin>>n;
do
{
cout<<"\n\n Enter the value for r (less than or equal to n): ";
cin>>r;
}while(r>n);
cout<<"\n\n Formula for P(n,r) is n!\n\t\t\t------\n\t\t\t(n-r)!";
cout<<"\n\n Formula for C(n,r) is n!\n\t\t\t--------\n\t\t\tr!(n-r)!";
p=factorial(n)/factorial(n-r);
c=factorial(n)/(factorial(r)*factorial(n-r));
cout<<"\n\n Permutation for n and r , P("<<n<<","<<r<<") is "<<p;
cout<<"\n\n Combination for n and r , C("<<n<<","<<r<<") is "<<c<<"\n\n";
getch();
return 0;
}
double factorial(double a)
{
if(a==0||a==1)
return 1;
return a*factorial(a-1);
}
Output
Program 9
//Program – 9: For any number n write a program to list all the solutions of the
equaqtion x1+x2+x3+…+xn=C where C is a constant and x1 x2 x3 … x4 are non
negative integers using brute force integer.
#include<iostream> using namespace std;
else
int main() {
int n ,r;
cout<<"\nx1+x2+x3+---+xn=c";
cout<<comb(n+r-1,r); }
Output
Program – 10
/* Program - 10 : WAP to print the truth table of following logical operations for
variables
x and y :
Conjunction , Disjunction , Exclusive OR , Conditional , Bi-conditional , Exclusive
NOR ,
Negation , NAND , NOR.
*/
#include<iostream>
#include<conio.h>
using namespace std;
#include<iostream>
#include<conio.h>
#include<math.h>
int main()
{
int deg, coeff[100], i, n, ans=0 ;
do
{
cout<<"\n\n Enter the degree of polynomial you want to have (less than 100) : ";
cin>>deg;
}while(deg<0||deg>100);
for(i=deg;i>=0;i--)
{
cout<<"\n\n Enter the coefficient for degree "<<i<<" : ";
cin>>coeff[i];
}
cout<<"\n\n Polynomial entered is f(x) = ";
for(i=deg;i>=0;i--)
{
if(i==0)
cout<<coeff[i];
else if(i==1)
cout<<coeff[i]<<".x";
else
cout<<coeff[i]<<".x^"<<i;
if(i!=0)
cout<<" + ";
}
cout<<"\n\n Enter the value for n for which you want to find f(n) : ";
cin>>n;
for(i=deg ; i>=0 ; i--)
{
ans += pow(n,i) * coeff[i] ;
}
cout<<"\n\n f("<<n<<") = "<<ans<<"\n\n";
return 0;
}
Output
Program –
12
/* Program - 12 : Write a program to represent graphs using the adjacency
matrices
and check if it is a complete graph. */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g[10][10],n,i,j;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter either 0 or 1 : ";
cin>>g[i][j];
while(g[i][j]!=0 && g[i][j]!=1)
{
cout<<"\n\n Please enter either 0 or 1 : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
int ans = check_complete(g,n);
if(ans==1)
cout<<"\n\n Entered graph is a complete graph.";
else
cout<<"\n\n Entered graph is not a complete graph.";
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g[10][10],n,i,j;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter either 0 or 1 : ";
cin>>g[i][j];
while(g[i][j]!=0 && g[i][j]!=1)
{
cout<<"\n\n Please enter either 0 or 1 : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
calc_degree(g,n);
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
Program – 15
/* Program - 15 : Given an adjacency matrix of a graph, write a program to check
whether a
given set of vertices {v1,v2,v3,...,vk} forms an Euler path/Euler circuit. */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g[10][10],n,i,j,flag,ans;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter the number of edges directed from vertex "<<i+1<<" to vertex
"<<j+1<<" : ";
cin>>g[i][j];
while(i==j && g[i][j]>1)
{
cout<<"\n\n Sorry!! Only one self-loop allowed.";
cout<<"\n\n Enter again : ";
cin>>g[i][j];
}
while(g[i][j]<0)
{
cout<<"\n\n Please enter a positive integer : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
flag=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(g[i][j]!=g[j][i])
flag=0;
}
}
if(flag==0) //graph is directed.
{
ans = check_euler_circuit_directed(g,n);
if(ans==1)
cout<<"\n\n Entered directed graph has an Euler circuit.";
else
cout<<"\n\n Entered directed graph does not have an Euler circuit.";
ans = check_euler_path_directed(g,n);
if(ans==1)
cout<<"\n\n Entered directed graph has an Euler path.";
else
cout<<"\n\n Entered directed graph does not have an Euler path.\n\n";
}
if(flag==1) //graph is undirected.
{
ans = check_euler_circuit_undirected(g,n);
if(ans==1)
cout<<"\n\n Entered undirected graph has an Euler circuit.";
else
cout<<"\n\n Entered undirected graph does not have an Euler circuit.";
ans = check_euler_path_undirected(g,n);
if(ans==1)
cout<<"\n\n Entered undirected graph has an Euler path.";
else
cout<<"\n\n Entered undirected graph does not have an Euler path.\n\n";
}
getch();
return 0;
}
return flag;
}
int main()
{
int i,m,l;
cout<<"\n\n Enter the value of m : ";
cin>>m;
cout<<"\n\n Enter the value of i : ";
cin>>i;
l=(m-1)*i+1;
cout<<"\n\n The number of leaf nodes is "<<l;
getch();
return 0;
}
Output