Sunteți pe pagina 1din 7

Discrete Structures MCC03

Assignment

1. Write a Program for the Addition and Subtraction of


two sets
#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespace std; 
vector<int> addSetsMinkowski(vector<int>& set1,vector<int>& set2){ 
//Assuming the vectors are sets i.e they don't have any repeated element 
int size1=set1.size(),size2=set2.size(); 
vector<int> result(size1*size2); 
int i=0,j=0; 
for(i=0;i<size1;i++){ 
for(j=0;j<size2;j++){ 
result.push_back(set1[i]+set2[j]); 


return result; 

vector<int> subtractSets(vector<int>& set1,vector<int> &set2){ 
//Assuming the vectors are sets i.e they don't have any repeating element 
//This function will calculate set1-set2 
int size1=set1.size(),size2=set2.size(); 
vector<int> result; 
int i=0,j=0; 
sort(set1.begin(),set1.end()); 
sort(set2.begin(),set2.end()); 
while(i<size1 && j<size2){ 
if(set2[j]>set1[i]){ 
result.push_back(set1[i]); 
i++; 

else if(set2[j]<set1[i]){ 
j++; 

else{ 
i++; 
j++; 


while(i<size1){ 
result.push_back(set1[i]); 
i++; 

return result; 

 
 

2. Write a program for checking if a given relation is


Equivalence Relation or not

#include<iostream> 
#include<vector> 
using namespace std; 
 
bool checkEquivalence(vector<pair<int,int>> &relation,vector<int> &domain){ 
vector<vector<pair<int,int>>> sum_hash(10); 
bool reflexive=true; 
bool symmetric=true; 
bool transitive=true; 
int refcount=0; 
for(int i=0;i<relation.size();i++){ 
if(relation[i].first==relation[i].second) refcount++; 
else sum_hash[(relation[i].first+relation[i].second)%10].push_back(relation[i]); 

if(refcount!=domain.size()) reflexive=false; 
for(int i=0;i<10;i++){ 
if(sum_hash[i].size()%2==1) symmetric=false; 

if(!reflexive&&symmetric) transitive=false; 
else{ 
for(int i=0;i<relation.size();i++){ 
if(relation[i].first==relation[i].second) continue; 
else{ 
for(int j=0;j<relation.size();j++){ 
if(relation[i]==relation[j]) continue; 
else if(relation[i].second==relation[j].first){ 
bool flag=false; 
for(int k=0;k<relation.size();k++){ 
if(relation[k]==make_pair(relation[i].first,relation[j].second)) { 
flag=true; 
break; 


if(!flag){ 
transitive=false; 
break; 



if(!transitive) break; 



return reflexive&&symmetric&&transitive; 

 

3. Write a program for checking whether the given


logical expression is a Tautology, Contingency or
Contradiction

#include<iostream> 
#include<stack> 
using namespace std; 
#define AND(a,b) a&&b 
#define OR(a,b) a||b 
#define NOT(a) !a 
#define IMPLIES(a,b) (!a)||b 
#define DOUBLEIMPLIES(a,b) ((!a)||b)&&((!b)||a) 
 
bool evaluate(string expr,bool p,bool q){ 
stack<bool> operands; 
stack<string> operators; 
int i=0; 
while(i<expr.length()){ 
if(expr[i]=='p' || expr[i]=='q' || expr[i]=='T' || expr[i]=='F'){ 
if(expr[i]=='p'){ 
operands.push(p); 

else if (expr[i]=='q'){ 
operands.push(q); 

else if (expr[i]=='T'){ 
operands.push(true); 

   
else{ 
operands.push(false); 


else if(expr[i]=='('){ 
int bracketcount=1; 
int j=i+1; 
while(bracketcount){ 
if(expr[j]==')') bracketcount--; 
else if(expr[j]=='(') bracketcount++; 
j++; 

operands.push(evaluate(expr.substr(i+1,j-i-2),p,q)); 
i=j-1; 

else{ 
if(expr[i]=='<'){ 
operators.push("<>"); 
i++; 

else{ 
operators.push(expr.substr(i,1)); 


i++; 

bool result; 
while(!operators.empty()){ 
string op=operators.top(); 
operators.pop(); 
   
if(op=="~"){ 
bool a1=operands.top(); 
operands.pop(); 
result=(NOT(a1)); 

else{ 
   
bool a1=operands.top(); 
operands.pop(); 
bool a2=operands.top(); 
operands.pop(); 
if(op=="^"){ 
result=(AND(a2,a1)); 

else if(op=="V"){ 
result=(OR(a2,a1)); 

else if(op==">"){ 
result=(IMPLIES(a2,a1)); 

else if(op=="<>"){ 
result=(DOUBLEIMPLIES(a2,a1)); 


operands.push(result); 

return operands.top(); 

void checkLogicalExpression(string expr){ 
bool truthtable[4]; 
int k=0; 
bool tautology=true,contradiction=true; 
for(int i=0;i<=1;i++){ 
for(int j=0;j<=1;j++){ 
truthtable[k]=evaluate(expr,i,j); 
if(truthtable[k]==true){ 
contradiction=false; 

else{ 
tautology=false; 

k++; 


if(tautology){ 
cout<<"it's a tautology"<<endl; 

else if(contradiction){ 
cout<<"it's a contradiction"<<endl; 

else{ 
cout<<"it's a contingency"<<endl; 


 

4. Explain Multisets with the following operations:


a. Union
b. Intersection
c. Subtraction
d. Addition
Also write a program to perform these operations on two
multisets.
 
Multisets are unordered collections of objects where one element can occur 
mutiple times in the same multiset. 
 
a) In the union operation of two multisets, the maximum multiplicity of a 
particular element is taken. 
b) In the intersection operation of two multisets, the minimum multiplicity 
of a particular element is taken. 
c) In the subtraction operation of two multisets, the multiplicity of an 
element in the second multiset is subtracted from the first multiset, 
and if that is non-negative, it is taken, otherwise multiplicity becomes 
zero for that element. 
d) In the addition operation of two multisets, the sum of the 
multiplicities of an element in the two multisets is taken. 

#include <iostream> 
#include <vector> 
using namespace std; 
vector<int> UnionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& domain){ 
//where domain is the same for both mutisets 
vector<int> result; 
int count1=0,count2=0,k,l; 
for(int i=0;i<domain.size();i++){ 
int j=domain[i]; 
count1=0; 
count2=0; 
for(k=0;k<Mset1.size();k++){ 
if(Mset1[k]==j) count1++; 

for(k=0;k<Mset2.size();k++){ 
if(Mset2[k]==j) count2++; 

l=max(count1,count2); 
for(k=0;k<l;k++){ 
result.push_back(j); 


return result; 

vector<int> IntersectionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& 
domain){ 
//where domain is the same for both mutisets 
vector<int> result; 
int count1=0,count2=0,k,l; 
for(int i=0;i<domain.size();i++){ 
int j=domain[i]; 
count1=0; 
count2=0; 
for(k=0;k<Mset1.size();k++){ 
if(Mset1[k]==j) count1++; 

for(k=0;k<Mset2.size();k++){ 
if(Mset2[k]==j) count2++; 

l=min(count1,count2); 
for(k=0;k<l;k++){ 
result.push_back(j); 


return result; 

vector<int> AdditionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& domain){ 
//where domain is the same for both mutisets 
vector<int> result; 
int count1=0,count2=0,k,l; 
for(int i=0;i<domain.size();i++){ 
int j=domain[i]; 
count1=0; 
count2=0; 
for(k=0;k<Mset1.size();k++){ 
if(Mset1[k]==j) count1++; 

for(k=0;k<Mset2.size();k++){ 
if(Mset2[k]==j) count2++; 

l=count1+count2; 
for(k=0;k<l;k++){ 
result.push_back(j); 


return result; 

vector<int> SubtractionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& 
domain){ 
//where domain is the same for both mutisets 
vector<int> result; 
int count1=0,count2=0,k,l; 
for(int i=0;i<domain.size();i++){ 
int j=domain[i]; 
count1=0; 
count2=0; 
for(k=0;k<Mset1.size();k++){ 
if(Mset1[k]==j) count1++; 

for(k=0;k<Mset2.size();k++){ 
if(Mset2[k]==j) count2++; 

l=count1-count2; 
for(k=0;k<l;k++){ 
result.push_back(j); 


return result; 

 

S-ar putea să vă placă și