0% found this document useful (0 votes)
30 views50 pages

DS PRACTICAL FILE Adaya

The document discusses programs to perform set operations like checking membership, finding cardinality and power set of a set. It includes code to define a SET class to represent sets and perform operations like union, intersection, subset checking, complement, difference and cartesian product on two input sets.

Uploaded by

Simi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views50 pages

DS PRACTICAL FILE Adaya

The document discusses programs to perform set operations like checking membership, finding cardinality and power set of a set. It includes code to define a SET class to represent sets and perform operations like union, intersection, subset checking, complement, difference and cartesian product on two input sets.

Uploaded by

Simi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

PRACTICAL NO 1

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;

bool isMember(char A[], char a, int size)


{
for(int i = 0; i < size; i++)
{
if(a == A[i])
{
return true;
}
}
return false;
}

int cardinality (char A[], int n)


{

for (int i = 0; i < n; i++)


{
for (int j = i+1; j < n; )
{
if (A[j] == A [i])
{
for (int k = j; k < n; k++ )
{
A[k] = A[k+1];
}
n--;
}
else
{
j++;
}
}
}
return n;
}

void powerSet(char A[], int size)


{
int powerset_size = pow(2, size);
cout << "{ ";
for(int counter = 0; counter < powerset_size; counter++)
{
cout << "{ ";
for(int i = 0; i < size; i++)
{
if(counter & (1 << i))
{
cout << A [i] << " ";
}
}
cout <<"} " ;
}
cout <<"}";
}

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];
}

int c = cardinality (A, size);


cout << "Cardinality of this set : " << c << endl;

cout << "Enter an element you want to search : ";


char a;
cin >> a;
bool x = isMember(A, a, size);
if(x == true)
{
cout << a <<" is an element of this set." << endl;
}
else
{
cout << a <<" is not an element of this set." << endl;
}

cout << "Powerset of this set : " << endl;


powerSet(A, c);
}

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++;
}
}
}
}

bool SET::isSubset(SET obj)


{
bool temp;
for(int i = 0; i < size; i++)
{
temp = false;
for(int j = 0; j < obj.size; j ++)
{
if(Arr[i] == obj.Arr[j])
{
temp = true;
}
}
if(temp == false)
{
break;
}
}
return temp;
}

void SET::Union(SET obj)


{
SET U;
int k = 0;
for(int i = 0;i < size;i++)
{
U.Arr[k++] = Arr[i];
}
for(int j = 0;j < obj.size;j++)
{
U.Arr[k++] = obj.Arr[j];
}
U.size = k;
U.remove_dup();
U.print();
}

void SET::Intersection(SET obj)


{
SET I;
int k = 0;
for(int i = 0; i < size; i ++)
{
for(int j = 0; j < obj.size; j++)
{
if(Arr[i] == obj.Arr[j])
{
I.Arr[k++] = Arr[i];
}
}
}
I.size = k;
I.remove_dup();
cout << "Intersection of the sets = ";
I.print();
}

void SET::Complement(SET obj, char x)


{
SET U;
int k = 0;
bool f;
for(int i = 0; i < obj.size; i++)
{
f = true;
for(int j = 0; j < size; j++)
{
if(obj.Arr[i] == Arr[j])
{
f = false;
}

}
if(f == true)
{
U.Arr[k++] = obj.Arr[i];
}
}
U.size = k;
cout << "Complement of Set " << x << " : ";
U.print();
}

SET SET:: set_diff(SET obj)


{
SET diff;
int k=0;
bool b;
for(int i = 0; i < size; i++)
{
b = true;
for(int j = 0; j <obj.size; j++)
{
if(Arr[i] == obj.Arr[j])
b=false;
}
if(b == true)
diff.Arr[k++]=Arr[i];
}
diff.size=k;
return diff;
}

void SET::cart_pro(SET obj, char x, char y)


{
cout << x << " X " << y << " : { ";
for(int i = 0; i < size; i++)
{
for(int j = 0; j < obj.size; j ++)
{
cout << "(" << Arr[i] << " , " << obj.Arr[j] << ")";
if(i < size - 1 || j < obj.size - 1)
{
cout << " , ";
}
}
}
cout << " }" << endl;;

int main()
{
SET S1, S2, Uni;

S1.getData('A');
S2.getData('B');
Uni.getData('U');

cout << "Union of the sets = ";


S1.Union(S2);

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');

cout << "A - B = ";


SET D1 = S1.set_diff(S2);
D1.print();
cout << "B - A = ";
SET D2 = S2.set_diff(S1);
D2.print();

cout << "Symmetric Difference : ";


D1.Union(D2);

S1.cart_pro(S2, 'A' , 'B');


S2.cart_pro(S1, 'B', 'A');
}

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;

if(R.transitive() && R.reflexive())


{
if(R.symmetric() && R.anti_symmetric())
{
cout << "Entered Relation is both Equivalent and Partial ordered
relation.";
}
else if(R.symmetric())
{
cout << "Entered Relation is Equivalent.";
}
else if(R.anti_symmetric())
{
cout << "Entered Relation is Partial Ordered Relation.";
}
}
else
{
cout << "Entered Relation is neither Equivalent nor Partial ordered
relation.";
}
}
Output:
PRACTICAL NO 5
Write a program to generate the fibonacci series using recursion.
Code:
#include <iostream>
using namespace std;

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;

void tower_hanoi(int n, char from_rod, char to_rod, char aux_rod)


{
if(n == 0)
{
return;
}
tower_hanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod << " to rod " <<
to_rod << endl;
tower_hanoi(n - 1, aux_rod, to_rod, from_rod);
}

int main()
{
cout << "Enter no. of disks : ";
int n;
cin >> n;

tower_hanoi(n, 'A', 'C', 'B');


}

Output:
PRACTICAL NO 7
Write a program to implement binary search using recursion.
Code:
#include <iostream>
using namespace std;

int binary_search(int Arr[], int n, int low, int high)


{
int mid = (low + high)/2;
if(low <= high)
{
if(n == Arr[mid])
{
return mid;
}
else if(n < Arr[mid])
{
return binary_search(Arr, n, low, mid - 1);
}
else
{
return binary_search(Arr, n, mid + 1, high);
}
}
return -1;
}

int main()
{
cout << "Enter the no. of elements : ";
int n;
cin >> n;

cout << "Enter the elements : ";


int Arr[n];
for(int i = 0; i < n; i++)
{
cin >> Arr[i];
}

cout << "Enter an element you want to serach for : ";


int x;
cin >> x;

int result = binary_search(Arr, x, 0, n -1);


(result == -1) ? cout << "Element Not Found. " : cout << "Element Found at
index " << result;
}

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();

cout << "Before Sorting : ";


S.print();
cout << endl;

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();

cout << "Before Sorting : ";


S.print();
cout << endl;

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;

void printPermutation(int Arr[], int n)


{
for(int i = 0; i < n; i++)
{
cout << Arr[i] << " ";
}
cout << endl;
}

void findPermutation(int Arr[], int size, int n)


{
if(size == 1)
{
printPermutation(Arr, n);
}
for(int i = 0; i < size; i++)
{
findPermutation(Arr, size - 1, n);
if(size % 2 == 1)
{
int t = Arr[0];
Arr[0] = Arr[size - 1];
Arr[size - 1] = t;
}
else
{
int t = Arr[i];
Arr[i] = Arr[size - 1];
Arr[size - 1] = t;
}
}
}

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];
}

cout <<"Permutation : " << endl;


findPermutation(Arr, size, size);
}

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 factorial (int num)


{
int f = 1;
if(num == 0)
{
return f;
}
for(int i = 1; i <= num; i++)
{
f = f * i;
}
return f;
}

int permutation(int n, int r)


{
int n_fact = factorial(n);
int n_minus_r_fact = factorial(n - r);
int nPr = n_fact / n_minus_r_fact;
return nPr;
}

int combination(int n, int r)


{
int r_fact = factorial(r);
int nCr = permutation(n, r) / r_fact;
return nCr;
}

int main()
{
cout <<"Formula for calculating Permutation : n! " << endl;
cout <<"\t\t\t\t --------" << endl;
cout <<"\t\t\t\t (n - r)!" << endl << endl;

cout <<"Formula for calculating Combination : n! " << endl;


cout <<"\t\t\t\t ----------" << endl;
cout <<"\t\t\t\t r!(n - r)!" << endl << endl;

cout << "Enter total no. of objects : ";


int n;
cin >> n;
cout << endl;

cout << "Enter no. of objects to choose : ";


int r;
cin >> r;
cout << endl << endl;

cout << "Permutation (nPr) : ";


cout << permutation(n, r);
cout << endl;

cout << "Combination (nCr) : ";


cout << combination(n, r);
cout << 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;

void print(int Arr[], int size)


{
for(int i = 0; i < size; i ++)
{
cout << Arr[i] << " ";
}
cout << endl;
}

void partition(int c, int n)


{
int A[c];
int k = 0;
A[k] = c;

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 << "Enter the constant (must be <= 10) : ";


cin >> C;

cout << "Enter the no. of partition : ";


cin >> n;

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 getData(char k, int size);


void Conjunction(OPT Obj, int size);
void Disjunction(OPT Obj, int size);
void XOR(OPT Obj, int size);
void Conditional(OPT Obj, int size);
void BIConditional(OPT Obj, int size);
void XNOR(OPT Obj, int size);
void Negation(int size, char k);
void NAND(OPT Obj, int size);
void NOR(OPT Obj, int size);
void print(int A[], int B[], int C[], string s, int size);
};

void OPT :: getData(char k, int size)


{
cout << "Enter the truth values of " << k <<" : ";
for(int i = 0; i < size; i++)
{
cin >> Arr[i];
}
}

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;
}

void OPT :: Conjunction(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = (Arr[i] & Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "&", size);
}

void OPT :: Disjunction(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = (Arr[i] || Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "|", size);
}

void OPT :: XOR(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = (Arr[i] ^ Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "^", size);
}

void OPT :: Conditional(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
if(Arr[i] == 1 && Obj.Arr[i] == 0)
{
C[i] = 0;
}
else
{
C[i] = 1;
}
}
print(Arr, Obj.Arr, C, "->", size);
}

void OPT :: BIConditional(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = (!Arr[i] ^ Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "<->", size);
}

void OPT :: XNOR(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = !(Arr[i] ^ Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "XNOR", size);
}

void OPT :: Negation(int size, char k)


{
cout << "-----------------------" << endl;
cout << " " << k << " ~" << k << endl;
cout << "-----------------------" << endl;
for(int i = 0; i < size; i++)
{
cout << " " << Arr[i] << " " <<(!Arr[i]) << endl;
}
cout << "-----------------------" << endl;
}

void OPT :: NAND(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = !(Arr[i] & Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "NAND", size);
}

void OPT :: NOR(OPT Obj, int size)


{
int C[size];
for(int i = 0; i < size; i++)
{
C[i] = !(Arr[i] || Obj.Arr[i] );
}
print(Arr, Obj.Arr, C, "NOR", size);
}

int main()
{
OPT X, Y;

cout << "Enter the no. of values : ";


int size;
cin >> size;

X.getData('x', size);
Y.getData('y', size);
cout << endl;

cout <<"a. CONJUNCTION : " << endl;


X.Conjunction(Y, size);

cout <<"b. DISJUNCTION : " << endl;


X.Disjunction(Y, size);

cout <<"c. Exclusive OR : " << endl;


X.XOR(Y, size);

cout <<"d. Conditional : " << endl;


X.Conditional(Y, size);

cout <<"e. Bi-Conditional : " << endl;


X.BIConditional(Y, size);

cout <<"f. Exclusive NOR : " << endl;


X.XNOR(Y, size);

cout <<"g. Negation: " << endl;


X.Negation(size, 'x');
Y.Negation(size, 'y');

cout <<"h. NAND : " << endl;


X.NAND(Y, size);

cout <<"i. NOR : " << endl;


X.NOR(Y, size);

}
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;

int Pol[d + 1];

cout << "\nEnter the coefficient of each term starting from the highest
order term : ";
for(int i = 0; i <= d; i++)
{
cin >> Pol[i];
}

cout << "\nf(x) = ";


for(int i = 0, j = d; i < d; i++, j--)
{
cout << Pol[i] << "x^"<< j <<" + ";
}
cout << Pol[d];

cout << "\nEnter the value of x : ";


int x;
cin >> x;

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];
}
}

cout << "Adjacency Matrix : " << endl;


cout << " ";
for(int i = 1; i <= v; i++)
{
cout << "v" << i << " ";
}
cout << endl;

for(int i = 0; i < v; i++)


{
cout << "v" << (i + 1) << " ";
for(int j = 0; j < v; j++)
{
cout << Arr[i][j] << " ";
}
cout << endl;
}

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];

for(int i = 0; i < v; i++)


{
for(int j = 0; j < v; j++)
{
cout << "Input no. of edges b/w vertex " << i + 1 << " and " << j
+ 1 << " : " ;
cin >> Arr[i][j];
cout << endl;
}
}

cout << "Adjacency Matrix : " << endl;


cout << " ";
for(int i = 1; i <= v; i++)
{
cout << "v" << i << " ";
}
cout << endl ;

for(int i = 0; i < v; i++)


{
cout << "v" << (i + 1) << " ";
for(int j = 0; j < v; j++)
{
cout << Arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
for(int i = 0; i < v; i++)
{
int sum = 0;
for(int j = 0; j < v; j++)
{
sum = sum + Arr[j][i];
}
cout << "In-degree of vertex " << i + 1 << " : " << sum << endl;
}
cout << endl;
for(int i = 0; i < v; i++)
{
int sum = 0;
for(int j = 0; j < v; j++)
{
sum = sum + Arr[i][j];
}
cout << "Out-degree of vertex " << i + 1 << " : " << sum << endl;
}
}

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];
}
}

cout << "Adjacency Matrix : " << endl;


cout << " ";
for(int i = 1; i <= v; i++)
{
cout << "v" << i << " ";
}
cout << endl;

for(int i = 0; i < v; i++)


{
cout << "v" << (i + 1) << " ";
for(int j = 0; j < v; j++)
{
cout << Arr[i][j] << " ";
}
cout << endl;
}

cout << "\nEnter the length of the path : ";


int path_len;
cin >> path_len;
cout << "\nEnter the source vertex : ";
int src;
cin >> src;

cout << "\nEnter the destination vertex : ";


int des;
cin >> des;

int len = cal_path(Arr, v, path_len, src, des);


cout <<"\n\nNo. of paths of length " << path_len << " from vertex " << src
<< " to vertex " << des << " are " <<len;
}

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];
}

void multiply(int A[10][10], int B[10][10], int num)


{
int C[num][num];
for(int i = 0; i < num; i++)
{
for(int j = 0; j < num; j++)
{
C[i][j] = 0;
for(int k = 0; k < num; k++)
{
C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
}
}
}
for(int i = 0; i < num; i++)
{
for(int j = 0; j < num; j++)
{
B[i][j] = C[i][j];
}
}
}

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 directed_undirected(int Arr[10][10], int size);


int circuit_directed(int Arr[10][10], int size);
int path_directed(int Arr[10][10], int size);
int circuit_undirected(int Arr[10][10], int size);
int path_undirected(int Arr[10][10], int size);

int main()
{
int Arr[10][10];

cout << "Enter the no. of vertices (must be <= 10) : ";
int v;
cin >> v;

for(int i = 0; i < v; i++)


{
for(int j = 0; j < v; j++)
{
cout << "Input no. of edges b/w vertex " << i + 1 << " and " << j
+ 1 << " : " ;
cin >> Arr[i][j];
cout << endl;
}
}

cout << "Adjacency Matrix : " << endl;


cout << " ";
for(int i = 1; i <= v; i++)
{
cout << "v" << i << " ";
}
cout << endl ;

for(int i = 0; i < v; i++)


{
cout << "v" << (i + 1) << " ";
for(int j = 0; j < v; j++)
{
cout << Arr[i][j] << " ";
}
cout << endl;
}

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;
}
}
}

int directed_undirected(int Arr[10][10], int size)


{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(Arr[i][j] != Arr[j][i])
{
return 0;
}
}
}
return 1;
}

int path_undirected(int Arr[10][10], int size)


{
int deg, count = 0;
for(int i = 0; i < size; i++)
{
deg = 0;
for(int j = 0; j < size; j++)
{
deg = deg + Arr[i][j];
}
if(deg % 2 != 0)
{
count ++;
}
}
if(count == 2)
{
return 1;
}
else
{
return 0;
}
}

int circuit_undirected(int Arr[10][10], int size)


{
int deg;
for(int i = 0; i < size; i++)
{
deg = 0;
for(int j = 0; j < size; j++)
{
deg = deg + Arr[i][j];
}
if(deg % 2 != 0)
{
return 0;
}
}
return 1;
}

int path_directed(int Arr[10][10], int size)


{
int deg, count = 0;
for(int i = 0; i < size; i++)
{
deg = 0;
for(int j = 0; j < size; j++)
{
deg = deg + Arr[i][j] + Arr[j][i];
}
if(deg % 2 != 0)
{
count ++;
}
}
if(count == 2)
{
return 1;
}
else
{
return 0;
}
}

int circuit_directed(int Arr[10][10], int size)


{
int deg;
for(int i = 0; i < size; i++)
{
deg = 0;
for(int j = 0; j < size; j++)
{
deg = deg + Arr[i][j] + Arr[j][i];
}
if(deg % 2 != 0)
{
return 0;
}
}
return 1;
}

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;

cout << "Enter the value of m : ";


cin >> m;

cout << "\nEnter the no. of internal vertices : ";


cin >> i;

l = (m - 1) * i + 1;

cout << "\nNo. of leaf nodes : " << l;


}

Output:

You might also like