Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

DISCRETE STRUCTURES &

THEORY OF LOGIC
(KCS - 353)

LABORATORY MANUAL

B.TECH, 2nd Year, Semester - III

COMPUTER SCIENCE
&
ENGINEERING

Faculty Name: Student Name :


Ms. Charu Singh Roll No. :
Section :
Vision of the Institute

“To become a leading institute of providing professionally competent and


socially responsive technocrats with high moral values."

Mission of the Institute

M1. To create an eco-system for the dissemination of technical knowledge, to


achieve academic excellence.

M2. To develop technocrats with creative skills and leadership qualities,


to solve local and global challenges.

M3. To impart human values and ethics in students, to make them socially and
eco-friendly responsible.
Vision of the Department

“To produce globally competent professionals having social values and


commitment to serve the global needs with the ability to work in an
interdisciplinary environment."

Mission of the Department

M1. "To impart quality education to the students to enhance their ethical,
professional and leadership qualities to make them globally competitive."

M2. "To create a conducive environment in which students can explore


computational problems and analyze them to identify the optimal solutions."

M3. "To strive for continual enhancement of technical knowledge & innovation
through industry interface to accomplish global needs."
Program Educational Objectives (PEOs)

PEO1: Students must be able to apply software engineering principles to analyze


complex computing problems and identify their solutions.

PEO2: Students must be able to analyze, design, and implement the latest
technology-driven projects.

PEO3: Students must be able to work in a collaborative environment and


understand the ethical, social, and economic impact of their work.

Program Specific Outcomes (PSOs)

PSO 1: Able to design and implement the data structures and algorithms to deliver
quality software products.

PSO 2: Able to apply Artificial Intelligence and Machine Learning concepts to


solve society-related needs.
Program Outcomes (POs)

PO’s An Engineering Graduate of the Department of Computer Science and Engineering


Program will be able to demonstrate:
PO1 ENGINEERING KNOWLEDGE: Apply the knowledge of mathematics, including
discrete mathematics, probability, statistics and fundamentals of various engineering disciplines
like computer science and engineering, electronic engineering and Electrical engineering in the
core information technologies.
PO2 PROBLEM ANALYSIS: Analyze a problem and identify the computing requirements
appropriate to its solution.
PO3 DESIGN/DEVELOPMENT OF SOLUTIONS: Design and implement hardware and
software systems, components, process or program to meet the desired needs within reasonable
economic, environmental, social, political, ethical, health and safety, manufacturability, and
sustainability constraints.

PO4 CONDUCT INVESTIGATIONS OF COMPLEX PROBLEMS: Reassess literature and


indulge in research to use research-based knowledge and methods to design and conduct new
experiments, as well as to organize, analyze and interpret data to produce draw valid Conclusions
and recommendations.
PO5 MODERN TOOL USAGE: Use appropriate techniques, resources, and modern
engineering and IT tools necessary for computer engineering practice.
PO6 THE ENGINEER AND SOCIETY: Show the understanding of local and global impact of
computing on individuals, organizations and society.
PO7 ENVIRONMENT AND SUSTAINABILITY: Integrate IT-based solutions in
environmental contexts, and demonstrate the knowledge of need for sustainable development.
PO8 ETHICS: Demonstrate the knowledge of professional and ethical responsibilities along
with the norms of the engineering practice.
PO9 INDIVIDUAL AND TEAM WORK: Demonstrate leadership and an ability to work as a
member with responsibility to function on multi-disciplinary teams to accomplish a common goal.
PO10 COMMUNICATION: Demonstrate effectively communicate skills in both oral and written
form with a range of audiences.
PO11 PROJECT MANAGEMENT AND FINANCE: Apply the knowledge and understanding of
engineering and management principles to design, planned budget and propose IT project for an
identified need within a specific scope.
PO12 LIFE-LONG LEARNING: Developed confidence to acquire new knowledge in the
computing discipline and to engage in life-long learning.
Course Objective:

Students will be able to:

1. Solve the practical examples of sets, functions, relations and recurrence relations.
2. Describe the concept of logical and mathematical foundations and study abstract
models of computation.
3. Define modern algebra for constructing and writing mathematical proofs.
4. Recognize the patterns that arise in graph problems and use this knowledge for
constructing the trees and spanning trees.

Course Outcomes (COs):

Bloom's
Course Outcomes:
Level (BL)

KCS353.1 Implementation of counting technique and probability-based questions. L3


KCS353.2 Implementation of various operations of set. L4
KCS353.3 Solve logic gates AND, OR, NOT L4
KCS353.4 Implementation of graph and trees and problem based on graph and trees. L4
INDEX

S. No. EXPERIMENT COs BL


1 Write a program in C to create two sets and perform the Union operation CO2 L4
on sets.
2 Write a program in C to create two sets and perform the Intersection CO2 L4
operation on sets.
3 Write a program in C to create two sets and perform the Difference CO2 L4
operation on sets.

4 Write a program in C to create two sets and perform the Symmetric CO2 L4
Difference operation.

5 Write a program in C to perform the Power Set operation on a set. CO2 L4

6 Write a C Program to find Cartesian Product of two sets. CO2 L4

7 Write a program in C to Display the Boolean Truth Table for AND, OR CO3 L4
, NOT .
8 Write a program in C for Minimum Cost Spanning Tree. CO4 L4

9 Write a program in C for finding Shortest Path in a Graph. CO4 L4

10 Write a program in C to calculate the Permutations and Combinations. CO1 L3


EXPERIMENT No. 1

Aim: Write a program in C to create two sets and perform the Union operation on sets.

Content: Set Operations include Set Union, Set Intersection, Set Difference, Complement of
Set, and Cartesian Product.
Union of two sets :-
If A={1,1,2,3} and B={5,6,7,8} are two sets, then union of the set A and B is :-
A ∪ B = {1,2,3,5,6,7,8}

Program:-
Here A and B is the two sets and C is the union of set A and B.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],i,c[10],j,k=0,n1,n2;

// taking input set A


printf("Enter number of element of set A\n");
scanf("%d",&n1);
printf("Enter the element of set A \n");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);

// taking input set B


printf("Enter number of element of set B\n");
scanf("%d",&n2);
printf("Enter the element of set B \n");
for(i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for calculate union


// copy the element of set A in set C
for(i=0;i<n1;i++)
{
// repeted element is not allowed so we check is any value repeted
for(j=0;j<k;j++)
{
if(c[j]==a[i])
break;
}
if(j==k) //if not repesated then store value in set c
{
c[k]=a[i];
k++;
}
}
// copy element of set B in set C
for(i=0;i<n2;i++)
{
// check for repeted element
for(j=0;j<k;j++)
{
if(c[j]==b[i])
break;
}
if(j==k) // if element is not repeted then store in set C
{
c[k]=b[i];
k++;
}
}
// printing of union of set A and set B
printf("Union of set A and B is:-\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);
}

Output:

Screenshot
EXPERIMENT No. 2

Aim: Write a program in C to create two sets and perform the Intersection operation on sets.

Content:
Intersection of two sets :- Intersection of two sets is the set in which only those element is
present which are present in both set A and set B.
If A={1,2,3,4,1} and B={1,2,5,6} are two sets then intersection of A and B is:-
A∩B = {1,2}

Program:-
Here A and B is the two sets and C is the intersection of set A and B.

#include<stdio.h>
int main()
{
int a[100],b[100],c[100],n1,n2,n,k=0,i,j;

// taking input of set A

printf("Enter number of element of set A\n");


scanf("%d",&n1);
printf("Enter elements of set A\n");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);

// taking input set B

printf("Enter number of element of set B\n");


scanf("%d",&n2);
printf("Enter elements of set B\n");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);

// Logic for intersection

for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
}

// Printing the elements of intersection of set A and set B


printf("Intersection of set A and set B is:-\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);

return 0;
}

Output:

Screenshot
EXPERIMENT No. 3

Aim: Write a program in C to create two sets and perform the Difference operation on sets.

Content:
If A and B are two sets, then their difference is given by A - B or B - A.
If A = {2, 3, 4} and B = {4, 5, 6},
(i) A - B means elements of A which are not the elements of B.
i.e., in the above example A - B = {2, 3}
(ii) B -A means elements of B which are not the elements of A.
i.e., in the above example B -A = {5, 6}

Program:-
Here A and B is the two sets and C is the difference of set A and B.

#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n1,n2,l,i,j;
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set A");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set B");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for find A-B

for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
// here we check that is element already present in the set
// if present than ignore it otherwise add to the difference set
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}
}

// logic for find B-A

for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is element already present in the set
//if present than ignore it otherwise add to the difference set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}

}
printf("Difference of A-B is:-\n");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
printf("\n");
printf("Difference of B-A is:-\n");
for(i=0;i<m;i++)
{
printf("%d ",d[i]);
}
return 0;
}

Output:

Screenshot
EXPERIMENT No. 4

Aim: Write a program in C to create two sets and perform the Symmetric difference operation
on sets.

Content:
Symmetric difference of the two set is:
Let the set M = {2, 6, 8, 12, 19, 23, 27, 54} and set N = {4, 5, 10, 24, 19, 27, 36, 49}
M-N = {2, 6, 8, 12, 23, 54} (If set N elements remove from set M and the remaining
elements left in set M write it)
N-M = {4, 5, 10, 24, 36, 49} (If set M elements remove from set N and the remaining
elements left in set N write it)
Therefore, MΔN = (M-N) U (N-M) = {2, 6, 8, 12, 23, 54} U {4, 5, 10, 24, 36, 49}
= {2, 4, 5, 6, 8, 10, 12, 23, 24, 36, 49, 54}

Program:-

#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n=0,n1,n2,l,i,j,sy[100];
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set A");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set B");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for find A-B


for( i=0;i<n1;i++)
{
// here we check that is b[i] already present in the ans set
// if present then ignore it otherwise add it to the ans set
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}
}

// logic for find B-A


for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is b[i] already present in the ans set
// if present then ignore it otherwise add it to the ans set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}
}
//logic for symmetric Difference
for(i=0;i<k;i++)
{
sy[n]=c[i];
n++;
}
for(i=0;i<m;i++)
{
sy[n]=d[i];
n++;
}
printf("\nSymmetric Difference of sets is:-\n");
for(i=0;i<n;i++)
printf("%d ",sy[i]);
return 0;
}

Output:

Screenshot
EXPERIMENT No. 5

Aim: Write a program in C to perform the Power Set operation on a set.

Content: Power Set Power set P(S) of a set S is the set of all subsets of S. For example S =
{a, b, c} then P(S) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}.
If S has n elements in it then P(S) will have 2n elements

Program:-
#include <stdio.h>
#include <math.h>
void printPowerSet(char *set, int set_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned int pow_set_size = pow(2, set_size);
int counter, j;

/*Run from counter 000..0 to 111..1*/


for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
/* Check if jth bit in the counter is set
If set then print jth element from set */
if(counter & (1<<j))
printf("%c", set[j]);
}
printf("
");
}
}
/*Driver program to test printPowerSet*/
int main()
{
char set[] = {'a','b','c'};
printPowerSet(set, 3);

return 0;
}

Output:
Screenshot
EXPERIMENT No. 6

Aim: Write a C Program to find Cartesian Product of two sets.

Content:
Cartesian Product of two set:-
If set A={1,2} and set B={4,5,6,7} then cartesian product of set A and B is :-
A*B={ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }

Program:-
#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2;
printf("Enter size of set A\n");
scanf("%d",&n1);
printf("Enter element of set A\n");
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B\n");
scanf("%d",&n2);
printf("Enter element of set B\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);

// logic for cartesian product

printf("Cartesian product = ");


printf("{");
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
printf(" (%d %d) ",a[i],b[j]);
}
}
printf("}");
return 0;
}

Output:
Screenshot
EXPERIMENT No. 7

Aim: Write a program in C to Display the Boolean Truth Table for AND, OR, NOT.

Content:

A Logic gate is an elementary building block of any digital circuits. It takes one or two inputs and
produces output based on those inputs. Outputs may be high (1) or low (0).
In a computer, most of the electronic circuits are made up logic gates. Logic gates are used to create a
circuit that performs calculations, data storage or shows off object-oriented programming especially the
power of inheritance.
There are seven basic logic gates defined, these are:
1. AND gate,
2. OR gate,
3. NOT gate,
4. NAND gate,
5. NOR gate,
6. XOR gate and
7. XNOR gate.

Below are the brief details about them (AND, OR, NOT gates):

1. AND Gate: The AND gate gives an output of 1 if both the two inputs are 1 and gives 0 otherwise.

2. OR Gate: The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.

3. NOT Gate: It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the
result as 0 and vice-versa.
Program:-

#include<stdio.h>

// logic for OR gate

int find_OR(int x,int y)


{
if(x==1 && y==1)
return 1;
if(x==1 && y==0 || x==0 && y==1)
return 1;
if(x==0 && y==0)
return 0;
}

// logic for find AND

int find_AND(int x,int y)


{
if(x==1 && y==1)
return 1;
else
return 0;
}

// logic for find NOT

int find_NOT(int x)
{
if(x==1)
return 0;
else
return 1;
}

// Driver function

int main()
{
int ch,a,b;
printf("1. OR\n");
printf("2. AND\n");
printf("3. NOT\n");
printf("4 .exit\n");
while(1)
{
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Give two input 1 for true and 0 for false\n");
scanf("%d%d",&a,&b);
printf("%d",find_OR(a,b));
break;
case 2: printf("Give two input 1 for true and 0 for false\n");
scanf("%d%d",&a,&b);
printf("%d",find_AND(a,b));
break;
case 3: printf("Give an input 1 for true and 0 for false\n");
scanf("%d",&a);
printf("%d",find_NOT(a));
break;
case 4: exit(0);
default: printf("Wrong key\n");
}
}
}

Output:

Screenshot
EXPERIMENT No. 8

Aim: Write a program in C for Minimum Cost Spanning Tree.

Content:
Given an undirected graph of V nodes (V > 2) named V1, V2, V3, …, Vn.
Two nodes Vi and Vj are connected to each other if and only if 0 < | i – j | ≤ 2. Each edge
between any vertex pair (Vi, Vj) is assigned a weight i + j. The task is to find the cost of
the minimum spanning tree of such graph with V nodes. Graph should be weighted,
connected, and undirected.

Example:
Input: V = 4
Output: 13

Program:-

#include<stdio.h>
int main()
{
int cost[10][10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of adjacency matrix\n");
//input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
// cost is 0 then initialize it by maximum value
if(cost[i][j]==0)
cost[i][j]=1000;
}
}
// logic for finding minimum cost spanning tree
visited[1]=1; // visited first node
while(no_e<n)
{
min=1000;
// in each cycle find minimum cost
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=i;
b=j;
}
}
}
}
//if node is not visited
if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;
// initialize with maximum value you can also use any other value
cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is %d",min_cost);
return 0;
}

Output:

Screenshot
EXPERIMENT No. 9

Aim: Write a program in C for finding Shortest Path in a Graph.

Content:
We are given a graph with a source vertex in the graph. And we have to find the shortest path
from the source vertex to all other vertices of the graph.
The Dijikstra’s algorithm is a greedy algorithm to find the shortest path from the source
vertex of the graph to the root node of the graph.

Program:-

#include<stdio.h>
int main()
{
int cost[10][10],i,j,n,source,target,visited[10]={0},min=999,dist[10],pre[10];
int start,m,d,path[10];
printf("Enter number of nodes\n ");
scanf("%d",&n);
printf("Enter weight of all the paths in adjacency matrix form\n");

// Input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("Enter the source\n");
scanf("%d",&source);
printf("Enter the target\n");
scanf("%d",&target);

// logic for dijkstra's aglorithm


start=source;
for(i=1;i<=n;i++)
{
dist[i]=999; // here we initialize all distances with the maximum value (999) you take any
other value also
pre[i]=-1; // pre for the previous node
}
visited[source]=1; // visited source node
dist[source]=0; // distance of first node from first node is 0
while(visited[target]==0)
{
min=999;
m=0;
for(i=1;i<=n;i++)
{
d=dist[start]+cost[start][i]; // calcualte the distance from the source
if(d<dist[i] && visited[i]==0)
{
dist[i]=d;
pre[i]=start;
}
if(min>dist[i] && visited[i]==0)
{
min=dist[i];
m=i;
}
}
start=m;
visited[m]=1;
}
// logic for printing path
start=target;
j=0;
while(start!=-1)
{
path[j++]=start;
start=pre[start];
}
// printing of the path
for(i=j-1;i>=0;i--)
{
if(i!=j-1)
printf(" to ");
printf("%d",path[i]);
}

printf("\n shortest path is %d",dist[target]);


return 0;
}

Output:
Screenshot
EXPERIMENT No. 10

Aim: Write a program in C to calculate the Permutations and Combinations.

Content:
In C programming language, nCr is referred as the combination. nCr is the selection of r
objects from a set of n objects, where the order of objects does not matter.
nP is referred as the permutation. nP is arrangement of 'r' objects from a set of 'n' objects,
r r
which should be in an order or sequence.

Permutations and combinations formulas


The formulas to find the permutation and combination of given numbers in C language are
given below −
 nCr = n! / (r! * (n-r)!)
 nPr = n! / (n-r)!

The logic used to find nCr is as follows −


result = factorial(n)/(factorial(r)*factorial(n-r));
The logic used to find nPr is as follows −
result = factorial(n)/factorial(n-r);

Program:-

Following is the C program to find the permutation and combination of given numbers –

#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main(){
int n, r;
long ncr, npr;
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r) {
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r) {
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}

Output:

Screenshot

You might also like