DS PRACTICAL FILE Adaya
DS PRACTICAL FILE Adaya
Write a Program to create a SET A and determine the cardinality of SET for an
input array of elements (repetition allowed) and perform the following
operations on the SET:
a) ismember (a, A): check whether an element belongs to set or not and return
value as true/false.
b) powerset(A): list all the elements of power set of A.
Code:
#include <iostream>
#include <cmath>
#include <set>
using namespace std;
int main()
{
cout << "Enter the size of the array : ";
int size;
cin >> size;
cout << "Enter the elements in the array : " << endl;
char A[size];
for(int i = 0; i < size; i++)
{
cin >> A[i];
}
Output:
PRACTICAL NO 2
Create a class SET and take two sets as input from user to perform following
SET Operations:
a) Subset: Check whether one set is a subset of other or not.
b) Union and Intersection of two Sets.
c) Complement: Assume Universal Set as per the input elements from the user.
d) Set Difference and Symmetric Difference between two SETS
e) Cartesian Product of Sets.
Code:
#include <iostream>
using namespace std;
class SET
{
char Arr[50];
int size;
public :
void getData(char x);
void print();
void remove_dup();
void Union(SET obj);
void Intersection(SET obj);
void Complement(SET obj, char x);
bool isSubset(SET obj);
SET set_diff(SET obj);
void cart_pro(SET obj, char x, char y);
};
void SET::getData(char x)
{
cout << "Enter the size of the array " << x << " : " << endl;
cin >> size;
cout << "Enter elements in the set " << x << " : " << endl;
for(int i = 0; i < size; i++)
{
cin >> Arr[i];
}
remove_dup();
print();
}
void SET::print()
{
cout << "{ ";
for(int i = 0; i < size; i++)
{
cout << Arr[i];
if(i < size-1)
{
cout << ", ";
}
}
cout << " }" << endl;
cout << endl ;
}
void SET::remove_dup()
{
for(int i = 0; i < size; i++)
{
for(int j = i+1; j < size; )
{
if(Arr[i] == Arr[j])
{
for(int k = j; k < size; k++)
{
Arr[k] = Arr[k+1];
}
size--;
}
else
{
j++;
}
}
}
}
}
if(f == true)
{
U.Arr[k++] = obj.Arr[i];
}
}
U.size = k;
cout << "Complement of Set " << x << " : ";
U.print();
}
int main()
{
SET S1, S2, Uni;
S1.getData('A');
S2.getData('B');
Uni.getData('U');
S1.Intersection(S2);
if(S1.isSubset(S2))
{
cout << "SET A is a subset of SET B" << endl;
}
else
{
cout << "SET A is not a subset of SET B" << endl;
}
cout << endl;
if(S2.isSubset(S1))
{
cout << "SET B is a subset of SET A" << endl;
}
else
{
cout << "SET B is not a subset of SET A" << endl;
}
cout << endl ;
S1.Complement(Uni, 'A');
S2.Complement(Uni, 'B');
Output
PRACTICAL NO 3 & 4
Create a class RELATION, use Matrix notation to represent a relation. Include
functions to check if the relation is Reflexive, Symmetric, Anti-symmetric and
Transitive. Write a Program to use this class.
Use the functions defined in Ques 3 to check whether the given relation is:
a) Equivalent, or
b) Partial Order relation, or
c) None
Code:
#include <iostream>
using namespace std;
class RELATION
{
int Arr[50][50];
int size;
public:
void getData();
void print_rel();
bool reflexive();
bool symmetric();
bool anti_symmetric();
bool transitive();
};
void RELATION::getData()
{
cout << "Enter the size of the array : ";
cin >> size;
cout << "Enter values as represented in matrix relation i.e. 0 and 1 : "
<< endl;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
cin >> Arr[i][j];
while(Arr[i][j] != 0 && Arr[i][j] != 1)
{
cout << "Wrong input : Enter either 0 or 1 : ";
cin >> Arr[i][j];
}
}
}
print_rel();
}
void RELATION::print_rel()
{
cout << "MATRIX NOTATION OF RELATION : " << endl;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
cout << Arr[i][j] << " ";
}
cout << endl;
}
}
bool RELATION::reflexive()
{
bool f = true;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(i == j)
{
if(Arr[i][j] != 1)
{
f = false;
break;
}
}
}
}
return f;
}
bool RELATION::symmetric()
{
bool f = true;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(Arr[i][j] == 1 && Arr[j][i] != 1)
{
f = false;
break;
}
}
}
return f;
}
bool RELATION::anti_symmetric()
{
bool f = true;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(i != j)
{
if(Arr[i][j] == 1 && Arr[j][i] == 1)
{
f = false;
break;
}
}
}
}
return f;
}
bool RELATION::transitive()
{
bool f = true;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
for(int k = 0; k < size; k++)
{
if(Arr[i][j] == 1 && Arr[j][k] == 1 && Arr[i][k] != 1)
{
f = false;
break;
}
}
}
}
return f;
}
int main()
{
RELATION R;
R.getData();
if(R.reflexive())
cout << "Entered Relation is Reflexive..." << endl <<endl;
else
cout << "Entered Relation is not Reflexive..." << endl << endl;
if(R.symmetric())
cout << "Entered Relation is Symmetric..." <<endl <<endl;
else
cout << "Entered Relation is not Symmetric..." << endl << endl;
if(R.anti_symmetric())
cout << "Entered Relation is Anti-Symmetric..." <<endl <<endl;
else
cout << "Entered Relation is not Anti-Symmetric..." << endl <<
endl;
if(R.transitive())
cout << "Entered Relation is Transitive..." <<endl <<endl;
else
cout << "Entered Relation is not transitive..." << endl <<
endl;
int fib_series(int n)
{
if((n == 1) || (n == 0))
{
return n;
}
else
{
return fib_series(n - 1) + fib_series(n -2);
}
}
int main ()
{
cout << "Enter nos required for the fibonacci series : ";
int n;
cin >> n;
int i = 0;
cout << "Fibonacci Series : ";
while(i < n)
{
cout << fib_series(i) << " ";
i++;
}
}
Output:
PRACTICAL NO 6
Write a program to implement Tower of Hanoi using recursion.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter no. of disks : ";
int n;
cin >> n;
Output:
PRACTICAL NO 7
Write a program to implement binary search using recursion.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the no. of elements : ";
int n;
cin >> n;
Output:
PRACTICAL NO 8
Write a Program to implement Bubble Sort. Find the number of comparisons
during each pass and display the intermediate result.
Code:
#include <iostream>
using namespace std;
class SORT
{
int Arr[50];
int size;
int count;
public:
void getData();
void print();
void bubble_sort();
};
void SORT::getData()
{
cout << "Enter the size of the array : ";
cin >> size;
cout << endl;
cout << "Enter elements : ";
for(int i = 0 ; i < size; i++)
{
cin >> Arr[i];
}
cout << endl;
}
void SORT::print()
{
for(int i = 0; i < size; i++)
{
cout << Arr[i] << " ";
}
cout << endl;
}
void SORT::bubble_sort()
{
int pass = 0, sum = 0;
for(int i = 0; i < size - 1; i++)
{
count = 0;
for(int j = 0; j < size - i - 1; j++)
{
count++;
if(Arr[j] > Arr[j+1])
{
int temp = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = temp;
}
}
++pass;
sum = sum + pass;
cout << "Pass : " << pass << endl;
cout << "Intermediate Array : ";
print();
cout << "No. of comparison : " << count << endl<<endl;
}
cout<<"\nTotal Comparison : " << sum << endl;
}
int main()
{
SORT S;
S.getData();
S.bubble_sort();
cout << "Sorted Array : ";
S.print();
}
Output:
PRACTICAL NO 9
Write a Program to implement Insertion Sort. Find the number of comparisons
during each pass and display the intermediate result.
Code:
#include <iostream>
using namespace std;
class SORT
{
int Arr[50];
int size;
int count;
public:
void getData();
void print();
void insert_sort();
};
void SORT::getData()
{
cout << "Enter the size of the array : ";
cin >> size;
cout << endl;
cout << "Enter elements : ";
for(int i = 0 ; i < size; i++)
{
cin >> Arr[i];
}
cout << endl;
}
void SORT::print()
{
for(int i = 0; i < size; i++)
{
cout << Arr[i] << " ";
}
cout << endl;
}
void SORT::insert_sort()
{
int pass = 0, sum = 0;
for(int i = 0; i < size ; i++)
{
int key = Arr[i];
int j = i - 1;
count = 0;
while(j >= 0 && Arr[j] > key)
{
count++;
Arr[j + 1] = Arr[j];
j = j - 1;
}
Arr[j + 1] = key;
++pass;
sum = sum + count;
cout << "Pass : " << pass << endl;
cout << "Intermediate Array : ";
print();
cout << "No. of comparison : " << count << endl<<endl;
}
cout<<"\nTotal Comparison : " << sum << endl;
}
int main()
{
SORT S;
S.getData();
S.insert_sort();
cout << "Sorted Array : ";
S.print();
}
Output:
PRACTICAL NO 10
Write a program that generates all the permutations of a given set of digits,
with or without repetition.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the no. of digits : ";
int size;
cin >> size;
int Arr[size];
cout << "Enter the digits : ";
for(int i = 0; i < size; i++)
{
cin >> Arr[i];
}
Output:
PRACTICAL NO 11
Write a program to calculate permutation and combination for an input value
n and r using recursive formula of nPr and nCr.
Code:
#include <iostream>
using namespace std;
int main()
{
cout <<"Formula for calculating Permutation : n! " << endl;
cout <<"\t\t\t\t --------" << endl;
cout <<"\t\t\t\t (n - r)!" << endl << endl;
Output:
PRACTICAL NO 12
For any number n, write a program to list all the solutions of the equation x1
+x2 + x3+…+xn=C, where C is a constant(C<=10) and x1, x2, x3, ----, xn are
non-negative integers using brute force strategy.
Code:
#include <iostream>
using namespace std;
while(true)
{
if((k + 1) == n)
{
print(A, k + 1);
}
int val = 0;
while(k >= 0 && A[k] == 1)
{
val = val + A[k];
k--;
}
if(k < 0)
{
return;
}
A[k]--;
val++;
while(val > A[k])
{
A[k + 1] = A[k];
val = val - A[k];
k++;
}
A[k + 1] = val;
k++;
}
}
int main()
{
int n, C;
cout << "All Partitions of " << C << " of size " << n << " : " << endl;
partition (C, n);
}
Output:
PRACTICAL NO 13
Write a Program to accept the truth values of variables x and y, and print the
truth table of the following logical operations:
a) Conjunction f) Exclusive NOR
b) Disjunction g) Negation
c) Exclusive OR h) NAND
d) Conditional i) NOR
e) Bi-Conditional
Code:
#include <iostream>
using namespace std;
class OPT
{
int Arr[50];
public:
void OPT :: print(int A[], int B[], int C[], string s, int size)
{
cout << "-------------------------" << endl;
cout << " x y x " << s <<" y" << endl;
cout << "-------------------------" << endl;
for(int i = 0; i < size; i++)
{
cout << " " << A[i] <<" "<< B[i] << " "<< C[i] <<endl;
}
cout << "-------------------------" << endl << endl;
}
int main()
{
OPT X, Y;
X.getData('x', size);
Y.getData('y', size);
cout << endl;
}
Output:
PRACTICAL NO 14
Write a program to accept an input n from the user and graphically represent
the values of T(n) where n varies from 0 to n for the recurrence relations. For
e.g. T(n) = T(n – 1) + n, T(0) = 1, T(n) = T(n – 1) + n2, T(0) = 1, T(n) = 2 *
T(n)/2 + n, T(1) = 1.
Code:
PRACTICAL NO 15
Write a program to store a function (polynomial/exponential) and then
evaluate the polynomial. (For example, store f(x)= 4n3+2n+9 in an array and
for a given value of n, say n=5, evaluate (i.e. compute the value of f(5)).
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Enter the degree of polynomial : ";
int d;
cin >> d;
cout << "\nEnter the coefficient of each term starting from the highest
order term : ";
for(int i = 0; i <= d; i++)
{
cin >> Pol[i];
}
int result = 0;
for(int i = 0; i <= d; i++)
{
result = result + (pow(x, d - i) * Pol[i]);
}
cout << "f(" << x <<") = " << result;
}
Output:
PRACTICAL NO 16
Write a program to represent Graphs using the Adjacency matrix and check if
it is a complete graph.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the no. of vertices in the graph : ";
int v;
cin >> v;
int Arr[v][v];
cout << "Enter 1 if there is an edge b/w the vertex otherwise enter 0 : ";
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
cin >> Arr[i][j];
}
}
bool f = true;
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
if(i == j)
{
if(Arr[i][j] == 1)
{
f = false;
break;
}
}
else
{
if(Arr[i][j] == 0)
{
f = false;
break;
}
}
}
}
if(f)
{
cout << "Given graph is complete.";
}
else
{
cout << "Given graph is not complete. ";
}
}
Output:
PRACTICAL NO 17
Write a Program to accept a directed graph G and compute the in-degree and
out-degree of each vertex.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the no. of vertices in the graph : ";
int v;
cin >> v;
int Arr[v][v];
Output:
PRACTICAL NO 18
Given a graph G, write a Program to find the number of paths of length n
between the source and destination entered by the user.
Code:
#include <iostream>
using namespace std;
int cal_path(int A[10][10], int num, int path_len, int src, int des);
void multiply(int A[10][10], int B[10][10], int num);
int main()
{
int Arr[10][10];
cout << "Enter the no. of vertices (must be <= 10): ";
int v;
cin >> v;
cout << "Enter 1 if there is an edge b/w the vertex otherwise enter 0 : ";
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
cin >> Arr[i][j];
}
}
int cal_path(int A[10][10], int num, int path_len, int src, int des)
{
src--;
des--;
int B[10][10];
for(int i = 0; i < num; i++)
{
for(int j = 0; j < num; j++)
{
B[i][j] = A[i][j];
}
}
for(int i = 0; i < path_len - 1; i++)
{
multiply(A, B, num);
}
return B[src][des];
}
Output:
PRACTICAL NO 19
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 (for
circuit assume vk=v1).
Code:
#include <iostream>
using namespace std;
int main()
{
int Arr[10][10];
cout << "Enter the no. of vertices (must be <= 10) : ";
int v;
cin >> v;
if(directed_undirected(Arr, v) == 1)
{
int p = path_undirected(Arr, v);
int c = circuit_undirected(Arr, v);
if(p == 1 && c == 1)
{
cout << "Given undirected graph has both euler's path and
circuit." << endl;
}
else if(p == 1)
{
cout << "Given undirected graph has euler's path only." << endl;
}
else if(c == 1)
{
cout << "Given undirected graph has euler's circuit only." <<
endl;
}
else
{
cout << "Given undirected graph neither has euler's path nor
euler's circuit." << endl;
}
}
else
{
int p = path_directed(Arr, v);
int c = circuit_directed(Arr, v);
if(p == 1 && c == 1)
{
cout << "Given directed graph has both euler's path and circuit."
<< endl;
}
else if(p == 1)
{
cout << "Given directed graph has euler's path only." << endl;
}
else if(c == 1)
{
cout << "Given directed graph has euler's circuit only." << endl;
}
else
{
cout << "Given directed graph neither has euler's path nor euler's
circuit." << endl;
}
}
}
Output:
PRACTICAL NO 20
Given a full m-ary tree with i internal vertices, write a Program to find the
number of leaf nodes.
Code:
#include <iostream>
using namespace std;
int main()
{
int m, i, l;
l = (m - 1) * i + 1;
Output: