DS PRACTICAL FILE SEM 2, Lab Reports of Discrete Structures and Graph Theory

in this i have covered all the practicals of discrete structure of sem 2 of delhi university.

Typology: Lab Reports

2020/2021

Uploaded on 09/17/2021

deepanshu-sheemar
deepanshu-sheemar 🇮🇳

1 document

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Course - B.Sc (Hons) Computer Science
Section - A
Subject - Discrete Structure
Date - August 7, 2021
QUES 1 Write a program to represent Graph using the adjacency Matrices and check if
it is a complete graph .
SOURCE CODE
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download DS PRACTICAL FILE SEM 2 and more Lab Reports Discrete Structures and Graph Theory in PDF only on Docsity!

Course - B.Sc (Hons) Computer Science

Section - A

Subject - Discrete Structure

Date - August 7, 2021

QUES 1 – Write a program to represent Graph using the adjacency Matrices and check if

it is a complete graph.

SOURCE CODE –

#include #include <conio.h> using namespace std; class graph{ private: char** arr; int size; char* set; public: graph(){ //Default constructor arr=NULL; size= 0 ; } void set_size(){ //function to set size of the matrix cout<<"ENTER THE SIZE OF THE SET: "; cin>>size; cout << endl; set = new char[size]; cout<<"ENTER ELEMENTS OF THE SET - \n"; for(int i= 0 ;i<size;i++){ cout << "ENTER ELEMENT " << i << ": "; cin>>set[i]; arr=new char*[size];} for(int i= 0 ;i<size;i++) arr[i]=new char[size]; for(int i= 0 ;i<size;i++) { //initalizing matrix with zero at all positions. for(int j= 0 ;j<size;j++){ arr[i][j]= 0 ; } } } void setedges(){ //function to enter relation pairs in matrix. int i,num; char a,b; cout << endl; cout<<"ENTER THE NO. OF ADJACENT SIDES: "; cin>>num; for(int i = 0 ;i<num;i++) { cout<<endl<<"ENTER PAIR NO. "<<(i+ 1 )<<" : "; cin>>a>>b; char p,q; for(int m= 0 ;m<size;m++) { if(a==set[m]) p=m; if(b==set[m]) q=m; }

if(i == j){ if(arr[i][j] != 0 ){ return false; } } } } return true; } bool checkmulti(){ for (int i = 0 ; i < size;i++){ for(int j = 0 ;j < size;j++){ if(arr[i][j] > 1 ){ return true; } } } return false; } }; int main(){ graph g; char ch = 'y'; do{ g.set_size(); g.setedges(); cout << endl; cout << "------------------------------------"; cout << endl; if(g.check()){ cout << "This is a complete graph."; } else{ if((g.checkmulti())){ cout << "THIS IS A MULTIGRAPH."; cout << endl; cout << endl; cout << "This is not a complete graph."; } else{ cout << "This is not a complete graph.";

cout << endl; cout << "-------------------------------------"; cout << endl; cout << "Do you want to re-execute the program(y/n): "; cin >> ch;}while(ch == 'y' || ch == 'Y'); getch(); }

OUTPUT –

else{ return (comb(n- 1 ,r- 1 ) + comb(n- 1 ,r)); } } //Driver code int main(){ int n,r,ch; char opt; do{ cout << "ENTER THE VALUE OF N: "; cin >>n; cout << "ENTER THE VALUE OF R: "; cin >> r; if(r > n){ cout << "ERROR: VALUE OF R IS GREATER THAN N."; cout << endl; cout << "DO YOU WANT TO CHECK AGAIN(Y/N)?: "; cin >> opt; } else{ cout << endl; cout << "COMBINATION : " << comb(n,r); cout << endl; cout << endl; cout << "DO YOU WANT TO CONTINUE(Y/N): "; cin >> opt; } }while(opt == 'Y'|| opt == 'y'); getch(); }

OUTPUT –

EXAMPLE - \

Take an example for n = 4, r = 3;

Now , according to our recursive function

1. It will first check if n is greater than r or not, if r is greater, it will return zero.

2. Here , we have n greater than zero , so it will check second case ,where if n = 0 or r =

0 or n == r , it will return 1.

3. Here n and r does not meet the case 2 condition , so it will do the else condition.

N = 4, r = 3;

It will compute comb(n-1,r-1) + comb(n-1,r)

i.e.. comb(3,2) + comb(3,3)

 Comb(3,3) = 1 ,because n == r so it will return 1.

 Comb(3,2) will again break acc to third case –

Comb(2,1) + comb(2,2)

Comb(3,2) + Comb(3,3)

Returns 1 as n == r.

Comb(2,1) + Comb(2,2)

Return 1 as n == r.

Comb(1,0) + comb(1,1)

1 + 1 [r = 0 and n == r]

Comb(4,3) = 1 + 1 + 1+1 = 4.