discrete math practical
discrete math practical
class SET
{
private:
int i,j;
public:
void Subset(int *arrA, int sizeA, int *arrB,
int sizeB)
{
int c=0;
if(c != sizeA)
cout << "SET A is not a subset of SET B" <<
endl;
else
cout << "SET A is a subset of SET B" <<
endl;
int c1=0;
if(c != sizeB)
cout << "SET B is not a subset of SET A" <<
endl;
else
cout << "SET B is a subset of SET A" <<
endl;
cout << "-----------------------------------------
--------" << endl;
}
else
continue;
}
}
if(y != 0)
{
cout << "Intersection of two sets is : {";
else
cout << "No intersection found";
int AC[sizeU],p=0,c=0;
else
continue;
}
if(c == 0)
{
AC[p]=U[i];
p++;
}
c=0;
}
cout << endl;
int BC[sizeU],q=0,ctr=0;
else
continue;
}
if(ctr == 0)
{
BC[q]=U[i];
q++;
}
ctr=0;
}
else
continue;
}
if(ctr == 0)
{
ABDif[q]=setA[i];
q++;
}
ctr=0;
}
else
continue;
}
if(c == 0)
{
BADif[p]=setB[i];
p++;
}
c=0;
}
int uSize=q+p;
int symDif[uSize];
int x=0,y=0;
sizeAB=sizeA*sizeB;
sizeBA=sizeAB;
int AB[sizeAB*2],BA[sizeBA*2];
if(i%2 != 0)
cout << ")";
}
cout << " }" << endl;
if(i%2 != 0)
cout << ")";
}
cout << " }" << endl;
int main()
{
cout << endl;
int i,sizeA,sizeB;
int arrA[sizeA];
SET ob;
OUTPUT
QUES2- Create a class RELATION, use
Matrix notation to represent a relation.
Include functions to
check if a relation is reflexive,
Symmetric, Anti-symmetric and
Transitive. Write a Program
to use this class.
CODE- #include<iostream>
#include<stdio.h>
#include<conio.h>
class RELATION
{
private:
int
i,j,k,x,y,z,ctr,iA,iB,nA,nR,*A,*R,**RM,*
*T;
public:
void empty();
int inputSet();
void inputRelation();
void printSet();
void printRelation();
void Matrix();
int reflexive();
int symmetric();
bool antiSymmetric();
bool transitive();
};
void RELATION::empty()
{
cout << "Set A is empty\n";
printSet();
cout << "Set A has no member.";
cout << "\nHence, relation R is
empty.\n";
nR = 0;
printRelation();
cout << "Therefore, no matrix
notation.";
cout << "\nRelation R is NOT
REFLEXIVE.";
symmetric();
antiSymmetric();
transitive();
}
int RELATION::inputSet()
{
cout << "Enter the size of SET A : ";
cin >> nA;
A = new int[nA];
if(nA == 0)
return 1;
void RELATION::inputRelation()
{
cout << "Enter the no of relations (R on
A) : ";
cin >> nR;
R = new int[nR * 2];
cout << "Enter the relations in pair :\n";
for(i=0; i<nR*2; i++)
cin >> R[i];
}
void RELATION::printSet()
{
cout << "A = {";
for(i=0; i<nA; i++)
cout << A[i] << " ";
cout << "}\n";
}
void RELATION::printRelation()
{
cout << "R = {";
for(i=0; i<nR*2; i++)
{
if(i%2 == 0)
cout << "(";
cout << R[i] << " ";
if(i%2 != 0)
cout << ")";
}
cout << "}\n";
}
void RELATION::Matrix()
{
cout << "\nMATRIX NOTATION\n\n";
RM = new int *[nA];
for(i=0; i<nA; i++)
RM[i]=new int[nA];
for(i=0; i<nA; i++)
{
for(j=0; j<nA; j++)
{
RM[i][j]=0;
}
}
int RELATION::reflexive()
{
x=0;
for(i=0; i<nA; i++)
{
if(RM[i][i] == 1)
x++;
}
if(x == nA)
{
cout << "\nRelation R is REFLEXIVE.";
return x = 0;
}
else
{
cout << "\nRelation R is NOT
REFLEXIVE.";
return x = 1;
}
}
int RELATION::symmetric()
{
ctr = 0;
else
{
ctr++;
break;
}
}
}
if(ctr != 0)
cout << "\nRelation R is NOT
SYMMETRIC.";
else
cout << "\nRelation R is SYMMETRIC.";
return ctr;
}
bool RELATION::antiSymmetric()
{
bool flag = true;
else
{
flag = false;
}
}
}
if(flag != true)
cout << "\nRelation R is NOT ANTI-
SYMMETRIC.";
else
cout << "\nRelation R is ANTI-
SYMMETRIC.";
return flag;
}
bool RELATION::transitive()
{
bool flag = true;
else
flag = false;
}
}
}
if(flag != true)
cout << "\nRelation R is NOT
TRANSITIVE.";
else
cout << "\nRelation R is TRANSITIVE.";
return flag;
}
int main()
{
int p = 0;
RELATION ob;
p = ob.inputSet();
if(p == 1)
ob.empty();
else
{
ob.printSet();
ob.inputRelation();
ob.printRelation();
ob.Matrix();
ob.reflexive();
ob.symmetric();
ob.antiSymmetric();
ob.transitive();
}
return 0;
}
OUTPUT-
QUES3- Write a Program that
generates all the permutations of a
given set of digits, with or without
repetition.
CODE- #include<iostream>
#include<stdio.h>
#include<conio.h>
#define MAX_DIM 100
int main()
{
int size;
char ch;
switch(ch)
{
case 'Y':
withRepetition(array, size);
break;
case 'N':
withoutRepetition(array, size);
break;
default:
cout << "\nWrong Choice";
}
return 0;
}
else
{
printWithRepetition(array, size, data,
last, index+1);
}
}
}
void withoutRepetition(int* array, int
size)
{
printWithoutRepetition(array, size, 0,
size-1);
cout << endl;
}
else
{
for(int i=start; i<end+1; i++)
{
swap(array[start], array[i]);
printWithoutRepetition(array, size,
start+1, end);
swap(array[start], array[i]);
}
}
}
CODE- #include<iostream>
if(sum == C)
{
cout << "[ ";
for(int j=0; j<index+1; j++)
cout << data[j] << " ";
cout << "] ";
counter++;
}
}
else
bruteForce(arr, size, data, last,
index+1, C, counter);
}
}
OUTPUT-
QUES5- Write a Program to evaluate a
polynomial function. (For example
store f(x) = 4n2 + 2n + 9 in an array and
for a given value of n, say n = 5,
compute the value of f(n))
CODE- #include<iostream>
#include<stdio.h>
#include<conio.h>
#include<cmath>
using namespace std;
int i;
class FUNCTION
{
private:
int n;
double *coefficient;
double *exponential;
public:
void input();
void display();
double evaluate(double);
};
void FUNCTION::input()
{
int n;
cout << "\nEnter the number of terms:
";
cin >> this->n;
void FUNCTION::display()
{
for(i=0; i<this->n; i++)
{
if(coefficient[i] >= 0)
cout << " + ";
else
cout << " - ";
cout << abs (coefficient[i]);
if(exponential[i] != 0)
cout << "(x^" << exponential[i] << ")";
}
}
double FUNCTION::evaluate(double x)
{
double result = 0.0;
for(i=0; i<this->n; i++)
{
result += coefficient[i] * (pow(x,
exponential[i]));
}
return result;
}
int main()
{
double x;
FUNCTION ob;
ob.input();
cout << "Function is f(x) = ";
ob.display();
cout << "\nEnter the value of x: ";
cin >> x;
cout << "\nValue of f(" << x << "): " <<
ob.evaluate(x) << endl;
return 0;
}
OUTPUT-
QUES6- Write a Program to check if a
given graph is a complete graph.
Represent the graph using the
Adjacency Matrix representation.
CODE- #include<iostream>
return count[src][dest][len];
}
int main()
{
int v;
cout << "\nEnter the nunber of
vertices: ";
cin >> v;
int matrix[100][100];
cout << "Enter the adjacency
matrix:\n";
for(int i=0; i<v; i++)
for(int j=0; j<v; j++)
cin >> matrix[i][j];
int len;
cout << "Enter the path lemgth: ";
cin >> len;
OUTPUT-
GRAPH-
QUES8- Write a Program to accept a
directed graph G and compute the in-
degree and outdegree of each vertex.
CODE- #include<iostream>
#include<cmath>
int main()
{
int v, nin, nout, inver, outver;
cout << "\nEnter the no. of vertices: ";
cin >> v;
int matrix[v][v];
for(int i=0; i<v; i++)
for(int j=0; j<v; j++)
matrix[i][j] = 0;
if(matrix[i][j] == -1)
indegree += matrix[i][j];
}
return 0;
}
OUTPUT-
GRAPH-
QUES7-. Write a Program to check if a
given graph is a complete graph.
Represent the graph using the
Adjacency List representation.
class AdjNode:
def __init__(self, value):
self.vertex = value
self.next = None
class Graph:
def __init__(self, num):
self.V = num
self.graph = [None] * self.V
# Add edges
def add_edge(self, s, d):
node = AdjNode(d)
node.next = self.graph[s]
self.graph[s] = node
node = AdjNode(s)
node.next = self.graph[d]
self.graph[d] = node
# Print the graph
def print_agraph(self):
for i in range(self.V):
print("Vertex " + str(i) + ":",
end="")
temp = self.graph[i]
while temp:
print(" ->
{}".format(temp.vertex), end="")
temp = temp.next
print(" \n")
if __name__ == "__main__":
V=5
# Create graph and edges
graph = Graph(V)
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(0, 3)
graph.add_edge(1, 2)
graph.print_agraph()
OUTPUT-
GRAPH-
INTRODUCTION
Name-Sumit Kumar
ROLL. NO- 23020570053
COURSE- Bsc.(hons.)
computer science
SEMESTER-
SUBJECT- DISCRETE
MATHEMATICAL
STRUCTURES
SUBMITTED TO- Dr. Aakash